from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
import mysql.connector
from job import job


class ConnectPage(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.cols = 2

        self.add_widget(Label(text='Name:'))
        self.name = TextInput(multiline=False)
        self.add_widget(self.name)

        self.add_widget(Label(text='Password:'))
        self.password = TextInput(multiline=False)
        self.add_widget(self.password)

        self.join = Button(text="Login")
        self.join.bind(on_press=self.join_button)
        self.add_widget(self.join)

        self.join = Button(text="Register")
        self.join.bind(on_press=self.register_button)
        self.add_widget(self.join)

    def join_button(self, instance):
        name = self.name.text
        password = self.password.text

        if name in names:
            if password == users_res[names.index(name)][2]:
                chat_app.screen_manager.current = 'Kawa'
            else:
                info = f"Podane hasło jest błędne"
                chat_app.info_page.update_info(info)
                chat_app.screen_manager.current = 'Info'
        else:
            info = f"Brak nazwy i hasła w takiej kombinacji"
            chat_app.info_page.update_info(info)
            chat_app.screen_manager.current = 'Info'

    def register_button(self, instance):
        name = self.name.text
        password = self.password.text
        if name in names:
            info = f"Name '{name}' already used"
            chat_app.info_page.update_info(info)
            chat_app.screen_manager.current = 'Info'
        else:
            cursor.execute(f"""
                INSERT INTO users (Name, Password)
                VALUES ('{name}', '{password}')
            """)
            db.commit()

            info = f"Added user '{name}' with password '{password}'"
            chat_app.info_page.update_info(info)
            chat_app.screen_manager.current = 'Info'


class InfoPage(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.cols = 1
        self.message = Label(halign="center", valign="middle", font_size=30)
        self.message = Label()
        self.message.bind(width=self.update_text_width)
        self.add_widget(self.message)

        self.join = Button(text="Back")
        self.join.bind(on_press=self.back)
        self.add_widget(self.join)

    def back(self, instance):
        chat_app.screen_manager.current = 'Connect'

    def update_info(self, message):
        self.message.text = message

    def update_text_width(self, *_):
        self.message.text_size = (self.message.width * 0.9, None)

class InfoPage2(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.cols = 1
        self.message = Label(halign="center", valign="middle", font_size=30)
        self.message = Label()
        self.message.bind(width=self.update_text_width)
        self.add_widget(self.message)

        self.join = Button(text="Back")
        self.join.bind(on_press=self.back)
        self.add_widget(self.join)

    def back(self, instance):
        chat_app.screen_manager.current = 'Kawa'

    def update_info(self, message):
        self.message.text = message

    def update_text_width(self, *_):
        self.message.text_size = (self.message.width * 0.9, None)


def result():
    info = job('1')
    chat_app.info_page.update_info(info)
    chat_app.screen_manager.current = 'Info2'


def job_to_be_done(instance):
    info = 'Przetwarzam zdjęcia'
    chat_app.info_page.update_info(info)
    chat_app.screen_manager.current = 'Info'
    result()


class Kawa(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1

        self.add_widget(Label())
        self.add_widget(Label())
        self.join = Button(text="Zdjęcie")
        self.join.bind(on_press=job_to_be_done)
        self.add_widget(self.join)


class WTB(App):
    def build(self):
        self.screen_manager = ScreenManager()

        self.connect_page = ConnectPage()
        screen = Screen(name='Connect')
        screen.add_widget(self.connect_page)
        self.screen_manager.add_widget(screen)

        self.info_page = InfoPage()
        screen = Screen(name='Info')
        screen.add_widget(self.info_page)
        self.screen_manager.add_widget(screen)

        self.info_page = InfoPage2()
        screen = Screen(name='Info2')
        screen.add_widget(self.info_page)
        self.screen_manager.add_widget(screen)

        self.kawa_page = Kawa()
        screen = Screen(name='Kawa')
        screen.add_widget(self.kawa_page)
        self.screen_manager.add_widget(screen)

        return self.screen_manager

    def back(self):
        chat_app.screen_manager.current = 'Connect'


if __name__ == "__main__":
    db = mysql.connector.connect(host="localhost", user="root", database="wtb_db")
    cursor = db.cursor()
    cursor.execute('SELECT * FROM users')

    users_res = cursor.fetchall()
    names = []
    for res in users_res:
        names.append(res[1])

    chat_app = WTB()
    chat_app.run()
