<?php

namespace App\Service;
use App\Models\DeeplGlossary;
use DeepL\GlossaryEntries;
use Filament\Forms\Components\Actions\Action;

class DeepLService {

    protected $mapping_source_lang;
    protected $mapping_trans_lang;
    protected $apiAuthKey,$translator;

    public function __construct() {

        $this->mapping_source_lang = [
            "en" => "EN",
            "jp" => "JA",
            "zh_TW" => "ZH"
        ];
        $this->mapping_trans_lang = [
            "en" => "EN-US",
            "jp" => "JA",
            "zh_TW" => "ZH-HANT"
        ];
        $this->apiAuthKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
        $this->translator = new \DeepL\Translator($this->apiAuthKey);
    }

    public function translateWordings($text, $localeTo, $localeFrom = "zh_TW") {
        if(empty($text)){
            return "";
        }
        $authKey = config("thirdPartyApi.DeepL.api-key"); // Replace with your key
        $translator = new \DeepL\Translator($authKey);

        $glossary = DeeplGlossary::where('translate_to', $localeTo)->first();

        $result = $translator->translateText($text, $this->mapping_source_lang[$localeFrom], $this->mapping_trans_lang[$localeTo],[
            "glossary" => $glossary->glossary_id
        ]);
        return $result->text; // Bonjour, le monde!
    }
    function createTranslationAction($actionName, array $fieldNames)
    {
        return Action::make("translate{$actionName}")
            ->label("翻譯")
            ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
            ->action(function ($set, $get, $arguments) use ($fieldNames) {
                $locale = $arguments['locale'] ?? null;
                if (!$locale) {
                    return;
                }
                foreach($fieldNames as $fieldName){
                    $translateText = $this->translateWordings($get("{$fieldName}.zh_TW"), $locale);
                    $set("{$fieldName}.{$locale}", $translateText);
                }
            });
    }

    public function buildGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $glossaryItems){
        $entries = GlossaryEntries::fromEntries($glossaryItems);
        $locateTo = "";
        switch ($glossaryLocaleTo){
            case "EN":
                $locateTo = "en";
                break;
            case "JA":
                $locateTo = "jp";
                break;
            default:
                break;
        }
        $myGlossary = $this->translator->createGlossary($glossaryName, $glossaryLocaleFrom, $glossaryLocaleTo, $entries);
        DeeplGlossary::insert(["translate_to" => $locateTo,"glossary_name" => $myGlossary->name, "glossary_id" => $myGlossary->glossaryId]);
    }

    public function listGlossary(){

        // Find and delete glossaries named 'Old glossary'
        $glossaries = $this->translator->listGlossaries();
        $result = [];
        foreach ($glossaries as $glossary) {
            $myGlossary = $this->translator->getGlossaryEntries($glossary);
            $result[] = $myGlossary->getEntries();
        }
        return $result;
    }

    public function deleteGlossary(){
        $glossaries = $this->translator->listGlossaries();
        foreach ($glossaries as $glossary) {
            $this->translator->deleteGlossary($glossary);
        }
    }
}
