"""

Author: Obrtac Tomas (obrtac.tomas@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import Showing_hiding_files as shgui_op
import REST_api as Ra

#Tato cela kniznica sluzi opat na vkladanie hodnot do inputov
#Je to v podstate iba skryvanie a doplnovanie textu

def put_values_to_device_edit(self):
    shgui_op.List_of_devices_hide(self)
    shgui_op.Create_new_device_show(self)
    shgui_op.Create_button_hide(self)
    shgui_op.Edit_button_show(self)
    index = self.devices_list.currentRow()
    device = Ra.parameters_of_device(self.devices_addreses[index])
    self.DevEUI_input.setText(device.deveui)
    self.AppEUI_input.setText(device.appeui)
    self.AppKey_input.setText(device.appkey)
    self.Set_Power_input.setCurrentIndex(int(device.power))
    self.Set_data_rate_input.setCurrentIndex(int(device.datr))
    if device.app == "semtech-mote":
        self.ApplicationD_input.setCurrentIndex(0)
    elif device.app == "microchip-mote":
        self.ApplicationD_input.setCurrentIndex(1)
    else:
        self.ApplicationD_input.setCurrentIndex(2)
    if device.chans != None: #Parcovanie channelov na ktorych moze vysielat
        channels = device.chans
        channels = channels.replace(" ", "")
        channels = channels.split(",")
        name = "Check_box_"
        for i in range(len(channels)):
            double = channels[i]
            double = double.split("-")
            for j in range(int(double[0]),int(double[-1])+1):
                exec("self.%s%d.setChecked(1)" %(name,j))
    return

#Mazanie hodnot v labeloch od devices
def delete_values_in_devices(self):
    self.DevEUI_input.setText("")
    self.AppEUI_input.setText("")
    self.AppKey_input.setText("")
    self.Set_Power_input.setCurrentIndex(0)
    self.Set_data_rate_input.setCurrentIndex(0)
    uncheck_buttons(self)

def put_values_to_links_edit(self):
    shgui_op.List_of_devaddr_hide(self)
    shgui_op.Create_new_link_show(self)
    shgui_op.Create_button_hide(self)
    shgui_op.Edit_button_show(self)
    index = self.links_list.currentRow()
    link = Ra.parameters_of_link(self.link_addreses[index],self.name,self.password)
    self.Devaddr_input.setText(link.devaddr)
    self.NwksKEY_input.setText(link.nwkskey)
    self.AppSKey_input.setText(link.appskey)
    self.Set_Power_input.setCurrentIndex(int(link.power))
    self.Set_data_rate_input.setCurrentIndex(int(link.datr))
    self.FCnt_Check_input.setCurrentIndex(int(link.fcnt_check))
    if link.app == "semtech-mote":
        self.Application_input.setCurrentIndex(0)
    elif link.app == "microchip-mote":
        self.Application_input.setCurrentIndex(1)
    else:
        self.Application_input.setCurrentIndex(2)
    if link.chans != None: #Opat parcovanie channelov na ktorych moze vysielat
        channels = link.chans
        channels = channels.replace(" ", "")
        channels = channels.split(",")
        name = "Check_box_"
        for i in range(len(channels)):
            double = channels[i]
            double = double.split("-")
            for j in range(int(double[0]), int(double[-1]) + 1):
                exec ("self.%s%d.setChecked(1)" % (name, j))
    return

def put_values_to_links_show(self,devaddr):
    shgui_op.List_of_devices_hide(self)
    shgui_op.Create_button_hide(self)
    shgui_op.Back_button_for_devices_show(self)
    link = Ra.parameters_of_link(devaddr)
    if link == -1:
        return
    self.Devaddr_input.setText(link.devaddr)
    self.NwksKEY_input.setText(link.nwkskey)
    self.AppSKey_input.setText(link.appskey)
    self.Set_Power_input.setCurrentIndex(int(link.power))
    self.Set_data_rate_input.setCurrentIndex(int(link.datr))
    self.FCnt_Check_input.setCurrentIndex(int(link.fcnt_check))
    if link.app == "semtech-mote":
        self.Application_input.setCurrentIndex(0)
    elif link.app == "microchip-mote":
        self.Application_input.setCurrentIndex(1)
    else:
        self.Application_input.setCurrentIndex(2)
    if link.chans != None: #Parcovanie channelov na ktorych moze vysielat
        channels = link.chans
        channels = channels.replace(" ", "")
        channels = channels.split(",")
        name = "Check_box_"
        for i in range(len(channels)):
            double = channels[i]
            double = double.split("-")
            for j in range(int(double[0]), int(double[-1]) + 1):
                exec ("self.%s%d.setChecked(1)" % (name, j))
    return

def put_values_to_gateways_edit(self):
    shgui_op.List_of_gateways_hide(self)
    shgui_op.Create_new_gateway_show(self)
    shgui_op.Create_button_hide(self)
    shgui_op.Edit_button_show(self)
    index = self.gateway_list.currentRow()
    gateway = Ra.parameters_of_gateway(self.gateway_addresses[index])
    self.MAC_input.setText(gateway.mac)
    self.TX_Channel_input.setText(gateway.tx_rfch)
    self.NetID_input.setText(gateway.netid)
    return
#Mazanie hodnot v labeloch od linkov
def delete_values_in_links(self):
    self.Devaddr_input.setText("")
    self.NwksKEY_input.setText("")
    self.AppSKey_input.setText("")
    self.Set_Power_input.setCurrentIndex(0)
    self.Set_data_rate_input.setCurrentIndex(0)
    uncheck_buttons(self)
#Mazanie hodnot v labeloch od gatewayov
def delete_values_in_gateways(self):
    self.MAC_input.setText("")
    self.TX_Channel_input.setText("")
    self.NetID_input.setText("")

def uncheck_buttons(self):
    name = "Check_box_"
    for j in range(1,16):
        exec ("self.%s%d.setChecked(0)" % (name, j))


