from textual.app import App, ComposeResult
from textual.widgets import Footer, Tab, Tabs, Header
from textual import on, events

from ui.widgets.sidebar import Sidebar
from ui.widgets.timeline import Timeline, TimelineRow
from ui.widgets.project_settings import ProjectSettings
from ui.widgets.channel import Channel

from project import ProjectChannel


class AppUI(App):    
    CSS_PATH = "../assets/style.tcss"
    
    def __init__(self, project):
        super().__init__()
        self.zoom_level = 0.05
        self.last_zoom_level = self.zoom_level
        self.project = project
        
    @on(events.Key)
    async def key_pressed(self, event: events.Key):
        if event.key == "space":
            timeline = self.query_one(Timeline)
            if not timeline.song_player.paused:
                timeline.song_player.pause()
            else:
                timeline.song_player.play_project(self.app.project)
    
    def create_channel(self, name: str):
        self.query_one("#channels").mount(Channel(
            len(self.project.channels),
            name,
        ), before=-1)
        self.query_one("#rows").mount(TimelineRow())
        
        self.project.channels.append(ProjectChannel(
            name
        ))
    
    def compose(self) -> ComposeResult:
        with Tabs(id="top-menu"):
            yield Tab("File")
            yield Tab("Edit")
            yield Tab("View")
            yield Tab("Help")
        yield Sidebar()
        yield Timeline()
        yield ProjectSettings()
        yield Footer()