use std::path::Path;

use crate::plugin::{CleanAction, CleanTarget, Plugin, Safety};
use crate::utils;

pub struct ChromePlugin;

impl Plugin for ChromePlugin {
    fn name(&self) -> &str {
        "chrome"
    }

    fn description(&self) -> &str {
        "Google Chrome caches and data"
    }

    fn scan_directory(&self, _dir: &Path) -> Vec<CleanTarget> {
        Vec::new()
    }

    fn global_targets(&self) -> Vec<CleanTarget> {
        let mut targets = Vec::new();
        let chrome_dir = chrome_support_dir();

        if !chrome_dir.is_dir() {
            return targets;
        }

        // Browser cache (HTTP cache, media cache, etc.)
        let cache = chrome_cache_dir();
        if cache.is_dir() {
            let size = utils::dir_size(&cache);
            targets.push(CleanTarget {
                label: format!("{}", cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Browser cache, regenerated automatically".to_string(),
            });
        }

        // Service Worker cache
        let sw_cache = chrome_dir.join("Default/Service Worker/CacheStorage");
        if sw_cache.is_dir() {
            let size = utils::dir_size(&sw_cache);
            targets.push(CleanTarget {
                label: format!("{}", sw_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(sw_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Service Worker cache, regenerated automatically".to_string(),
            });
        }

        // GPU shader cache
        let gpu_cache = chrome_dir.join("ShaderCache");
        if gpu_cache.is_dir() {
            let size = utils::dir_size(&gpu_cache);
            targets.push(CleanTarget {
                label: format!("{}", gpu_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(gpu_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "GPU shader cache, regenerated automatically".to_string(),
            });
        }

        // GrShaderCache
        let gr_cache = chrome_dir.join("GrShaderCache");
        if gr_cache.is_dir() {
            let size = utils::dir_size(&gr_cache);
            targets.push(CleanTarget {
                label: format!("{}", gr_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(gr_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "GPU shader cache, regenerated automatically".to_string(),
            });
        }

        // Code Cache (compiled JS bytecode)
        let code_cache = chrome_dir.join("Default/Code Cache");
        if code_cache.is_dir() {
            let size = utils::dir_size(&code_cache);
            targets.push(CleanTarget {
                label: format!("{}", code_cache.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(code_cache),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "JavaScript bytecode cache, regenerated automatically".to_string(),
            });
        }

        // Crash reports
        let crash_reports = chrome_dir.join("Crashpad/completed");
        if crash_reports.is_dir() {
            let size = utils::dir_size(&crash_reports);
            targets.push(CleanTarget {
                label: format!("{}", crash_reports.display()),
                size_bytes: size,
                action: CleanAction::DeletePath(crash_reports),
                plugin_name: self.name().to_string(),
                safety: Safety::Recoverable,
                explanation: "Crash report data".to_string(),
            });
        }

        targets
    }

    fn is_available(&self) -> bool {
        chrome_support_dir().is_dir()
    }
}

fn chrome_support_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Application Support/Google/Chrome")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".config/google-chrome")
    } else {
        // Windows: %LOCALAPPDATA%\Google\Chrome\User Data
        utils::home_dir().join("AppData/Local/Google/Chrome/User Data")
    }
}

fn chrome_cache_dir() -> std::path::PathBuf {
    if cfg!(target_os = "macos") {
        utils::home_dir().join("Library/Caches/Google/Chrome")
    } else if cfg!(target_os = "linux") {
        utils::home_dir().join(".cache/google-chrome")
    } else {
        chrome_support_dir().join("Cache")
    }
}
