from textual.containers import Vertical, VerticalScroll, Horizontal, VerticalGroup, HorizontalGroup
from textual.widgets import Rule
from textual.app import ComposeResult
from textual import on, events

from ui.widgets.chunk_types.audio import AudioChunk, Chunk
from ui.widgets.play_head import PlayHead
from project import ChunkType


class TimelineRow(Horizontal):
    DEFAULT_CSS = """
    TimelineRow {
        background: $surface-lighten-1;
        max-height: 8;
        margin-bottom: 1;
    }
    """

class Timeline(Vertical):
    DEFAULT_CSS = """
    Timeline {
        #rows {
            hatch: "-" $surface-lighten-1;
            padding: 1 0;
            
            .beat-line {
                color: $surface-lighten-1;
                
            }
            
            .bar-line {
                color: $surface-lighten-2;
                
            }
            
            .beat-line, .bar-line {
                dock: left;
                margin: 0;
            }
            
            
        }
        
        PlayHead {
            layer: top;
        }
    }
    """
    
    def __init__(self):
        super().__init__(id="timeline")
        
        self.calc_bar_offset()
        
    def calc_bar_offset(self):
        self.bar_offset = self.app.project.bpm / 8 * (0.03333333333 / self.app.zoom_level)
        
    @on(events.MouseScrollDown)
    async def mouse_scroll_down(self, event: events.MouseScrollDown):
        self.app.zoom_level += (self.app.scroll_sensitivity_x / 200)
        self.calc_bar_offset()
        self.app.last_zoom_level = self.app.zoom_level
        self.handle_zoom()
        
    @on(events.MouseScrollUp)
    async def mouse_scroll_up(self, event: events.MouseScrollUp):
        self.app.zoom_level = max(self.app.zoom_level - self.app.scroll_sensitivity_x/200, 0.001)
        
        if self.app.zoom_level != self.app.last_zoom_level:
            self.app.last_zoom_level = self.app.zoom_level
            self.calc_bar_offset()
            self.handle_zoom()
            
    def handle_zoom(self):
        for chunk in self.query(Chunk):
            chunk.calculate_size()
            chunk.update_offset(self)
            
        for bar_line in self.query(Rule):
            if not isinstance(bar_line, PlayHead):
                bar_line.offset = (self.bar_offset * bar_line.index, 0)
                
                if self.app.zoom_level >= 0.09 and bar_line.has_class("beat-line"):
                    bar_line.display = False
                else:
                    bar_line.display = True
    
    def compose(self) -> ComposeResult:
        
        
        
        with VerticalScroll(id="rows"):
            
            
            
            for channel in self.app.project.channels:
                self.notify(str(channel))
                
                with TimelineRow():
                    for chunk in channel.chunks:
                        if chunk.chunk_type == ChunkType.CHUNK:
                            yield Chunk(chunk_name=chunk.name, bar_pos=chunk.position)
                        elif chunk.chunk_type == ChunkType.AUDIO:
                            yield AudioChunk(chunk.audio_data, chunk.sample_rate, chunk.name, chunk.position)

            for i in range(1, 17):
                bar = None
                if i % 4 == 0:
                    bar = Rule.vertical(classes="bar-line", line_style="double")
                else:
                    bar = Rule.vertical(classes="beat-line")
                    
                bar.offset = (self.bar_offset * i, 0)
                bar.index = i
                    
                yield bar
             
        
             
        yield PlayHead()