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/hive/history_data.dart';
import 'package:trans_lark/page/translator/translator_controller.dart';
import 'package:trans_lark/util/t_object_utils.dart';
import 'package:trans_lark/util/t_print.dart';
import 'package:trans_lark/widget/t_base_easy_loading.dart';
import 'package:trans_lark/widget/t_translate_text_full_screen_page.dart';
import 'package:translator/translator.dart';
import 'package:trans_lark/entity/history_model.dart';

class TranslatorResultController extends GetxController {
  var translator = GoogleTranslator();
  RxString sourceText = "".obs;
  RxString targetText = "".obs;
  var isTranslationCompleted = false.obs;
  bool isHistory = false;
  String? fromLanguage;
  String? toLanguage;

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

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

  Future<void> translation({String? text, String? language}) async {
    if (TObjectUtils.isNotEmpty(sourceText.value)) {
      try {
        Translation translate = await translator.translate(
          sourceText.value,
          from: TranslateManager().fromLanguageEntity.value.languageCode,
          to: TranslateManager().toLanguageEntity.value.languageCode,
        );
        targetText.value = translate.text;
        addHistory();
      } catch (e) {
        TBaseEasyLoading.toast('Translation failed');
        TPrint.d('Translation failed,${e.toString()}');
      }
      isTranslationCompleted.value = true;
    }
  }

  void addHistory({HistoryEntity? entity, int? index}) {
    final entity = HistoryEntity(
      sourceText: sourceText.value,
      targetText: targetText.value,
      sourceLanguageName:
          TranslateManager().fromLanguageEntity.value.languageName,
      sourceLanguageCode:
          TranslateManager().fromLanguageEntity.value.languageCode,
      targetLanguageName:
          TranslateManager().toLanguageEntity.value.languageName,
      targetLanguageCode:
          TranslateManager().toLanguageEntity.value.languageCode,
    );
    HistoryData().addHistory(entity);
    if (Get.isRegistered<TranslatorController>()) {
      final controller = Get.find<TranslatorController>();
      controller.insertHistory(entity);
    }
  }

  void onTextToSpeech(String text, String type, {String? language}) {
    if (TObjectUtils.isNotEmpty(text)) {
      String language =
          TranslateManager().fromLanguageEntity.value.languageCode;
      switch (type) {
        case "from":
          language = fromLanguage ??
              TranslateManager().fromLanguageEntity.value.languageCode;
          break;
        case "to":
          language = toLanguage ??
              TranslateManager().toLanguageEntity.value.languageCode;
          break;
      }
      FlutterTtsManager().translatorTtsPlay(text, language);
    }
  }

  void copyText({String? copyText}) {
    if (TObjectUtils.isNotEmpty(targetText.value)) {
      Clipboard.setData(ClipboardData(text: targetText.value));
      TBaseEasyLoading.toast('Copied to clipboard');
    }
  }

  void toFullScreen({String? title}) {
    if (TObjectUtils.isNotEmpty(targetText.value)) {
      Get.to(() => const TTranslateTextFullScreenPage(),
          arguments: {"data": targetText.value});
    }
  }
}
