import logging.config

import yaml
from fasthtml import serve

from myfasthtml.controls.CommandsDebugger import CommandsDebugger
from myfasthtml.controls.FileUpload import FileUpload
from myfasthtml.controls.InstancesDebugger import InstancesDebugger
from myfasthtml.controls.Keyboard import Keyboard
from myfasthtml.controls.Layout import Layout
from myfasthtml.controls.TabsManager import TabsManager
from myfasthtml.controls.helpers import Ids, mk
from myfasthtml.core.instances import UniqueInstance
from myfasthtml.icons.carbon import volume_object_storage
from myfasthtml.icons.fluent_p3 import folder_open20_regular
from myfasthtml.myfastapp import create_app

with open('logging.yaml', 'r') as f:
  config = yaml.safe_load(f)

# At the top of your script or module
logging.config.dictConfig(config)

app, rt = create_app(protect_routes=True,
                     mount_auth_app=True,
                     pico=False,
                     vis=True,
                     title="MyFastHtml",
                     live=True,
                     base_url="http://localhost:5003")


@rt("/")
def index(session):
  session_instance = UniqueInstance(session=session, _id=Ids.UserSession)
  layout = Layout(session_instance, "Testing Layout")
  layout.set_footer("Goodbye World")
  
  tabs_manager = TabsManager(layout, _id=f"{TabsManager.compute_prefix()}-main")
  btn_show_right_drawer = mk.button("show",
                                    command=layout.commands.toggle_drawer("right"),
                                    id="btn_show_right_drawer_id")
  
  instances_debugger = InstancesDebugger(layout)
  btn_show_instances_debugger = mk.label("Instances",
                                         icon=volume_object_storage,
                                         command=tabs_manager.commands.add_tab("Instances", instances_debugger),
                                         id=instances_debugger.get_id())
  
  commands_debugger = CommandsDebugger(layout)
  btn_show_commands_debugger = mk.label("Commands",
                                        icon=None,
                                        command=tabs_manager.commands.add_tab("Commands", commands_debugger),
                                        id=commands_debugger.get_id())
  
  btn_file_upload = mk.label("Upload",
                             icon=folder_open20_regular,
                             command=tabs_manager.commands.add_tab("File Open", FileUpload(layout)),
                             id="file_upload_id")
  
  layout.header_left.add(tabs_manager.add_tab_btn())
  layout.header_right.add(btn_show_right_drawer)
  layout.left_drawer.add(btn_show_instances_debugger, "Debugger")
  layout.left_drawer.add(btn_show_commands_debugger, "Debugger")
  layout.left_drawer.add(btn_file_upload, "Test")
  layout.set_main(tabs_manager)
  keyboard = Keyboard(layout).add("ctrl+o", tabs_manager.commands.add_tab("File Open", FileUpload(layout)))
  keyboard.add("ctrl+n", tabs_manager.commands.add_tab("File Open", FileUpload(layout)))
  return layout, keyboard


if __name__ == "__main__":
  # debug_routes(app)
  serve(port=5003)
