"""

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 sys
import REST_api as curl
import Second_page as SP

class Login_page_layout(QtGui.QWidget):
    #Inicializovanie vsetkych prvkov na uvodnej strane
    def __init__(self):
        super(Login_page_layout,self).__init__()
        self.setGeometry(300,300,700,500)
        self.setFixedSize(self.size())
        self.setWindowTitle('Login Page')
        self.Login_input_init()
        self.Password_input_init()
        self.Login_button_init()
        self.Wrong_inputs_init()
        self.Server_connection_fail_init()

    def Login_input_init(self): #Vstup pre meno
        self.Login_input = QtGui.QLineEdit(self)
        self.Login_input.move(250, 240)
        self.Login_input.setFixedSize(200, 30)
        self.Login_input.setStyleSheet("padding-left: 5px") #Offset zlava
        self.Login_input.setPlaceholderText("Login") #Zatmavnenie textu a napis ked tam nic nieje
        self.Login_input.setFont(QtGui.QFont("Arial",10)) #Styl textu
        self.Login_input.setCursorPosition(40)

    def Password_input_init(self): #Vstup pre heslo
        self.Password_input = QtGui.QLineEdit(self)
        self.Password_input.setFixedSize(200, 30)
        self.Password_input.move(250, 280) #Pozicia voci systemu
        self.Password_input.setStyleSheet("padding-left: 5px")
        self.Password_input.setPlaceholderText("Password")
        self.Password_input.setFont(QtGui.QFont("Arial", 10))
        self.Password_input.setEchoMode(QtGui.QLineEdit.Password) # Aby nam to dalo bodky pre heslo

    def Login_button_init(self): #Button ktory bude sluzit ako prihlasenie sa
        self.Login_button = QtGui.QPushButton(self)
        self.Login_button.setText("Login")
        self.Login_button.setFixedSize(200,30)
        self.Login_button.move(250,320)

    def Wrong_inputs_init(self): #Label v pripade zlych vstupnych parametrov
        self.wrong_inputs = QtGui.QLabel('Zadali ste nespravne prihlasovacie udaje',self)
        self.wrong_inputs.move(233,220)
        self.wrong_inputs.setFont(QtGui.QFont("Arial", 10))
        self.wrong_inputs.hide()

    def Server_connection_fail_init(self): #Label v pripade zlyhania prihlasenia sa naserver
        self.Sever_connection_fail = QtGui.QLabel('Nepodarilo sa nadviazat spojenie so serverom',self)
        self.Sever_connection_fail.move(218,220)
        self.Sever_connection_fail.setFont(QtGui.QFont("Arial", 10))
        self.Sever_connection_fail.hide()


    def runapp(self):
        self.show()

class Login_page_functions():

    def __init__(self):
        self.Run()

    def Run(self): #Vykona sa pri starte
        self.app = QtGui.QApplication(sys.argv)
        self.Gui = Login_page_layout()
        self.functions = Gui_functions() #Funkcie musime zadefinovat pred koncom inak nam to padne....  Teda nepadne iba sa neinicializuju
        self.buttons_actions()
        self.Gui.runapp()
        sys.exit(self.app.exec_())


    def buttons_actions(self): #Funkcia pre tlacitko login
        self.Gui.Login_button.clicked.connect(lambda :self.functions.login_in_server(self.Gui)) # Lambda je tam dana kvoli tomu,
        #Aby to bolo intepretovane ako callback bez parametra


class Gui_functions():

    def login_in_server(self,basic_Gui):
        self.Gui = basic_Gui
        name = basic_Gui.Login_input.text()
        password = basic_Gui.Password_input.text()
        status = curl.user_verification(password,name) #Overujeme prihlasenie
        #Kazdy status nieco znamena, 1 uspesne prihlasenie, 2 chybne meno heslo, 3 Chyba pri nadviazani komunikacie so serverom
        if status == 1:
            basic_Gui.wrong_inputs.hide()
            basic_Gui.Sever_connection_fail.hide()
            self.Next_page = SP.First_page_layout(name,password)
            self.Next_page.runapp()
            self.Next_page.Logout_button.mousePressEvent = self.logout_action
            basic_Gui.hide()
        elif status == 2: #Pre pripad ze prihlasovanie nebolo uspesne
            basic_Gui.Sever_connection_fail.hide()
            basic_Gui.wrong_inputs.show()
        else: #Nepodarilo sa nadviazat spojenie so serverom
            basic_Gui.Sever_connection_fail.show()
            basic_Gui.wrong_inputs.hide()

    #Pre tlacitko z nasledujucej strany, sluzi ako logout button.
    def logout_action(self,event):
        self.Next_page.kill_yourself()
        self.Gui.show()
        return

