from nicegui import ui
from database import AppConfig
from core.state import state

def show():
    # Carrega configuração inicial
    semi_auto_initial = AppConfig.get_val('semi_auto', 'false') == 'true'

    with ui.grid(columns=2).classes('w-full gap-4'):
        
        # --- COLUNA DA ESQUERDA: CONTROLES E LOGS ---
        with ui.column().classes('w-full gap-4'):
            
            # Painel de Controle
            with ui.card().classes('w-full p-4 border-l-4 border-indigo-500'):
                ui.label('🎛️ Painel de Controle').classes('text-xl font-bold mb-4 text-gray-700')
                
                # Status inicial do Switch
                is_running = state.watcher.is_running if state.watcher else False
                
                def toggle_watcher(e):
                    if state.watcher:
                        state.watcher.is_running = e.value
                        state.log(f"Comando: {'INICIAR' if e.value else 'PAUSAR'}")

                switch_run = ui.switch('Monitoramento Ativo', value=is_running, on_change=toggle_watcher).props('color=green size=lg')

                def toggle_semi_auto(e):
                    AppConfig.set_val('semi_auto', str(e.value).lower())
                    state.log(f"⚠️ Modo Semi-Auto: {e.value}")

                switch_auto = ui.switch('Modo Semi-Automático', value=semi_auto_initial, on_change=toggle_semi_auto).props('color=amber size=lg')
                
                def cancel_task():
                    if state.watcher: 
                        state.watcher.abort_current_task()
                        ui.notify('Cancelando tarefa atual...', type='warning')

                btn_cancel = ui.button('CANCELAR ATUAL', on_click=cancel_task, icon='cancel').props('color=red').classes('w-full mt-4')
                btn_cancel.set_visibility(False)

            # TERMINAL DE LOGS (Versão Otimizada)
            with ui.card().classes('w-full h-80 bg-black p-2 flex flex-col'):
                ui.label('>_ Logs do Sistema').classes('text-gray-500 mb-1 border-b border-gray-800 w-full text-xs')
                
                # O componente ui.log é específico para mensagens de terminal
                log_view = ui.log(max_lines=100).classes('w-full flex-grow text-green-400 font-mono text-[11px]')
                # Variável de controle para não repetir logs já mostrados
                log_view.last_index = 0

        # --- COLUNA DA DIREITA: LISTA DE TAREFAS ---
        with ui.card().classes('w-full h-[80vh] bg-gray-50 flex flex-col p-0'):
            with ui.row().classes('w-full p-4 bg-white border-b items-center justify-between'):
                ui.label('📋 Fila de Processamento').classes('text-lg font-bold text-gray-700')
                lbl_status_top = ui.label('Ocioso').classes('text-sm text-gray-400')

            # Container onde a lista será renderizada
            tasks_container = ui.column().classes('w-full p-2 gap-2 overflow-y-auto flex-grow')

    # --- RENDERIZADOR REATIVO DA LISTA ---
    @ui.refreshable
    def render_tasks():
        # Proteção contra erro de dicionário alterado durante o loop (causa do sumiço da lista)
        try:
            current_tasks = list(state.tasks.items())
        except Exception:
            return

        if not current_tasks:
            ui.label('Nenhuma atividade recente.').classes('text-gray-400 italic p-4')
            return

        for fname, data in reversed(current_tasks):
            status = data.get('status', 'pending')
            global_pct = data.get('progress', 0)
            file_pct = data.get('file_progress', 0)
            speed = data.get('speed', '')
            label = data.get('label', 'Tarefa')
            
            icon = 'circle'; color = 'grey'; spin = False
            bg_color = 'bg-white'
            
            if status == 'pending': icon = 'hourglass_empty'
            elif status == 'running': 
                icon = 'sync'; color = 'blue'; spin = True
                bg_color = 'bg-blue-50 border-blue-200 border'
            elif status == 'warning':
                icon = 'warning'; color = 'orange'
                bg_color = 'bg-orange-50 border-orange-200 border'
            elif status == 'done': icon = 'check_circle'; color = 'green'
            elif status == 'error': icon = 'error'; color = 'red'
            elif status == 'skipped': icon = 'block'; color = 'red'

            with ui.card().classes(f'w-full p-3 {bg_color} flex-row items-start gap-3 shadow-sm'):
                if spin: ui.spinner(size='sm').classes('text-blue-500 mt-1')
                else: ui.icon(icon, color=color, size='sm').classes('mt-1')
                
                with ui.column().classes('flex-grow gap-1 w-full'):
                    ui.label(label).classes('font-bold text-sm text-gray-800 break-all')
                    ui.label(fname).classes('text-[10px] text-gray-500 break-all mb-1')
                    
                    if status == 'running':
                        # Barra Total
                        with ui.row().classes('w-full items-center gap-2'):
                            ui.label('Total').classes('text-[10px] font-bold text-gray-400 w-8')
                            ui.linear_progress(value=global_pct/100).classes('h-1.5 rounded flex-grow').props('color=blue-4')
                            ui.label(f"{int(global_pct)}%").classes('text-[10px] font-bold text-blue-400 w-7 text-right')

                        # Barra do Arquivo (se houver progresso)
                        if file_pct > 0 or (speed and speed != "0x"):
                            with ui.row().classes('w-full items-center gap-2'):
                                ui.label('File').classes('text-[10px] font-bold text-gray-400 w-8')
                                ui.linear_progress(value=file_pct/100).classes('h-2 rounded flex-grow').props('color=green')
                                with ui.row().classes('items-center gap-1'):
                                    ui.label(f"{int(file_pct)}%").classes('text-[10px] font-bold text-green-600')
                                    if speed:
                                        ui.label(speed).classes('text-[10px] bg-green-100 text-green-800 px-1 rounded font-mono')

    # Inicializa a lista dentro do container
    with tasks_container:
        render_tasks()

    # --- LOOP DE ATUALIZAÇÃO (1 segundo) ---
    def update_ui():
        # 1. ATUALIZAR LOGS (Envia apenas as linhas novas para o ui.log)
        try:
            all_logs = state.get_logs()
            if len(all_logs) > log_view.last_index:
                # Pega apenas as mensagens que ainda não foram enviadas para a tela
                new_messages = all_logs[log_view.last_index:]
                for msg in new_messages:
                    log_view.push(str(msg))
                log_view.last_index = len(all_logs)
        except Exception:
            pass

        # 2. ATUALIZAR LISTA DE TAREFAS
        render_tasks.refresh()

        # 3. ATUALIZAR STATUS GLOBAL DO SERVIÇO
        if state.watcher and state.watcher.is_running:
            lbl_status_top.set_text("Serviço Rodando")
            lbl_status_top.classes(remove='text-red-400', add='text-green-500')
            switch_run.value = True
        else:
            lbl_status_top.set_text("Serviço Pausado")
            lbl_status_top.classes(remove='text-green-500', add='text-red-400')
            switch_run.value = False

        # 4. BOTÃO CANCELAR
        btn_cancel.set_visibility(bool(state.current_file))

    # Inicia o timer de atualização
    ui.timer(1.0, update_ui)