'''
    Main Screen of the client interface
'''

from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.lang import Builder

from kivy.properties import BooleanProperty, StringProperty

from apps.login_client import LoginClient
from apps.apps_list import AppsList

# accessing to DesktopClient.open_screen from StartScreen is a bit convoluted
# and depends on where StartScreen is included inside DesktopClient
# MEMO : any better solution ?
# currently, access through root.manager.parent.parent.open_screen()

Builder.load_string('''
<StartScreen>:

    BoxLayout:
        orientation: "vertical"
        size: root.size
        spacing: 20
        padding: 20
        
        Label:
            text: "Home"
        Button:
            text: "Main Chat"
            on_release:
                root.manager.parent.parent.open_screen("/chat/global_main")

        Button:
            text: "Dev Chat"
            on_release:
                root.manager.parent.parent.open_screen("/chat/global_dev")

        Button:
            text: "Wiki"
            on_release:
                root.manager.parent.parent.open_screen("/wiki/home")
        
        Button:
            text: "Tic Tac Toe"
            on_release:
                root.manager.parent.parent.open_screen("/game/tic_tac_toe")
        
        Button:
            text: "Connect Four"
            on_release:
                root.manager.parent.parent.open_screen("/game/connect_four")
        
        Button:
            text: "Reversi"
            on_release:
                root.manager.parent.parent.open_screen("/game/reversi")
''')

class StartScreen(Screen):
    pass


# TODO: we are using an import directly in KV code.
# Another way of initializing username_label would be better.

Builder.load_string('''
<DesktopClient>:
    BoxLayout:
        orientation: "vertical"
        size: root.size
        
        BoxLayout:
            orientation: "horizontal"
            size_hint_y: .1
            
            Button:
                id: home_button
                text: "Home"
                size_hint_x: .3
                padding_x: 5
                disabled: not root.is_logged
                opacity: 1 if root.is_logged and screen_manager.current != "startscreen" else 0
                on_release:
                    screen_manager.current = "startscreen"
            Label:
                id: nothing_label
            Label:
                id: username_label
                text: root.username
                size_hint_x: .3
                pos_hint: {'right': 0.9}
                opacity: 1 if root.is_logged else 0
            
        ScreenManager:
            id: screen_manager
            
            LoginClient:
                name: "loginscreen"
                manager: screen_manager
                on_login_finished:
                    self.manager.current = "startscreen"
                    root.is_logged = True
                    from core.main_user import MainUser
                    root.username = MainUser.username
            
            StartScreen:
                name: "startscreen"
                manager: screen_manager
''')

class DesktopClient(Screen):
    
    is_logged = BooleanProperty(False)
    username = StringProperty("")

    def __init__(self, **kwargs):
        self.chat_client = None
        self.wiki_client = None
        self.turnbasedgame_client = None
        super().__init__(**kwargs)

    # test of screen dynamic creation
    def open_screen(self, address):
        if address.startswith("/chat/"):
            if self.chat_client is None:
                self.chat_client = AppsList.get_client_app(address)
                self.chat_client.name = "devchatscreen"
                self.ids["screen_manager"].add_widget(self.chat_client)
            
            self.chat_client.chat_address = address
            self.ids["screen_manager"].current = "devchatscreen"
        elif address.startswith("/wiki/"):
            if self.wiki_client is None:
                self.wiki_client = AppsList.get_client_app(address)
                self.wiki_client.name = "wikiscreen"
                self.ids["screen_manager"].add_widget(self.wiki_client)
            
            self.wiki_client.target_address = address
            self.ids["screen_manager"].current = "wikiscreen"
        elif address.startswith("/game/"):
            if self.turnbasedgame_client is None:
                self.turnbasedgame_client = AppsList.get_client_app(address)
                self.turnbasedgame_client.name = "turnbasedgamescreen"
                self.ids["screen_manager"].add_widget(self.turnbasedgame_client)
            
            self.turnbasedgame_client.target_address = address
            self.ids["screen_manager"].current = "turnbasedgamescreen"
