from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import json, glob, random
from datetime import datetime
from pathlib import Path
from hoverable import HoverBehavior
from kivy.uix.image import Image
from kivy.uix.behaviors  import ButtonBehavior

Builder.load_file('design.kv')

class LoginScreen(Screen):
    def sign_up(self):
        self.manager.current = "signup_screen"
    def forgot_password(self):
        self.manager.current = "forgot_password_screen"
    def login(self, username, password):
        with open("users.json") as file:
            users = json.load(file)
        if username in users and users[username]['password'] == password:
            self.manager.current = "login_screen_success"
        else:
            self.ids.login_wrong.text = "Wrong username or password"

class RootWidget(ScreenManager):
    pass

class SignUpScreen(Screen):
    def add_user(self, username, password):
        with open("users.json") as file:
            users = json.load(file)
        users[username] = {'username':username, 'password':password,
        'created':datetime.now().strftime("%Y-%m-%d %H-%M-%S")}
        with open("users.json", 'w') as file:
            json.dump(users, file)
        self.manager.current = "signup_screen_success"

class SignUpScreenSuccess(Screen):
    def go_to_login(self):
        self.manager.transition.direction = "right"
        self.manager.current = "login_screen"

class LoginScreenSuccess(Screen):
    def log_out(self):
        self.manager.transition.direction = "right"
        self.manager.current = "login_screen"
    def get_quote(self,feeling):
        feeling = feeling.lower()
        available_feelings = glob.glob("quotes/*txt")
        available_feelings = [Path(filename).stem for filename in available_feelings]
        if feeling in available_feelings:
            with open(f"quotes/{feeling}.txt", encoding="utf8") as file:
                quotes = file.readlines()
            self.ids.quote.text = random.choice(quotes)
        else:
            self.ids.quote.text = "Try another feeling"

class ForgotPasswordScreen(Screen):
    def check_username(self, username):
        global uname_for_password
        uname_for_password = username
        with open("users.json") as file:
            users = json.load(file)
        if username in users:
            self.manager.current = "enter_new_password_screen"
        else:
            self.ids.confirm_username.text = "Incorrect Username! Enter Again."

class EnterNewPasswordScreen(Screen):
    def save_password(self, new_password, confirm_new_password):
        global uname_for_password
        if(new_password == confirm_new_password):
            with open("users.json") as file:
                users = json.load(file)
            users[uname_for_password]['password'] = new_password
            with open("users.json", 'w') as file:
                json.dump(users, file)
            self.ids.password_check.text = "Password saved"
        else:
            self.ids.password_check.text = "Password does not match, Enter again!"
    def return_to_login(self):
        self.manager.current = "login_screen"

class ImageButton(ButtonBehavior, HoverBehavior, Image):
    pass

class FeelGoodApp(App):
    def build(self):
        return RootWidget()

if __name__ == "__main__":
    FeelGoodApp().run()