use crate::config::Language;

pub struct Translator {
    language: Language,
    keep_types_english: bool,
    keep_changelog_types_english: bool,
}

impl Translator {
    pub fn new(language: Language, keep_types_english: bool, keep_changelog_types_english: bool) -> Self {
        Self {
            language,
            keep_types_english,
            keep_changelog_types_english,
        }
    }

    pub fn translate_commit_type(&self, commit_type: &str) -> String {
        if self.keep_types_english {
            return commit_type.to_string();
        }

        match self.language {
            Language::English => commit_type.to_string(),
            Language::Chinese => self.translate_commit_type_zh(commit_type),
            Language::Japanese => self.translate_commit_type_ja(commit_type),
            Language::Korean => self.translate_commit_type_ko(commit_type),
            Language::Spanish => self.translate_commit_type_es(commit_type),
            Language::French => self.translate_commit_type_fr(commit_type),
            Language::German => self.translate_commit_type_de(commit_type),
        }
    }

    pub fn translate_changelog_category(&self, category: &str) -> String {
        if self.keep_changelog_types_english {
            return category.to_string();
        }

        match self.language {
            Language::English => category.to_string(),
            Language::Chinese => self.translate_changelog_category_zh(category),
            Language::Japanese => self.translate_changelog_category_ja(category),
            Language::Korean => self.translate_changelog_category_ko(category),
            Language::Spanish => self.translate_changelog_category_es(category),
            Language::French => self.translate_changelog_category_fr(category),
            Language::German => self.translate_changelog_category_de(category),
        }
    }

    fn translate_commit_type_zh(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "新功能".to_string(),
            "fix" => "修复".to_string(),
            "docs" => "文档".to_string(),
            "style" => "样式".to_string(),
            "refactor" => "重构".to_string(),
            "perf" => "性能".to_string(),
            "test" => "测试".to_string(),
            "build" => "构建".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "杂项".to_string(),
            "revert" => "回滚".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_commit_type_ja(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "機能".to_string(),
            "fix" => "修正".to_string(),
            "docs" => "ドキュメント".to_string(),
            "style" => "スタイル".to_string(),
            "refactor" => "リファクタリング".to_string(),
            "perf" => "パフォーマンス".to_string(),
            "test" => "テスト".to_string(),
            "build" => "ビルド".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "雑務".to_string(),
            "revert" => "取り消し".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_commit_type_ko(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "기능".to_string(),
            "fix" => "버그 수정".to_string(),
            "docs" => "문서".to_string(),
            "style" => "스타일".to_string(),
            "refactor" => "리팩토링".to_string(),
            "perf" => "성능".to_string(),
            "test" => "테스트".to_string(),
            "build" => "빌드".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "기타".to_string(),
            "revert" => "되돌리기".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_commit_type_es(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "nueva función".to_string(),
            "fix" => "corrección".to_string(),
            "docs" => "documentación".to_string(),
            "style" => "estilo".to_string(),
            "refactor" => "refactorización".to_string(),
            "perf" => "rendimiento".to_string(),
            "test" => "pruebas".to_string(),
            "build" => "construcción".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "tareas".to_string(),
            "revert" => "revertir".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_commit_type_fr(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "nouvelle fonctionnalité".to_string(),
            "fix" => "correction".to_string(),
            "docs" => "documentation".to_string(),
            "style" => "style".to_string(),
            "refactor" => "refactorisation".to_string(),
            "perf" => "performance".to_string(),
            "test" => "tests".to_string(),
            "build" => "construction".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "tâches".to_string(),
            "revert" => "rétablir".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_commit_type_de(&self, commit_type: &str) -> String {
        match commit_type {
            "feat" => "Neue Funktion".to_string(),
            "fix" => "Korrektur".to_string(),
            "docs" => "Dokumentation".to_string(),
            "style" => "Stil".to_string(),
            "refactor" => "Refactoring".to_string(),
            "perf" => "Leistung".to_string(),
            "test" => "Tests".to_string(),
            "build" => "Build".to_string(),
            "ci" => "CI".to_string(),
            "chore" => "Wartung".to_string(),
            "revert" => "Zurücksetzen".to_string(),
            _ => commit_type.to_string(),
        }
    }

    fn translate_changelog_category_zh(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "新增".to_string(),
            "changed" => "更改".to_string(),
            "deprecated" => "弃用".to_string(),
            "removed" => "移除".to_string(),
            "fixed" => "修复".to_string(),
            "security" => "安全".to_string(),
            _ => category.to_string(),
        }
    }

    fn translate_changelog_category_ja(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "追加".to_string(),
            "changed" => "変更".to_string(),
            "deprecated" => "非推奨".to_string(),
            "removed" => "削除".to_string(),
            "fixed" => "修正".to_string(),
            "security" => "セキュリティ".to_string(),
            _ => category.to_string(),
        }
    }

    fn translate_changelog_category_ko(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "추가됨".to_string(),
            "changed" => "변경됨".to_string(),
            "deprecated" => "사용 중단".to_string(),
            "removed" => "제거됨".to_string(),
            "fixed" => "수정됨".to_string(),
            "security" => "보안".to_string(),
            _ => category.to_string(),
        }
    }

    fn translate_changelog_category_es(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "Agregado".to_string(),
            "changed" => "Cambiado".to_string(),
            "deprecated" => "Obsoleto".to_string(),
            "removed" => "Eliminado".to_string(),
            "fixed" => "Corregido".to_string(),
            "security" => "Seguridad".to_string(),
            _ => category.to_string(),
        }
    }

    fn translate_changelog_category_fr(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "Ajouté".to_string(),
            "changed" => "Modifié".to_string(),
            "deprecated" => "Obsolète".to_string(),
            "removed" => "Supprimé".to_string(),
            "fixed" => "Corrigé".to_string(),
            "security" => "Sécurité".to_string(),
            _ => category.to_string(),
        }
    }

    fn translate_changelog_category_de(&self, category: &str) -> String {
        match category.to_lowercase().as_str() {
            "added" => "Hinzugefügt".to_string(),
            "changed" => "Geändert".to_string(),
            "deprecated" => "Veraltet".to_string(),
            "removed" => "Entfernt".to_string(),
            "fixed" => "Behoben".to_string(),
            "security" => "Sicherheit".to_string(),
            _ => category.to_string(),
        }
    }
}

pub fn translate_commit_type(commit_type: &str, language: Language, keep_english: bool) -> String {
    let translator = Translator::new(language, keep_english, true);
    translator.translate_commit_type(commit_type)
}

pub fn translate_changelog_category(category: &str, language: Language, keep_english: bool) -> String {
    let translator = Translator::new(language, true, keep_english);
    translator.translate_changelog_category(category)
}
