import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:translator/translator.dart';
import 'package:trans_lark/entity/history_model.dart';
import 'package:trans_lark/global/translate_language.dart';
import 'package:trans_lark/global/tts_manager.dart';
import 'package:trans_lark/page/translator/translator_logic.dart';
import 'package:trans_lark/storage/history_data.dart';
import 'package:trans_lark/util/obj_util.dart';
import 'package:trans_lark/widget/base_easyloading.dart';
import 'package:trans_lark/widget/translate_text_full_screen_page.dart';

import 'translator_result_state.dart';

/// @description:
/// @author
/// @date: 2024-06-27 16:53:29
class TranslatorResultPageLogic extends GetxController {
  final state = TranslatorResultPageState();

  @override
  void onInit() {
    super.onInit();
    state.sourceText.value = Get.arguments['sourceText'] ?? '';
    state.targetText.value = Get.arguments['targetText'] ?? '';
    state.isHistory = Get.arguments['isHistory'] ?? false;
    state.fromLanguage = Get.arguments['fromLanguage'];
    state.toLanguage = Get.arguments['toLanguage'];
  }

  @override
  void onReady() {
    super.onReady();
    if (state.isHistory) {
      state.isTranslationCompleted.value = true;
    } else {
      translation();
    }
  }

  Future<void> translation() async {
    if (ObjUtil.isNotEmpty(state.sourceText.value)) {
      try {
        Translation translate = await state.translator.translate(
          state.sourceText.value,
          from: TranslateLanguage().fromLanguageEntity.value.languageCode,
          to: TranslateLanguage().toLanguageEntity.value.languageCode,
        );
        state.targetText.value = translate.text;
        addHistory();
      } catch (e) {
        BaseEasyLoading.toast('Translation failed');
        debugPrint('Translation failed,${e.toString()}');
      }
      state.isTranslationCompleted.value = true;
    }
  }

  void addHistory() {
    final entity = HistoryEntity(
      sourceText: state.sourceText.value,
      targetText: state.targetText.value,
      sourceLanguageName:
          TranslateLanguage().fromLanguageEntity.value.languageName,
      sourceLanguageCode:
          TranslateLanguage().fromLanguageEntity.value.languageCode,
      targetLanguageName:
          TranslateLanguage().toLanguageEntity.value.languageName,
      targetLanguageCode:
          TranslateLanguage().toLanguageEntity.value.languageCode,
    );
    HistoryData().addData(entity);
    if (Get.isRegistered<TranslatorLogic>()) {
      final logic = Get.find<TranslatorLogic>();
      logic.insertHistory(entity);
    }
  }

  void translatorTtsPlay(String text, type) {
    if (ObjUtil.isNotEmpty(text)) {
      String language = TranslateLanguage().fromLanguageEntity.value.languageCode;
      switch (type) {
        case "from":
          language = state.fromLanguage ?? TranslateLanguage().fromLanguageEntity.value.languageCode;
          break;
        case "to":
          language = state.toLanguage ?? TranslateLanguage().toLanguageEntity.value.languageCode;
          break;
      }
      TtsManager().translatorTtsPlay(text, language);
    }
  }

  void copy() {
    if (ObjUtil.isNotEmpty(state.targetText.value)) {
      Clipboard.setData(ClipboardData(text: state.targetText.value));
      BaseEasyLoading.toast('Copied to clipboard');
    }
  }

  void openFullScreenPage() {
    if (ObjUtil.isNotEmpty(state.targetText.value)) {
      Get.to(() => const TranslateTextFullScreenPage(),
          arguments: {"data": state.targetText.value});
    }
  }
}
