import kivy
kivy.require('1.11.1')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
#from database import DataBase

class CreateAccountWindow(Screen):
    namee = ObjectProperty(None)
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def submit(self):
        if self.namee.text != "" and self.email.text != "" and self.email.text.count("@") == 1 and self.email.text.count(".") > 0:
            if self.password != "":
                db.add_user(self.email.text, self.password.text, self.namee.text)

                self.reset()

                sm.current = "login"
            else:
                invalidForm()
        else:
            invalidForm()

    def login(self):
        self.reset()
        sm.current = "login"

    def reset(self):
        self.namee.text = ""
        self.email.text = ""
        self.password.text = ""


class LoginWindow(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def loginBtn(self):
        if db.validate(self.email.text, self.password.text):
            MainWindow.current = self.email.text
            self.reset()
            sm.current = "main"
        else:
            invalidLogin()

    def createBtn(self):
        self.reset()
        sm.current = "create"

    def reset(self):
        self.email.text = ""
        self.password.text = ""

class MainWindow(Screen):
    n = ObjectProperty(None)
    created = ObjectProperty(None)
    email = ObjectProperty(None)
    current = ""

    def logOut(self):
        sm.current = "login"

    def on_enter(self, *args):
        password, name, created = db.get_user(self.current)
        self.n.text = "Account Name: " + name
        self.email.text = "Email: " + self.current
        self.created.text = "Created On: " + created

class WindowManager(ScreenManager):
    pass

def invalidLogin():
    pop = Popup(title = 'Invalid Login',
                content = Label(text='Invalid username or password.'),
                size_hint = (None, None), size = (400,400))

    pop.open()

def invalidForm():
    pop = Popup(title='Invalid Form',
                  content=Label(text='Please fill in all inputs with valid information.'),
                  size_hint=(None, None), size=(400, 400))

    pop.open()

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

        #self.inside = GridLayout()
        #self.inside.cols = 2

        #self.inside.add_widget(Label(text="Email: "))
        #self.email = TextInput(multiline=False)
        #self.inside.add_widget(self.email)

        #self.inside.add_widget(Label(text="Password: "))
        #self.pwd = TextInput(multiline=False)
        #self.inside.add_widget(self.pwd)

        #self.add_widget(self.inside)

        #self.submit = Button(text = "Login", font_size = 40)
        #self.sub = Button(text = "Click to sign up", font_size = 30)
        #self.submit.bind(on_press = self.pressed)
        #self.sub.bind(on_press = self.press)
        #self.add_widget(self.submit)
        #self.add_widget(self.sub)


    #def pressed(self, instance):
        #email = self.email.text
        #pwd = self.pwd.text

        #print("Email: ", email, "Password: ", pwd)
        #self.email.text = ""
        #self.pwd.text = ""

kv = Builder.load_file("main.kv")
sm = WindowManager()
#db = DataBase("users.txt")

screens = [LoginWindow(name="login"), CreateAccountWindow(name="create"), MainWindow(name="main")]
for screen in screens:
    sm.add_widget(screen)

    sm.current = "login"

class MyMainApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
        MyMainApp().run()
