//! Dolphin (GameCube/Wii emulator) launch command builder via Flatpak
//!
//! Builds commands for running GameCube/Wii games via the Dolphin Flatpak

use anyhow::Result;
use std::path::{Path, PathBuf};

use crate::game::launch_command::{
    EmulatorLaunchCommand, EmulatorLauncher, EmulatorOptions, EmulatorPlatform, LaunchCommand,
    LaunchCommandKind,
};
use crate::menu_utils::{ConfirmResult, FzfWrapper};
use crate::ui::nerd_font::NerdFont;

use super::flatpak::is_flatpak_app_installed;
use super::prompts::{
    FileSelectionPrompt, ask_fullscreen, confirm_value, select_file_with_validation,
};
use super::validation::{DOLPHIN_EXTENSIONS, format_valid_extensions, validate_game_file};

/// Dolphin Flatpak application ID
const DOLPHIN_FLATPAK_ID: &str = "org.DolphinEmu.dolphin-emu";

pub struct DolphinBuilder;

impl DolphinBuilder {
    /// Build a Dolphin Flatpak launch command interactively
    pub fn build_command() -> Result<Option<LaunchCommand>> {
        // Step 1: Check if Dolphin Flatpak is installed
        if !Self::check_dolphin_installed()? {
            FzfWrapper::message(&format!(
                "{} Dolphin Flatpak not found!\n\n\
                 Install it with:\n\
                 flatpak install flathub {}\n\n\
                 Or visit: https://flathub.org/apps/org.DolphinEmu.dolphin-emu",
                char::from(NerdFont::CrossCircle),
                DOLPHIN_FLATPAK_ID
            ))?;
            return Ok(None);
        }

        // Step 2: Select game file
        let game_file = match Self::select_game_file()? {
            Some(f) => f,
            None => return Ok(None),
        };

        // Step 3: Ask for batch mode (exit when game closes)
        let batch_mode = Self::ask_batch_mode()?;

        // Step 4: Ask for fullscreen (only if not batch mode, as batch implies game-focused)
        let fullscreen = if !batch_mode {
            ask_fullscreen()?
        } else {
            false
        };

        // Build the command
        let command = Self::build_launch_command(&game_file, batch_mode, fullscreen);

        // Show preview and confirm
        confirm_value(command)
    }

    fn check_dolphin_installed() -> Result<bool> {
        is_flatpak_app_installed(DOLPHIN_FLATPAK_ID)
    }

    fn select_game_file() -> Result<Option<PathBuf>> {
        select_file_with_validation(
            FileSelectionPrompt::game_file(
                format!(
                    "{} Select GameCube/Wii Game File",
                    char::from(NerdFont::Disc)
                ),
                format!(
                    "{} Select a GameCube/Wii game file ({})",
                    char::from(NerdFont::Info),
                    format_valid_extensions(DOLPHIN_EXTENSIONS)
                ),
            ),
            |path| validate_game_file(path, "Dolphin", DOLPHIN_EXTENSIONS),
        )
    }

    fn ask_batch_mode() -> Result<bool> {
        match FzfWrapper::builder()
            .confirm(format!(
                "{} Use batch mode?\n\n\
                 Batch mode will exit Dolphin when the game closes.\n\
                 This is recommended for launching games directly.",
                char::from(NerdFont::Terminal)
            ))
            .yes_text("Yes, use batch mode")
            .no_text("No, keep Dolphin open")
            .confirm_dialog()?
        {
            ConfirmResult::Yes => Ok(true),
            _ => Ok(false),
        }
    }

    fn build_launch_command(game_file: &Path, batch_mode: bool, fullscreen: bool) -> LaunchCommand {
        let _ = DOLPHIN_FLATPAK_ID;
        LaunchCommand {
            wrappers: Default::default(),
            kind: LaunchCommandKind::Emulator(EmulatorLaunchCommand {
                platform: EmulatorPlatform::Dolphin,
                launcher: EmulatorLauncher::Flatpak {
                    app_id: DOLPHIN_FLATPAK_ID,
                },
                game: game_file.to_path_buf(),
                options: EmulatorOptions {
                    batch_mode,
                    fullscreen,
                },
            }),
        }
    }
}
