import 'package:flutter/services.dart';
import 'package:flutter_translate/common/hive/history_data.dart';
import 'package:flutter_translate/common/utils/bot_toast.dart';
import 'package:flutter_translate/common/utils/log_utils.dart';
import 'package:flutter_translate/common/utils/object_utils.dart';
import 'package:flutter_translate/manager/translate.dart';
import 'package:flutter_translate/manager/tts.dart';
import 'package:flutter_translate/model/history_model.dart';
import 'package:flutter_translate/pages/home/home_controller.dart';
import 'package:flutter_translate/pages/translator/translator_controller.dart';
import 'package:get/get.dart';
import 'package:translator/translator.dart';

class TranslatedTextController extends GetxController {
  HomeController homeController = Get.find<HomeController>();
  var translator = GoogleTranslator();
  RxString sourceText = "".obs;
  RxString targetText = "".obs;
  var isTranslationCompleted = false.obs;
  bool isHistory = false;
  String? fromLanguageName;
  String? toLanguageName;
  String? fromLanguageCode;
  String? toLanguageCode;
  RxBool isPlayingFrom = false.obs;
  RxBool isPlayingTo = false.obs;

  @override
  void onInit() {
    super.onInit();
    isHistory = Get.arguments['isHistory'] ?? false;
    sourceText.value = Get.arguments['sourceText'] ?? '';
    targetText.value = Get.arguments['targetText'] ?? '';
    fromLanguageName = Get.arguments['fromLanguageName'];
    toLanguageName = Get.arguments['toLanguageName'];
    fromLanguageCode = Get.arguments['fromLanguageCode'];
    toLanguageCode = Get.arguments['toLanguageCode'];
  }

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

  Future<void> translation() async {
    if (ObjectUtils.isNotEmpty(sourceText.value)) {
      try {
        Translation translate = await translator.translate(
          sourceText.value,
          from: Translate().fromLanguageEntity.value.code,
          to: Translate().toLanguageEntity.value.code,
        );
        targetText.value = translate.text;
        addHistory();
      } catch (e) {
        toast('Translation failed');
        Log.d('Translation failed,${e.toString()}');
      }
      isTranslationCompleted.value = true;
    }
  }

  void addHistory() {
    final entity = HistoryModel(
      sourceText: sourceText.value,
      targetText: targetText.value,
      sourceLanguageName: Translate().fromLanguageEntity.value.name,
      sourceLanguageCode: Translate().fromLanguageEntity.value.code,
      targetLanguageName: Translate().toLanguageEntity.value.name,
      targetLanguageCode: Translate().toLanguageEntity.value.code,
    );
    HistoryData().add(entity);
    if (Get.isRegistered<TranslatorController>()) {
      final controller = Get.find<TranslatorController>();
      controller.insertHistory(entity);
      homeController.initHistoryList();
    }
  }

  void onTextToSpeech(String text, String type) async {
    isPlayingTo.value = false;
    isPlayingFrom.value = false;
    if (ObjectUtils.isNotEmpty(text)) {
      String language = Translate().fromLanguageEntity.value.code;
      switch (type) {
        case "from":
          isPlayingFrom.value = true;
          language = fromLanguageCode ?? Translate().fromLanguageEntity.value.code;
          await Tts().voiceTranslator(text, language);
          isPlayingFrom.value = false;
          break;
        case "to":
          isPlayingTo.value = true;
          language = toLanguageCode ?? Translate().toLanguageEntity.value.code;
          await Tts().voiceTranslator(text, language);
          isPlayingTo.value = false;
          break;
      }
    }
  }

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

  void toFullScreen() {
    // if (ObjectUtils.isNotEmpty(targetText.value)) {
    //   Get.to(() => const TTranslateTextFullScreenPage(),
    //       arguments: {"data": targetText.value});
    // }
  }
}
