import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:trans_lark/global/flutter_tts_manager.dart';
import 'package:trans_lark/global/translate_manager.dart';
import 'package:trans_lark/page/translator_result/translator_result_state.dart';
import 'package:trans_lark/util/log_print.dart';
import 'package:translator/translator.dart';
import 'package:trans_lark/entity/history_model.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';

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: TranslateManager().fromLanguageEntity.value.languageCode,
          to: TranslateManager().toLanguageEntity.value.languageCode,
        );
        state.targetText.value = translate.text;
        addHistory();
      } catch (e) {
        BaseEasyLoading.toast('Translation failed');
        LogPrint.d('Translation failed,${e.toString()}');
      }
      state.isTranslationCompleted.value = true;
    }
  }

  void addHistory() {
    final entity = HistoryEntity(
      sourceText: state.sourceText.value,
      targetText: state.targetText.value,
      sourceLanguageName:
          TranslateManager().fromLanguageEntity.value.languageName,
      sourceLanguageCode:
          TranslateManager().fromLanguageEntity.value.languageCode,
      targetLanguageName:
          TranslateManager().toLanguageEntity.value.languageName,
      targetLanguageCode:
          TranslateManager().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 =
          TranslateManager().fromLanguageEntity.value.languageCode;
      switch (type) {
        case "from":
          language = state.fromLanguage ??
              TranslateManager().fromLanguageEntity.value.languageCode;
          break;
        case "to":
          language = state.toLanguage ??
              TranslateManager().toLanguageEntity.value.languageCode;
          break;
      }
      FlutterTtsManager().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});
    }
  }
}
