"""processor.py"""

# Standard Library
import csv

# Pip
import argostranslate.translate
import torchaudio

# Local
from .constants import LANGUAGES, WORD, COMMA
from .utility import CCCEDICT, TTS, DictionaryResult, ProcessFile, TranslationResult

# Constants

FIELDNAMES = ["simplified", "traditional", "pinyin", "meaning"]
DIALECT = "excel-tab"

# Results Classes


def dictation_process(
    text_lines: list[str], process_file: ProcessFile
) -> list[TranslationResult]:
    """Process for Dictation translation"""
    results = []
    for n, line in enumerate(text_lines):
        line = line.strip()
        line = "  ".join(line.split())
        audio_line = " ".join(line)
        audio_line = audio_line.replace("，", "，。。。]")
        audio_path = process_file.resources / f"N{n:03n}.wav"
        if not audio_path.exists():
            audio = TTS.MODEL.generate(
                f"{audio_line}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
            )
            torchaudio.save(audio_path, audio, TTS.MODEL.sr)
        translated = argostranslate.translate.translate(
            line, LANGUAGES.CN, process_file.language_id
        )
        results.append(
            TranslationResult(process_file.language_id, translated, line, audio_path)
        )
    return results


def translator_process(
    text_lines: list[str], process_file: ProcessFile
) -> list[TranslationResult]:
    """Process for phases or sentence translation"""
    results = []
    for n, line in enumerate(text_lines):
        line = line.strip()
        line = "  ".join(line.split())
        audio_line = "[pause:0.1s]".join(line)
        audio_line = audio_line.replace("，", "，[pause:0.5s]")
        audio_path = process_file.resources / f"N{n:03n}.wav"
        if not audio_path.exists():
            audio = TTS.MODEL.generate(
                f"{audio_line}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
            )
            torchaudio.save(audio_path, audio, TTS.MODEL.sr)
        translated = argostranslate.translate.translate(
            line, LANGUAGES.CN, process_file.language_id
        )
        results.append(
            TranslationResult(process_file.language_id, translated, line, audio_path)
        )
    return results


def dictionary_pre_process(words_list: list[str], process_file: ProcessFile):
    """Pre Process dictionary files into a intermediary resources file"""
    dictionary = CCCEDICT.create_cedict(process_file.language_id)
    with process_file.dictionary_resource_file.open(
        "w", encoding="utf8", newline=""
    ) as resource_file:
        tsv_writer = csv.DictWriter(
            resource_file, dialect=DIALECT, fieldnames=FIELDNAMES
        )
        tsv_writer.writeheader()
        for words in words_list:
            word = words.split()[0]
            pinyin = " ".join(words.split()[1:]) if len(words.split()) > 1 else None
            if entries := dictionary.get(word):
                if pinyin is not None:
                    entries = list(filter(lambda x: x.pinyin == pinyin, entries))
                if len(entries) > 1:
                    print(f"\nWARNING: {word} has multiple meanings:")
                for entry in entries:
                    for meaning in entry.meanings:
                        tsv_writer.writerow(
                            {
                                "simplified": entry.simplified,
                                "traditional": entry.traditional,
                                "pinyin": entry.pinyin,
                                "meaning": meaning,
                            }
                        )
            else:
                print("============================================")
                print(f"===================>ERROR: {word} not found")
                print("============================================")
                tsv_writer.writerow(
                    {
                        "simplified": word,
                        "traditional": None,
                        "pinyin": None,
                        "meaning": None,
                    }
                )


def dictionary_process(process_file: ProcessFile) -> list[DictionaryResult]:
    """Process a dictionary_resource_file into a final result"""
    results = []
    with process_file.dictionary_resource_file.open(
        "r", encoding="utf8", newline=""
    ) as resource_file:
        reader = csv.DictReader(resource_file, dialect=DIALECT)
        for line in reader:
            audio_path = process_file.resources / f"{line['pinyin']}.wav"
            if not audio_path.exists():
                audio = TTS.MODEL.generate(
                    f"{line['simplified']}。", language_id=LANGUAGES.CN, **TTS.DEFAULTS
                )
                torchaudio.save(audio_path, audio, TTS.MODEL.sr)
            print(line)
            result = DictionaryResult(
                **line, audio_path=audio_path, language_id=process_file.language_id
            )
            results.append(result)
    return results


# def output_tsv(out_file, results):
#     """writes the output as a tsv file"""
#     final_file = out_file.parent / f"{out_file.stem}.tsv"
#     with final_file.open("w", encoding="utf8", newline="") as csvfile:
#         writer = csv.writer(csvfile, delimiter="\t", quotechar='"')
#         for entry in results:
#             writer.writerow(
#                 [
#                     "\n ".join(f"{n+1}. {m}" for n, m in enumerate(entry.meanings)),
#                     PinyinToneConverter().convert_text(entry.pinyin),
#                     entry.simplified,
#                     entry.traditional,
#                 ]
#             )
