from textual.containers import VerticalGroup, Horizontal
from textual.app import ComposeResult
from textual.widgets import Checkbox, Input, Static

from textual_slider import Slider

from textual import on, events

from project import ProjectChannel


class Channel(VerticalGroup):
    DEFAULT_CSS = """
    Channel {
        height: 8;
        border: tall $surface-lighten-1;
        background: $surface-darken-1;
        padding: 0 1;
        margin-bottom: 1;
        
        #name {
            padding: 0 1;
            margin-bottom: 1;
        }
        
        Checkbox {
            margin-right: 1;
        }
        
        .channel-slider {
            height: 1;
            Static {
                width: 50%;
                margin-right: 1;
            }
            
            Slider {
                width: 45%;
                border: none;
                max-height: 1;
            }
            
        }
        
        #channel-buttons {
            margin-top: 1;
        }
    }
    """
    
    def __init__(self, channel_index: int, project_channel: ProjectChannel, channel_name: str = "", muted: bool = False, solo: bool = False, pan: float = 0, volume: float = 0):
        super().__init__()
        self.channel_index = channel_index
        self.project_channel = project_channel
        self.channel_name = channel_name
        self.muted = muted
        self.solo = solo
        self.pan = pan
        self.volume = volume
        print(self.channel_name)

    def on_checkbox_changed(self, event: Checkbox.Changed):
        if event.checkbox.id == "mute":
            self.muted = event.value
            
            self.app.query_one("#timeline").query_one()
        elif event.checkbox.id == "solo":
            self.solo = event.value
        
    def on_slider_changed(self, event: Slider.Changed):
        if event.slider.id == "volume":
            self.volume = round(event.value, 2)
            self.query_one("#volume-text").update(f"Volume ({self.volume}Db):")
        elif event.slider.id == "pan":
            self.pan = round(event.value)
            pan_text = self.query_one("#pan-text")
            
            if self.pan == 0:
                pan_text.update("Pan (center):")
            elif self.pan < 0:
                pan_text.update(f"Pan ({-self.pan}% left):")
            elif self.pan > 0:
                pan_text.update(f"Pan ({self.pan}% right):")
    
    def compose(self) -> ComposeResult:
        yield Input(placeholder="Channel Name", compact=True, id="name", value=self.channel_name)
        
        with Horizontal(classes="channel-slider"):
            yield Static("Pan (center):", id="pan-text")
            yield Slider(-100, 100, step=1, value=self.pan, id="pan")
        with Horizontal(classes="channel-slider"):
            yield Static("Volume (0Db):", id="volume-text")
            yield Slider(-15, 15, step=0.1, value=self.volume, id="volume")
        
        with Horizontal(id="channel-buttons"):
            yield Checkbox("Mute", compact=True, id="mute", tooltip="Mute this track", value=self.muted)
            yield Checkbox("Solo", compact=True, id="solo", tooltip="Hear only this track", value=self.solo)
            