use clap::Args;
use crossterm::{
    event::{read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers, MouseEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    widgets::StatefulWidget,
    Terminal,
};
use hefesto_widgets::{FileBrowserPopup, FileBrowserState, PopupSize};

use crate::{keybinds, style};
use std::path::PathBuf;

const FILE_GUIDE: &str = include_str!("../FILE_GUIDE.md");

#[derive(Args)]
pub struct FileArgs {
    /// Starting directory (default: current dir)
    pub path: Option<PathBuf>,

    /// Select directories mode (Enter = cwd, Space = select dir)
    #[arg(short, long)]
    pub dirs: bool,

    /// Popup width (0 = auto)
    #[arg(short = 'W', long, default_value = "0")]
    pub width: u16,

    /// Enable multi-select (space to toggle, enter to confirm)
    #[arg(short, long)]
    pub multi: bool,

    /// Max selected items
    #[arg(short = 'M', long)]
    pub max: Option<usize>,

    #[arg(long)]
    pub guide: bool,
}

pub fn run(args: FileArgs) {
    if args.guide {
        println!("{}", FILE_GUIDE);
        return;
    }

    crate::tty::ensure_terminal_stdin();
    let mut tty: Box<dyn std::io::Write> = match std::fs::OpenOptions::new().write(true).open("/dev/tty") {
        Ok(f) => Box::new(f),
        Err(_) => Box::new(std::io::stdout()),
    };
    if enable_raw_mode().is_err() || execute!(tty, EnterAlternateScreen, EnableMouseCapture).is_err() {
        eprintln!("{} file: el terminal no es interactivo", crate::BIN_NAME);
        std::process::exit(1);
    }
    let mut terminal = Terminal::new(CrosstermBackend::new(tty)).unwrap();
    crate::tty::with_terminal_stdout(|| terminal.clear()).unwrap();
    crate::tty::with_terminal_stdout(|| terminal.hide_cursor()).unwrap();

    let start = args.path.clone().unwrap_or_else(|| std::env::current_dir().unwrap());
    let mut state = FileBrowserState {
        entries: vec![],
        items: vec![],
        cwd: PathBuf::new(),
        choose_popup_state: hefesto_widgets::ChoosePopupState::default(),
        show_hidden: false,
    };
    state.navigate_to(&start);

    let mut result: Option<Vec<PathBuf>> = None;
    let mut pending_g: bool = false;

    while result.is_none() {
        let pw = if args.width > 0 { args.width } else { 44 };

        let mut popup = FileBrowserPopup::new()
            .dir_style(ratatui::style::Style::new().fg(style::ACCENT))
            .file_style(ratatui::style::Style::new().fg(style::TEXT))
            .width(PopupSize::Fixed(pw));
        if let Some(max) = args.max {
            popup = popup.max_selected(max);
        }

        terminal
            .draw(|frame| {
                StatefulWidget::render(popup, frame.area(), frame.buffer_mut(), &mut state);
            })
            .unwrap();

        match read().unwrap() {
            Event::Key(key) => {
                if key.code == keybinds::EMERGENCY && key.modifiers == KeyModifiers::CONTROL {
                    break;
                }
                match key.code {
                    keybinds::UP | keybinds::UP_ALT => { state.previous(); pending_g = false; },
                    keybinds::DOWN | keybinds::DOWN_ALT => { state.next(); pending_g = false; },
                    keybinds::TOGGLE_MULTI
                        if (args.multi || args.dirs) && !state.show_filter() =>
                    {
                        if args.multi {
                            if let Some(entry) = state.selected_entry() {
                                if args.dirs && entry.is_dir {
                                    state.toggle_cursor();
                                } else if !args.dirs && !entry.is_dir {
                                    state.toggle_cursor();
                                }
                            }
                        } else if args.dirs {
                            if let Some(entry) = state.selected_entry() {
                                if entry.is_dir {
                                    result = Some(vec![entry.path.clone()]);
                                }
                            }
                        }
                        pending_g = false;
                    }
                    keybinds::CONFIRM => {
                        if args.multi {
                            if state.chosen_indices().is_empty() {
                                state.toggle_cursor();
                            }
                            result = Some(state.chosen_paths());
                        } else if args.dirs {
                            result = Some(vec![state.cwd.clone()]);
                        } else if let Some(entry) = state.selected_entry() {
                            if entry.is_dir {
                                if entry.name == ".." {
                                    state.go_up();
                                } else {
                                    state.enter_directory();
                                }
                            } else {
                                result = Some(vec![entry.path.clone()]);
                            }
                        }
                        pending_g = false;
                    }
                    keybinds::OPEN_DIR if !state.show_filter() => {
                        if let Some(entry) = state.selected_entry() {
                            if entry.is_dir {
                                if entry.name == ".." {
                                    state.go_up();
                                } else {
                                    state.enter_directory();
                                }
                            }
                        }
                        pending_g = false;
                    }
                    keybinds::FILTER_BACKSPACE => {
                        if state.show_filter() {
                            state.delete_before_filter();
                        } else {
                            state.go_up();
                        }
                    }
                    keybinds::FILTER_LEFT => {
                        if state.show_filter() {
                            state.filter_cursor_left();
                        } else {
                            state.go_up();
                        }
                    }
                    keybinds::FILTER_RIGHT if state.show_filter() => {
                        state.filter_cursor_right();
                    }
                    keybinds::FILTER_HOME if state.show_filter() => {
                        state.filter_cursor_home();
                    }
                    keybinds::FILTER_END if state.show_filter() => {
                        state.filter_cursor_end();
                    }
                    keybinds::CANCEL => {
                        if state.show_filter() {
                            state.set_show_filter(false);
                        } else {
                            break;
                        }
                    }
                    keybinds::FIRST => {
                        if pending_g {
                            state.choose_popup_state.first();
                            pending_g = false;
                        } else {
                            pending_g = true;
                        }
                    }
                    keybinds::LAST => {
                        state.choose_popup_state.last(state.visible_count());
                        pending_g = false;
                    }
                    KeyCode::Char(c) if state.show_filter() => {
                        state.insert_filter_char(c);
                        pending_g = false;
                    }
                    keybinds::FILTER_ACTIVATE if !state.show_filter() => {
                        state.set_show_filter(true);
                    }
                    _ => pending_g = false,
                }

                let visible = state.visible_count();
                if visible > 0 {
                    state.choose_popup_state.cursor = state.choose_popup_state.cursor.min(visible.saturating_sub(1));
                } else {
                    state.choose_popup_state.cursor = 0;
                }
            }
            Event::Mouse(mouse) => {
                if mouse.kind == MouseEventKind::Down(crossterm::event::MouseButton::Left) {
                    // no mouse handling for file browser (no drag needed)
                }
            }
            _ => {}
        }
    }

    disable_raw_mode().unwrap();
    execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture).unwrap();
    terminal.show_cursor().unwrap();

    if let Some(paths) = result {
        for path in &paths {
            println!("{}", path.display());
        }
        std::process::exit(if paths.is_empty() { 1 } else { 0 });
    }
    std::process::exit(1);
}
