from core.network_interface import NetworkInterface
from widgets.custom_labels import ScrollableLabel
from utils.utc_date import UtcDate

from kivy.uix.screenmanager import Screen, NoTransition
from kivy.properties import BooleanProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''
<WikiClient>:
    BoxLayout:
        orientation: "vertical"
        size: root.size
        
        BoxLayout:
            orientation: "horizontal"
            size_hint_y: .1
            
            Button:
                id: article_button
                text: "Article"
                disabled: root.is_edit or not root.is_showing_history
                on_release:
                    screen_manager.current = "display_screen"
                    root.is_showing_history = False
                    root.show_article()

            Button:
                id: edit_button
                text: "Edit"
                disabled: root.is_edit or root.is_showing_history
                on_release: 
                    screen_manager.current = "edit_screen"
                    root.is_edit = True
                    root.start_edit()
            
            Button:
                id: cancel_button
                text: "Cancel"
                disabled: not root.is_edit
                on_release:
                    screen_manager.current = "display_screen"
                    root.is_edit = False
                    root.cancel_edit()

            Button:
                id: save_button
                text: "Save"
                disabled: not root.is_edit
                on_release:
                    screen_manager.current = "display_screen"
                    root.is_edit = False
                    root.save_edit()
        
            Button:
                id: history_button
                text: "History"
                disabled: root.is_edit or root.is_showing_history
                on_release:
                    root.is_showing_history = True
                    root.show_history()

        ScreenManager:
            id: screen_manager
            Screen:
                id: display_screen
                name: "display_screen"
                ScrollableLabel:
                    id:label
                    text: ""
                    on_link_clicked:
                        root.target_address = "/wiki/" + args[1]
                        root.read_address(root.target_address)
            Screen:
                id: edit_screen
                name: "edit_screen"
                TextInput:
                    multiline: True
                    id:textbox
''')

class WikiClient(Screen):
    
    # kivy string property indicating the target network address
    target_address = StringProperty()
    
    is_edit = BooleanProperty(False)
    is_showing_history = BooleanProperty(False)

    # called by Kivy when the screen is entered (displayed)
    def on_enter(self):
        # activate for utf8 input(japanese characters), display is ok but input is a bit buggy
        utf8_font_path = "NotoSansCJK-Regular.ttc"
        self.ids["textbox"].font_name = utf8_font_path
        self.ids["label"].font_name = utf8_font_path

        self.ids["textbox"].text_validate_unfocus = False
        self.ids["screen_manager"].transition = NoTransition()
        
        self.clear_text()

        NetworkInterface.register(client =self, network_path = "/wiki/")
        self.read_address(self.target_address)
    
    def on_leave(self):
        # remove current text (to avoid flickering when reentering)
        self.clear_text()
        
        # reset default buttons set
        self.is_edit = False
        self.is_showing_history = False
        self.ids["article_button"].disabled = True
        self.ids["edit_button"].disabled = False
        self.ids["cancel_button"].disabled = True
        self.ids["save_button"].disabled = True
        self.ids["history_button"].disabled = False
        self.ids["screen_manager"].current = "display_screen"

    def receive_message(self, message):
        if message.command == "READ_RESULT":
            self.current_content = message.content
            self.update_text(self.current_content)
        elif message.command == "READ_LOG_RESULT":
            result_str = ""
            for item in message.content[::-1]:
                idx, timestamp, username, comment = item
                result_str += UtcDate.convert_utc_to_local(timestamp) + " " + username + " " + comment + "\n"
            self.update_text(result_str)
        elif message.command == "WRITE_DONE":
            # read the address again after writing
            self.read_address(self.target_address)
        elif message.command == "NOT_EXISTING":
            self.current_content = ""
            self.update_text(self.target_address + " not existing yet.")
    
    def read_address(self, read_target_address):
        NetworkInterface.send(read_target_address, "READ", "")
    
    def read_address_changelog(self, read_target_address):
        NetworkInterface.send(read_target_address, "READ_LOG", "")
    
    def clear_text(self):
        self.current_content = ""
        self.ids["label"].set_wiki_text("", title = "", text_color= "000000")

    def update_text(self, msg):
        # Page title is the part after /wiki/ in the page address
        self.ids["label"].set_wiki_text(msg, title = self.target_address[6:], text_color= "000000")
    
    def show_article(self):
        self.read_address(self.target_address)

    def show_history(self):
        self.read_address_changelog(self.target_address)

    def start_edit(self):
        self.ids["textbox"].focus = True
        self.ids["textbox"].text = self.current_content
    
    def save_edit(self):
        page_content = self.ids["textbox"].text
        
        # TODO : real comment from the user in the interface
        change_comment = ""
        NetworkInterface.send(self.target_address, "WRITE", [page_content, change_comment])
        
        # remove the current content of the label to show that we are waiting the server response
        self.ids["label"].text = ""
    
    def cancel_edit(self):
        #TODO : confirmation popup
        self.show_article()
