"""

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/>.

"""


from PySide import QtGui
import REST_api as Ra
import matplotlib
matplotlib.use('Qt4Agg')
import datetime
import time
import numpy
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PySide import QtCore
import Showing_hiding_files as shgui_op
import Buttons_actions as BTN_ac
import Set_text_to_inputs as Set_text
from matplotlib.dates import DateFormatter as DateFormatter
from matplotlib.ticker import MaxNLocator

class First_page_layout(QtGui.QWidget):

    def __init__(self,name,password):
        super(First_page_layout,self).__init__()
        self.previous_command = 0
        self.actual_command = 0
        self.data_from_device = []
        self.name = name
        self.password = password
        self.link_addreses = Ra.list_of_devaddr(self.name,self.password)
        self.devices_addreses = Ra.list_of_devEUI(self.name,self.password)
        self.gateway_addresses = Ra.list_of_gateways(self.name,self.password)
        self.setGeometry(300,300,900,550)
        self.setWindowTitle("Menu")
        self.setFixedSize(self.size())
        self.option_list_init()
        self.Create_button_init()
        self.Edit_button_init()
        self.Back_button_init()
        self.Back_button_for_devices_init()
        self.Delete_button_init()
        self.Show_button_init()
        self.Back_graphs_button_init()
        self.horizontal_line_init()
        self.Refresh_button_init()
        self.list_of_links_init(self.link_addreses)
        self.list_of_devices_init(self.devices_addreses)
        self.list_of_gateways_init(self.gateway_addresses)
        self.TabWidget_for_graphs_init()
        self.list_of_dates_init(None)
        self.User_name_init(self.name)
        self.Logout_button_init()
        self.link_rx = []
        #self.TabWidget_for_graphs_init(self.link_addreses[0])
        self.Create_new_link_init()
        self.Create_new_device_init()
        self.Create_new_Gateway_init()
    # Klasika runapp aby sa nam zobrazil objekt
    def runapp(self):
        self.show()

    #Inicializujeme lavy list s moznostami
    def option_list_init(self):
        self.option_list = QtGui.QTreeWidget(self)
        self.option_list.move(20, 100)
        self.option_list.setFixedSize(200, 430)
        self.option_list.setColumnCount(1)
        # Celkovo toto je pre vytvorenie stylu moznosti
        self.User = Tree_options("User",1, self.option_list)
        self.Link = Tree_options("Links",2,self.option_list)
        #Moznosti v menu, asi to bude celkom porod naprogramovat.....
        self.Link_option1 = Tree_subclass_options("Watch accepted data",21, self.Link)
        #self.Link_option1.textLabel.mouseReleaseEvent = self.watch_accepted_rxframes
        self.Link_option2 = Tree_subclass_options("Create new link",22, self.Link)
        #self.Link_option2.textLabel.mouseReleaseEvent = self.Create_new_link
        self.Link_option3 = Tree_subclass_options("Edit existing link",23, self.Link)
        #self.connect(self.Link_option3, QtCore.SIGNAL('itemClicked(QTreeWidgetItem *, int)'), self.bla)
        self.Link_option4 = Tree_subclass_options("Delete existing link",24, self.Link)
        self.Device = Tree_options("Devices",3, self.option_list)
        self.Device_option2 = Tree_subclass_options("Create new device",32, self.Device)
        self.Device_option3 = Tree_subclass_options("Edit existing device",33, self.Device)
        self.Device_option4 = Tree_subclass_options("Delete existing device",34, self.Device)
        self.Device_option6 = Tree_subclass_options("Devices links", 36, self.Device)
        self.Gateway = Tree_options("Gateways",4, self.option_list)
        self.Gateway_option2 = Tree_subclass_options("Create new Gateway",41, self.Gateway)
        self.Gateway_option3 = Tree_subclass_options("Edit existing Gateway",42, self.Gateway)
        self.Gateway_option4 = Tree_subclass_options("Delete existing Gateway",43, self.Gateway)
        self.Gateway_option4 = Tree_subclass_options("Define Gateway",44, self.Gateway)
        self.option_list.itemPressed.connect(self.Layout_configuration)
        self.option_list.header().close()
        #self.option_list.mouseReleaseEvent= self.Windows_command

    # Inicializovanie horizontalnej ciarky
    def horizontal_line_init(self):
        self.horizontal_line = QtGui.QFrame(self)
        self.horizontal_line.setFrameStyle(QtGui.QFrame.HLine)
        self.horizontal_line.move(10, 70)
        self.horizontal_line.setFixedWidth(880)
  # Zoznam linkov
    def list_of_links_init(self,adresses): #List s devices
        self.links_list = QtGui.QListWidget(self)
        self.links_list.move(230, 100)
        self.links_list.setFixedSize(140, 430)
        length_of_adress = len(adresses)
        self.qlistwidgetitem_links = []
        if adresses:
            for i in range(length_of_adress): #Postupne priadavanie adries
                Linklayout = Links(adresses[i])
                self.qlistwidgetitem_links.append(QtGui.QListWidgetItem(self.links_list))
                self.qlistwidgetitem_links[i].setSizeHint(Linklayout.sizeHint())
                self.links_list.addItem(self.qlistwidgetitem_links[i])
                self.links_list.setItemWidget(self.qlistwidgetitem_links[i], Linklayout)
                # self.devices_list.addItem(self.addreses[i])
        self.links_list.hide()
        self.links_list.itemPressed.connect(self.link_managment)
        self.links_list.hide()
    #Zoznam zariadeni
    def list_of_devices_init(self,adresses): #List s devices
        self.devices_list = QtGui.QListWidget(self)
        self.devices_list.move(230, 100)
        self.devices_list.setFixedSize(140, 430)
        length_of_adress = len(adresses)
        self.qlistwidgetitem = []
        if adresses :
            for i in range(length_of_adress): #Postupne pridavame adresy, Link je widget ktory urcuje ich vzhlad
                Linklayout = Links(adresses[i])
                self.qlistwidgetitem.append(QtGui.QListWidgetItem(self.devices_list))
                self.qlistwidgetitem[i].setSizeHint(Linklayout.sizeHint())
                self.devices_list.addItem(self.qlistwidgetitem[i])
                self.devices_list.setItemWidget(self.qlistwidgetitem[i],Linklayout)
                #self.devices_list.addItem(self.addreses[i])
        self.devices_list.hide()
        self.devices_list.itemPressed.connect(self.device_managment)
    #Zoznam Gateways
    def list_of_gateways_init(self,adresses): #List s devices
        self.gateway_list = QtGui.QListWidget(self)
        self.gateway_list.move(230, 100)
        self.gateway_list.setFixedSize(140, 430)
        length_of_adress = len(adresses)
        self.qlistwidgetitem = []
        if adresses :
            for i in range(length_of_adress): #Postupne pridavanie zariadeni
                Linklayout = Links(adresses[i])
                self.qlistwidgetitem.append(QtGui.QListWidgetItem(self.gateway_list))
                self.qlistwidgetitem[i].setSizeHint(Linklayout.sizeHint())
                self.gateway_list.addItem(self.qlistwidgetitem[i])
                self.gateway_list.setItemWidget(self.qlistwidgetitem[i],Linklayout)
                #self.devices_list.addItem(self.addreses[i])
        self.gateway_list.hide()
        self.gateway_list.itemPressed.connect(self.gateway_managment)

    #List na ktorom su zobrazene jednotlive casy, zatial tam nic nevkladam
    def list_of_dates_init(self,dev_addr): #List s devices
        self.Dates_list = QtGui.QListWidget(self)
        self.Dates_list.move(230, 100)
        self.Dates_list.setFixedSize(140, 430)
        self.Dates_list.hide()
        self.Dates_list.itemPressed.connect(self.Dates_managment)

    #Tabwidget na ktorom su nasledne zobrazene grafy
    def TabWidget_for_graphs_init(self):
        self.TabWidget_for_graphs = QtGui.QTabWidget(self)
        self.TabWidget_for_graphs.move(380,100)
        self.TabWidget_for_graphs.setFixedSize(510,395)
        #Najprv grafy vytvorime, nasledne ich tam vlozime, Pre grafy je vytvorena vlastna trieda Graph_widget
        self.Temperature_plot = Graph_widget(datetime.datetime.now(), 1)
        self.Pressure_plot = Graph_widget(datetime.datetime.now(), 1)
        self.Humidity_plot = Graph_widget(datetime.datetime.now(), 1)
        self.Light_plot = Graph_widget(datetime.datetime.now(), 1)
        self.TabWidget_for_graphs.addTab(self.Temperature_plot, "Temperature")
        self.TabWidget_for_graphs.addTab(self.Humidity_plot, "Humidity")
        self.TabWidget_for_graphs.addTab(self.Pressure_plot, "Pressure")
        self.TabWidget_for_graphs.addTab(self.Light_plot, "Light intensity")
        self.TabWidget_for_graphs.hide()
        #Definujeme prvu stranu
        #Funkcia, vlozime devaddr a uz z raz vyparcovanych dat pouzijeme tie spravne

    #Inicializovanie User Name
    def User_name_init(self,name):
        self.User_name = QtGui.QLabel(self)
        self.User_name.setText(name)
        self.User_name.move(20,20)
        myfont = QtGui.QFont("Calibri", 40)
        #myfont.setBold(True)
        self.User_name.setFont(myfont)
    #Tlacitko logout
    def Logout_button_init(self):
        self.Logout_button = QtGui.QLabel(self)
        url = unicode(R"./images/index.jpeg") #Nahrame do neho obrazok
        picture = QtGui.QPixmap("./images/on_off.png")
        self.Logout_button.setFixedSize(50,50)
        self.Logout_button.move(830,20)
        #picture.load('images/index.jpeg')
        transparent = QtGui.QColor(0,0,0,0)
        pallete = QtGui.QPalette(self.Logout_button.palette())
        pallete.setColor(QtGui.QPalette.Button,transparent)
        self.Logout_button.setPalette(pallete)
        self.Logout_button.setPixmap(picture)

    #Inicializovanie veci pre create new link
    def Create_new_link_init(self):
        # devaddr label
        upperscript = u"\u002A"
        self.Devaddr_label = QtGui.QLabel(self)
        self.Devaddr_label.setText("DevAddr"+upperscript)
        myfont = QtGui.QFont("Calibri", 12)
        # myfont.setBold(True)
        self.Devaddr_label.setFont(myfont)
        self.Devaddr_label.move(260,110)

        self.Devaddr_input = QtGui.QLineEdit(self)
        self.Devaddr_input.setFixedSize(510, 30)
        self.Devaddr_input.move(380, 103)  # Pozicia voci systemu
        self.Devaddr_input.setStyleSheet("padding-left: 5px")
        self.Devaddr_input.setPlaceholderText("Dev Addr")
        self.Devaddr_input.setFont(QtGui.QFont("Arial", 10))


        # Vertikalna ciara init
        self.vertical_line = QtGui.QFrame(self)
        self.vertical_line.setFrameStyle(QtGui.QFrame.VLine)
        self.vertical_line.move(193,100)
        self.vertical_line.setFixedHeight(430)
        #Region label
        self.Region_label = QtGui.QLabel(self)
        self.Region_label.setText("Region")
        self.Region_label.setFont(myfont)
        self.Region_label.move(260, 150)

        self.Region_input = QtGui.QComboBox(self)
        self.Region_input.setFixedSize(510, 30)
        self.Region_input.move(380, 143)
        self.Region_input.addItems(["EU 863-870 MHz", "US 902-928 MHz", "China 779-787 MHz","EU 433 MHz","Australia 915-928MHz"
                                     ,"China 470-510 MHz"])

        #Frequency label
        self.Application_label = QtGui.QLabel(self)
        self.Application_label.setText("Application")
        self.Application_label.setFont(myfont)
        self.Application_label.move(260, 190)

        self.Application_input = QtGui.QComboBox(self)
        self.Application_input.setFixedSize(510, 30)
        self.Application_input.move(380, 183)
        self.Application_input.addItems(["semtech-mote","microchip-mote","websocket"])
        #AppId
        self.AppID_label = QtGui.QLabel(self)
        self.AppID_label.setText("AppID")
        self.AppID_label.setFont(myfont)
        self.AppID_label.move(260, 230)

        self.AppID_input = QtGui.QLineEdit(self)
        self.AppID_input.setFixedSize(530, 30)
        self.AppID_input.move(360, 223)  # Pozicia voci systemu
        self.AppID_input.setStyleSheet("padding-left: 5px")
        self.AppID_input.setPlaceholderText("AppID")
        self.AppID_input.setFont(QtGui.QFont("Arial", 10))
        #NwskKey
        self.NwkSKey_label = QtGui.QLabel(self)
        self.NwkSKey_label.setText("NwkSKey"+upperscript)
        self.NwkSKey_label.setFont(myfont)
        self.NwkSKey_label.move(260, 270)

        self.NwksKEY_input = QtGui.QLineEdit(self)
        self.NwksKEY_input.setFixedSize(510, 30)
        self.NwksKEY_input.move(380, 263)  # Pozicia voci systemu
        self.NwksKEY_input.setStyleSheet("padding-left: 5px")
        self.NwksKEY_input.setPlaceholderText("Nwks Key")
        self.NwksKEY_input.setFont(QtGui.QFont("Arial", 10))

        #Appskey
        self.AppSKey_label = QtGui.QLabel(self)
        self.AppSKey_label.setText("AppSKey"+upperscript)
        self.AppSKey_label.setFont(myfont)
        self.AppSKey_label.move(260, 310)

        self.AppSKey_input = QtGui.QLineEdit(self)
        self.AppSKey_input.setFixedSize(510, 30)
        self.AppSKey_input.move(380, 303)  # Pozicia voci systemu
        self.AppSKey_input.setStyleSheet("padding-left: 5px")
        self.AppSKey_input.setPlaceholderText("Apps Key")
        self.AppSKey_input.setFont(QtGui.QFont("Arial", 10))

        self.FCnt_Chcek_label = QtGui.QLabel(self)
        self.FCnt_Chcek_label.setText("Fcnt Check")
        self.FCnt_Chcek_label.setFont(myfont)
        self.FCnt_Chcek_label.move(260,350)

        self.FCnt_Check_input = QtGui.QComboBox(self)
        self.FCnt_Check_input.setFixedSize(510, 30)
        self.FCnt_Check_input.move(380, 343)  # Pozicia voci systemu
        self.FCnt_Check_input.addItems(["Strict 16-bit", "Strict 32-bit", "Reset on zero", "Disabled"])


        self.Set_Power_label = QtGui.QLabel(self)
        self.Set_Power_label.setText("Set Power")
        self.Set_Power_label.setFont(myfont)
        self.Set_Power_label.move(260, 390)

        self.Set_Power_input = QtGui.QComboBox(self)
        self.Set_Power_input.setFixedSize(510, 30)
        self.Set_Power_input.move(380, 383)
        self.Set_Power_input.addItems(["20 dBm","14 dBm", "11 dBm", "8 dBm", "5 dBm", "2 dBm"])

        self.SET_data_rate_label = QtGui.QLabel(self)
        self.SET_data_rate_label.setText("Set data rate")
        self.SET_data_rate_label.setFont(myfont)
        self.SET_data_rate_label.move(260, 430)

        self.Set_data_rate_input = QtGui.QComboBox(self)
        self.Set_data_rate_input.setFixedSize(510, 30)
        self.Set_data_rate_input.move(380, 423)
        self.Set_data_rate_input.addItems(["SF12 125kHz (250 bit/s)","SF11 125kHz (440 bit/s)","SF10 125kHz (980 bit/s)","SF9 125kHz (1760 bit/s)",
                                           "SF8 125kHz (3125 bit/s)", "SF7 125kHz (5470 bit/s)", "SF7 125kHz (11000 bit/s)"])

        self.Set_channel_label = QtGui.QLabel(self)
        self.Set_channel_label.setText("Set chanel")
        self.Set_channel_label.setFont(myfont)
        self.Set_channel_label.move(260, 470)

        self.Check_box_1=QtGui.QCheckBox(self)
        self.Check_box_1.move(380,460)

        self.Check_box_1_label = QtGui.QLabel("1",self)
        self.Check_box_1_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_1_label.move(386,480)

        self.Check_box_2 = QtGui.QCheckBox(self)
        self.Check_box_2.move(410, 460)

        self.Check_box_2_label = QtGui.QLabel("2", self)
        self.Check_box_2_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_2_label.move(416, 480)

        self.Check_box_3 = QtGui.QCheckBox(self)
        self.Check_box_3.move(440, 460)

        self.Check_box_3_label = QtGui.QLabel("3", self)
        self.Check_box_3_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_3_label.move(446, 480)

        self.Check_box_4 = QtGui.QCheckBox(self)
        self.Check_box_4.move(470, 460)

        self.Check_box_4_label = QtGui.QLabel("4", self)
        self.Check_box_4_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_4_label.move(476, 480)

        self.Check_box_5 = QtGui.QCheckBox(self)
        self.Check_box_5.move(500, 460)

        self.Check_box_5_label = QtGui.QLabel("5", self)
        self.Check_box_5_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_5_label.move(506, 480)

        self.Check_box_6 = QtGui.QCheckBox(self)
        self.Check_box_6.move(530, 460)

        self.Check_box_6_label = QtGui.QLabel("6", self)
        self.Check_box_6_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_6_label.move(536, 480)

        self.Check_box_7 = QtGui.QCheckBox(self)
        self.Check_box_7.move(560, 460)

        self.Check_box_7_label = QtGui.QLabel("7", self)
        self.Check_box_7_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_7_label.move(566, 480)

        self.Check_box_8 = QtGui.QCheckBox(self)
        self.Check_box_8.move(590, 460)

        self.Check_box_8_label = QtGui.QLabel("8", self)
        self.Check_box_8_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_8_label.move(596, 480)

        self.Check_box_9 = QtGui.QCheckBox(self)
        self.Check_box_9.move(620, 460)

        self.Check_box_9_label = QtGui.QLabel("9", self)
        self.Check_box_9_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_9_label.move(626, 480)

        self.Check_box_10 = QtGui.QCheckBox(self)
        self.Check_box_10.move(650, 460)

        self.Check_box_10_label = QtGui.QLabel("10", self)
        self.Check_box_10_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_10_label.move(653, 480)

        self.Check_box_11 = QtGui.QCheckBox(self)
        self.Check_box_11.move(680, 460)

        self.Check_box_11_label = QtGui.QLabel("11", self)
        self.Check_box_11_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_11_label.move(683, 480)

        self.Check_box_12 = QtGui.QCheckBox(self)
        self.Check_box_12.move(710, 460)

        self.Check_box_12_label = QtGui.QLabel("12", self)
        self.Check_box_12_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_12_label.move(713, 480)

        self.Check_box_13 = QtGui.QCheckBox( self)
        self.Check_box_13.move(740, 460)

        self.Check_box_13_label = QtGui.QLabel("13", self)
        self.Check_box_13_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_13_label.move(743, 480)

        self.Check_box_14 = QtGui.QCheckBox(self)
        self.Check_box_14.move(770, 460)

        self.Check_box_14_label = QtGui.QLabel("14", self)
        self.Check_box_14_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_14_label.move(773, 480)

        self.Check_box_15 = QtGui.QCheckBox(self)
        self.Check_box_15.move(800, 460)

        self.Check_box_15_label = QtGui.QLabel("15", self)
        self.Check_box_15_label.setFont(QtGui.QFont("Arial", 10))
        self.Check_box_15_label.move(803, 480)
        #self.Check_box_1.

        #Vsetko skryjeme
        shgui_op.Create_new_link_hide(self)

    #inicializovanie Rozhrania pre nove zariadenie
    def Create_new_device_init(self):
        upperscript = u"\u002A"
        self.DevEUI_label = QtGui.QLabel(self)
        self.DevEUI_label.setText("DevEUI"+upperscript)
        myfont = QtGui.QFont("Calibri", 12)
        # myfont.setBold(True)
        self.DevEUI_label.setFont(myfont)
        self.DevEUI_label.move(260, 110)

        self.DevEUI_input = QtGui.QLineEdit(self)
        self.DevEUI_input.setFixedSize(510, 30)
        self.DevEUI_input.move(380, 103)  # Pozicia voci systemu
        self.DevEUI_input.setStyleSheet("padding-left: 5px")
        self.DevEUI_input.setPlaceholderText("Dev EUI")
        self.DevEUI_input.setFont(QtGui.QFont("Arial", 10))

        # Frequency label
        self.ApplicationD_label = QtGui.QLabel(self)
        self.ApplicationD_label.setText("Application")
        self.ApplicationD_label.setFont(myfont)
        self.ApplicationD_label.move(260, 190)

        self.ApplicationD_input = QtGui.QComboBox(self)
        self.ApplicationD_input.setFixedSize(510, 30)
        self.ApplicationD_input.move(380, 183)
        self.ApplicationD_input.addItems(["semtech-mote", "microchip-mote", "websocket"])

        # Appskey
        self.AppID_label = QtGui.QLabel(self)
        self.AppID_label.setText("AppID")
        self.AppID_label.setFont(myfont)
        self.AppID_label.move(260, 230)

        self.AppID_input = QtGui.QLineEdit(self)
        self.AppID_input.setFixedSize(510, 30)
        self.AppID_input.move(380, 223)  # Pozicia voci systemu
        self.AppID_input.setStyleSheet("padding-left: 5px")
        self.AppID_input.setPlaceholderText("AppID")
        self.AppID_input.setFont(QtGui.QFont("Arial", 10))

        self.AppEUI_label = QtGui.QLabel(self)
        self.AppEUI_label.setText("AppEUI"+upperscript)
        self.AppEUI_label.setFont(myfont)
        self.AppEUI_label.move(260, 270)

        self.AppEUI_input = QtGui.QLineEdit(self)
        self.AppEUI_input.setFixedSize(510, 30)
        self.AppEUI_input.move(380, 263)  # Pozicia voci systemu
        self.AppEUI_input.setStyleSheet("padding-left: 5px")
        self.AppEUI_input.setPlaceholderText("AppEUI")
        self.AppEUI_input.setFont(QtGui.QFont("Arial", 10))

        self.AppKey_label = QtGui.QLabel(self)
        self.AppKey_label.setText("AppKey"+upperscript)
        self.AppKey_label.setFont(myfont)
        self.AppKey_label.move(260, 310)

        self.AppKey_input = QtGui.QLineEdit(self)
        self.AppKey_input.setFixedSize(510, 30)
        self.AppKey_input.move(380, 303)  # Pozicia voci systemu
        self.AppKey_input.setStyleSheet("padding-left: 5px")
        self.AppKey_input.setPlaceholderText("AppKey")
        self.AppKey_input.setFont(QtGui.QFont("Arial", 10))

        self.Can_joinD_label = QtGui.QLabel(self)
        self.Can_joinD_label.setText("Can Join? ")
        self.Can_joinD_label.setFont(myfont)
        self.Can_joinD_label.move(260, 350)

        self.Can_joinD_input = QtGui.QComboBox(self)
        self.Can_joinD_input.setFixedSize(510, 30)
        self.Can_joinD_input.move(380, 343)
        self.Can_joinD_input.addItems(["undefined", "true", "false"])

        shgui_op.Create_new_device_hide(self)

    #Menu pre vytvorenie novej Gateway
    def Create_new_Gateway_init(self):
        upperscript = u"\u002A"
        self.MAC_label = QtGui.QLabel(self)
        self.MAC_label.setText("Mac"+upperscript)
        myfont = QtGui.QFont("Calibri", 12)
        # myfont.setBold(True)
        self.MAC_label.setFont(myfont)
        self.MAC_label.move(260, 110)

        self.MAC_input = QtGui.QLineEdit(self)
        self.MAC_input.setFixedSize(510, 30)
        self.MAC_input.move(380, 103)  # Pozicia voci systemu
        self.MAC_input.setStyleSheet("padding-left: 5px")
        self.MAC_input.setPlaceholderText("MAC")
        self.MAC_input.setFont(QtGui.QFont("Arial", 10))

        # Frequency label
        self.TX_Channel_label = QtGui.QLabel(self)
        self.TX_Channel_label.setText("TX Channel"+upperscript)
        self.TX_Channel_label.setFont(myfont)
        self.TX_Channel_label.move(260, 150)

        self.TX_Channel_input = QtGui.QLineEdit(self)
        self.TX_Channel_input.setFixedSize(510, 30)
        self.TX_Channel_input.move(380, 143)
        self.TX_Channel_input.setStyleSheet("padding-left: 5px")
        self.TX_Channel_input.setPlaceholderText("TX Channel")
        self.TX_Channel_input.setFont(QtGui.QFont("Arial", 10))

        # Appskey
        self.NetID_label = QtGui.QLabel(self)
        self.NetID_label.setText("NetID"+upperscript)
        self.NetID_label.setFont(myfont)
        self.NetID_label.move(260, 190)

        self.NetID_input = QtGui.QLineEdit(self)
        self.NetID_input.setFixedSize(510, 30)
        self.NetID_input.move(380, 183)  # Pozicia voci systemu
        self.NetID_input.setStyleSheet("padding-left: 5px")
        self.NetID_input.setPlaceholderText("NetID")
        self.NetID_input.setFont(QtGui.QFont("Arial", 10))

        shgui_op.Create_new_gateway_hide(self)

    #Zistovanie prvotneho zobrazenia
    def Layout_configuration(self):

        self.actual_command = self.option_list.currentItem().recognizing_value
        if self.actual_command != self.previous_command:

            if self.actual_command == 21:
                self.Layout_configration_hide()
                shgui_op.List_of_devaddr_show(self)
                shgui_op.Refresh_button_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 23:
                self.Layout_configration_hide()
                shgui_op.List_of_devaddr_show(self)
                self.Devaddr_input.setDisabled(1)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 24:
                self.Layout_configration_hide()
                shgui_op.List_of_devaddr_show(self)
                shgui_op.Delete_button_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 22:
                self.Layout_configration_hide()
                shgui_op.Create_new_link_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 32:
                self.Layout_configration_hide()
                shgui_op.Create_new_device_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 41:
                self.Layout_configration_hide()
                shgui_op.Create_new_gateway_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 42:
                self.Layout_configration_hide()
                shgui_op.List_of_gateways_show(self)
                self.previous_command = self.actual_command
                Set_text.delete_values_in_gateways(self)
                self.MAC_input.setDisabled(1)
                return

            if self.actual_command == 43:
                self.Layout_configration_hide()
                shgui_op.Delete_button_show(self)
                shgui_op.List_of_gateways_show(self)
                self.previous_command = self.actual_command
                Set_text.delete_values_in_gateways(self)
                return

            if self.actual_command == 31:
                self.Layout_configration_hide()
                shgui_op.List_of_devices_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 33:
                self.Layout_configration_hide()
                shgui_op.List_of_devices_show(self)
                self.DevEUI_input.setDisabled(1)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 34:
                self.Layout_configration_hide()
                shgui_op.List_of_devices_show(self)
                shgui_op.Delete_button_show(self)
                self.previous_command = self.actual_command
                return

            if self.actual_command == 25:
                self.Layout_configration_hide()
                self.previous_command = self.actual_command

            if self.actual_command == 36:
                self.Layout_configration_hide()
                shgui_op.List_of_devices_show(self)
                self.previous_command = self.actual_command


#sluzi na skrytie veci po konfiguracii

    #Zistoanie co sa ma skryt po kliknuti na iny label
    def Layout_configration_hide(self):

        if self.previous_command == 21:
            shgui_op.List_of_devaddr_hide(self)
            shgui_op.List_of_devaddr_hide(self)
            shgui_op.List_of_dates_hide(self)
            shgui_op.TabWidget_for_graphs_hide(self)
            shgui_op.Back_graph_button_hide(self)
            shgui_op.Refresh_button_hide(self)
            return

        if self.previous_command == 23:
            shgui_op.Create_new_link_hide(self)
            Set_text.delete_values_in_links(self)
            shgui_op.Edit_button_hide(self)
            shgui_op.List_of_devaddr_hide(self)
            self.Devaddr_input.setDisabled(0)
            return

        if self.previous_command == 24:
            shgui_op.List_of_devaddr_hide(self)
            shgui_op.Delete_button_hide(self)
            return

        if self.previous_command == 22:
            shgui_op.Create_new_link_hide(self)
            Set_text.delete_values_in_links(self)
            return

        if self.previous_command == 32:
            shgui_op.Create_new_device_hide(self)
            Set_text.delete_values_in_devices(self)
            return

        if self.previous_command == 41:
            shgui_op.Create_new_gateway_hide(self)
            return

        if self.previous_command == 42:
            shgui_op.List_of_gateways_hide(self)
            shgui_op.Edit_button_hide(self)
            shgui_op.Create_new_gateway_hide(self)
            Set_text.delete_values_in_gateways(self)
            self.MAC_input.setDisabled(0)

        if self.previous_command == 43:
            shgui_op.List_of_gateways_hide(self)
            Set_text.delete_values_in_gateways(self)
            shgui_op.Create_new_gateway_hide(self)
            shgui_op.Delete_button_hide(self)


        if self.previous_command == 31:
            shgui_op.List_of_devices_hide(self)
            return

        if self.previous_command == 33:
            shgui_op.List_of_devices_hide(self)
            Set_text.delete_values_in_devices(self)
            shgui_op.Create_new_device_hide(self)
            shgui_op.Edit_button_hide(self)
            self.DevEUI_input.setDisabled(0)
            return

        if self.previous_command == 34:
            shgui_op.List_of_devices_hide(self)
            shgui_op.Delete_button_hide(self)
            return

        if self.previous_command == 36:
            shgui_op.List_of_devaddr_hide(self)
            shgui_op.Create_new_link_hide(self)
            Set_text.delete_values_in_links(self)
            shgui_op.Back_button_for_devices_hide(self)


#Zobrazi list s Devaddr

#Button ktory bude posielat veci na server
 #Tlacitoko na vytvorenie noveho device
# Button Create

    # Button Create pre vytvaranie zariadeni
    def Create_button_init(self):
        self.Create_button = QtGui.QPushButton(self)
        self.Create_button.setText("Create")
        self.Create_button.move(800,505)
        self.Create_button.setFixedSize(90,30)
        self.Create_button.hide()
        self.Create_button.clicked.connect(lambda: BTN_ac.Create_new_device(self))

    #Button Edit pre editovanie zariadeni
    def Edit_button_init(self):
        self.Edit_button = QtGui.QPushButton(self)
        self.Edit_button.setText("Edit")
        self.Edit_button.move(800,505)
        self.Edit_button.setFixedSize(90,30)
        self.Edit_button.hide()
        self.Edit_button.clicked.connect(lambda: BTN_ac.Edit_existing_device(self))

    #Tlacitko spat ktore je zobrazovane spolu s Edit Buttonom
    def Back_button_init(self):
        self.Back_button = QtGui.QPushButton(self)
        self.Back_button.setText("Back")
        self.Back_button.move(700,505)
        self.Back_button.setFixedSize(90,30)
        self.Back_button.hide()
        self.Back_button.clicked.connect(lambda: BTN_ac.Back_button(self))

    #Dalsie tlacitko ktore je vyobrazovane pri pozerani si linkov patriacich k devicom
    def Back_button_for_devices_init(self):
        self.Back_button_for_devices = QtGui.QPushButton(self)
        self.Back_button_for_devices.setText("Back")
        self.Back_button_for_devices.move(800,505)
        self.Back_button_for_devices.setFixedSize(90,30)
        self.Back_button_for_devices.hide()
        self.Back_button_for_devices.clicked.connect(lambda: BTN_ac.Back_button(self))

    #Tlacitko delete sa vyobrazi pri mazani zariadeni
    def Delete_button_init(self):
        self.Delete_button = QtGui.QPushButton(self)
        self.Delete_button.setText("Delete")
        self.Delete_button.move(800,505)
        self.Delete_button.setFixedSize(90,30)
        self.Delete_button.hide()
        self.Delete_button.clicked.connect(lambda: BTN_ac.Delete_device(self))

    #
    def Show_button_init(self):
        self.Show_button = QtGui.QPushButton(self)
        self.Show_button.setText("Show data")
        self.Show_button.move(800,505)
        self.Show_button.setFixedSize(90,30)
        self.Show_button.hide()
        #self.Show_button.clicked.connect(lambda: BTN_ac.Delete_device(self))

    # Tlacitko dozadu ktore je inicilaizovane pri zobrazovani grafov
    def Back_graphs_button_init(self):
        self.Back_graphs_button = QtGui.QPushButton(self)
        self.Back_graphs_button.setText("Back")
        self.Back_graphs_button.move(700,505)
        self.Back_graphs_button.setFixedSize(90,30)
        self.Back_graphs_button.hide()
        self.Back_graphs_button.clicked.connect(lambda: BTN_ac.back_to_list_of_devaddr(self))

    #Tlacitko na aktualizovanie grafu
    def Refresh_button_init(self):
        self.Refresh_button = QtGui.QPushButton(self)
        self.Refresh_button.setText("Refresh")
        self.Refresh_button.move(800,505)
        self.Refresh_button.setFixedSize(90,30)
        self.Refresh_button.hide()
        self.Refresh_button.clicked.connect(lambda: BTN_ac.Refresh_button_actions(self))

    #Obsluhuje klikanie na listu s devices
    def device_managment(self):
        if self.option_list.currentItem().recognizing_value == 33: #Pre pripad modifikacii
            Set_text.put_values_to_device_edit(self)
            return

        if self.option_list.currentItem().recognizing_value == 36: #Pre pripad ze si ideme zobrazit Link od device
            index = self.devices_list.currentRow()
            device = Ra.parameters_of_device(self.devices_addreses[index])
            if device.link == "": #V pripade ze Link neobsahuje nestane sa nic
                return
            else: #Link je, avsak ak bol zruseny objavia sa prazne kolonky
                shgui_op.List_of_devices_hide(self)
                shgui_op.Create_new_link_show(self)
                Set_text.put_values_to_links_show(self,device.link)
            return

    #Obsahuje obsluhu ktora nastane po kliknuti na listu s Linkami
    def link_managment(self):
        if self.option_list.currentItem().recognizing_value == 23: #V pripade editovania zariadeni
            Set_text.put_values_to_links_edit(self)
            return

        if self.option_list.currentItem().recognizing_value == 21: #Ked si ideme pozerat data
            #self.list_of_dates_init(self.link_addreses[self.links_list.currentRow()])
            shgui_op.List_of_devaddr_hide(self)
            shgui_op.List_of_dates_show(self) #Zakryjeme listu s devaddr, zobrazime datumy
            # Dalej zobraujeme aj jednotlive buttony
            self.data_from_device = BTN_ac.Put_values_to_Dates_list(self,self.link_addreses[self.links_list.currentRow()]) #Vlozime hodnoty do datumov
            shgui_op.Back_graph_button_show(self)
            return

    #Pre obsluhu listy s gateway
    def gateway_managment(self):
        if self.option_list.currentItem().recognizing_value == 42: #Pre pripad editovania
            Set_text.put_values_to_gateways_edit(self)
            return

    #Obsluha listy s datumami
    def Dates_managment(self):
        BTN_ac.Put_values_to_graph(self,self.data_from_device,self.Dates_list.currentRow()) #Zavolanie funkcie na zobrazenie dat
        return

    def kill_yourself(self):
        self.close()
        del self

class Links(QtGui.QWidget): #Trieda pre nastavenie dizajnu moznosti v devicoch

    def __init__(self, devaddr,parent = None):
        super(Links,self).__init__(parent)
        self.devaddr = devaddr
        self.BoxLayout = QtGui.QVBoxLayout()
        self.textLabel = QtGui.QLabel()
        self.BoxLayout.addWidget(self.textLabel)
        self.textLabel.setStyleSheet("color: black;"
                        "selection-color: white;"
                        "selection-background-color: red;")
        self.textLabel.setText(devaddr)
        self.textLabel.setFont(QtGui.QFont("Calibri", 8))
        self.setLayout(self.BoxLayout)

#Sluzi pre zadefinovanie grafiky prvkov v hlavnom strome
class Tree_options(QtGui.QTreeWidgetItem): #Trieda pre nastavenie dizajnu moznosti v devicoch
    def __init__(self, name,recognizing_value,parent ):
        super(Tree_options,self).__init__(parent)
        self.name = name
        self.recognizing_value = recognizing_value
        #self.BoxLayout = QtGui.QVBoxLayout()
        self.textLabel = QtGui.QLabel()
        self.treeWidget().setItemWidget(self,0,self.textLabel)
        self.textLabel.setStyleSheet("color: black;"
                        "selection-color: white;"
                        "selection-background-color: red;")
        self.textLabel.setText(self.name)
        self.textLabel.setFont(QtGui.QFont("Calibri", 12))
        self.setSizeHint(0,QtCore.QSize(30,37))
#Vytvorenie podprvkov ovladacieho panela
class Tree_subclass_options(QtGui.QTreeWidgetItem): #Trieda pre nastavenie dizajnu moznosti v devicoch
    def __init__(self, name,recognizing_value,parent ):
        super(Tree_subclass_options,self).__init__(parent)
        self.name = name
        self.recognizing_value= recognizing_value
        #self.BoxLayout = QtGui.QVBoxLayout()
        self.textLabel = QtGui.QLabel()
        self.treeWidget().setItemWidget(self,0,self.textLabel)
        #self.BoxLayout.addWidget(self.textLabel)
        self.textLabel.setStyleSheet("color: black;"
                        "selection-color: white;"
                        "selection-background-color: red;")
        self.textLabel.setText(self.name)
        self.textLabel.setFont(QtGui.QFont("Calibri", 10))
        self.setSizeHint(0,QtCore.QSize(30,28))
        #self.BoxLayout.addWidget(self.treeWidget())
        #self.connect(self.treeWidget, SIGNAL("itemSelectionChanged()"), self.clicked_on_optinion())
        #self.textLabel.connect(self.treeWidget, SIGNAL("itemSelectionChanged()"), self.clicked_on_optinion())
        #self.textLabel.mouseDoubleClickEvent = self.clicked_on_optinion
    def print_name(self):
        print self.recognizing_value

#Zadefinujeme vzhlad a funkcionalitu grafov
class Graph_widget(QtGui.QWidget):
    def __init__(self,data_x,data_y,parent = None):
        super(Graph_widget, self).__init__(parent)
        self.chart = Figure((6.0, 4.0), dpi=120) # Vytvorenie grafu
        self.plot_values = self.chart.add_subplot(111)
        #self.graph = self.plot_values.plot(data_x, data_y) # Vyplotovanie grafu
        self.graph = self.plot_values.plot([])[0]
        xfmt = DateFormatter('%H:%M:%S')
        self.plot_values.xaxis.set_major_formatter(xfmt)
        self.plot_values.yaxis.set_major_locator(MaxNLocator(integer=True))
        self.chart.autofmt_xdate() # Autoformat xovej osi
        # plot_values["layout"].update({"plot_bgcolor": "rgba(242, 241, 240, 255)"})
        self.chart.patch.set_facecolor('#F6F6F6')
        self.fig = FigureCanvas(self.chart)# Netusim co ale bez toho to plotovanie nefunguje
        mini_widget = QtGui.QVBoxLayout()
        mini_widget.addWidget(self.fig)
        self.setLayout(mini_widget)

    def actualize_values(self,data_x,data_y,axis_name):
        plt.cla()
        self.plot_values.clear()
        self.plot_values = self.chart.add_subplot(111)
        self.plot_values.plot(data_x,data_y)
        #self.plot_values.set_ylabel(axis_name, rotation = 0)
        self.plot_values.set_title(str(axis_name))
        xfmt = DateFormatter('%H:%M:%S')
        self.plot_values.xaxis.set_major_formatter(xfmt)
        #self.plot_values.yaxis.set_label_coords(-0.1,1.02)
        self.chart.autofmt_xdate()
        self.fig.draw()
        self.fig.adjustSize()
        return


