import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:trans_lark/global/translate_manager.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:translator/translator.dart';
import 'package:trans_lark/global/speech_to_text_manager.dart';

class P2PController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final aboveScrollController = ScrollController();
  final underScrollController = ScrollController();

  var angle = 0.0.obs;

  /// 显示的文本
  var aboveText = ''.obs;
  var underText = ''.obs;

  /// 翻译
  final _translator = GoogleTranslator();

  /// 是否选择的下方
  var selectedUnder = true.obs;

  @override
  void onReady() async {
    super.onReady();
    onSpeak(true);
  }

  @override
  void onClose() async {
    await SpeechToTextManager().stopListening();
    aboveScrollController.dispose();
    underScrollController.dispose();
    super.onClose();
  }

  Future<void> onBack() async {
    await SpeechToTextManager().stopListening();
    Get.back();
  }

  /// 旋转 180 度
  void onRotate() {
    angle.value -= 0.5;
  }

  Future<void> onSpeak(bool isUnder, {int? speakType}) async {
    if (!SpeechToTextManager().hasSpeech) {
      await SpeechToTextManager().init();
    }
    if (SpeechToTextManager().hasSpeech) {
      if (isUnder) {
        if (!selectedUnder.value) {
          await SpeechToTextManager().stopListening();
        }
        selectedUnder.value = true;
        _scrollToBottom();
        SpeechToTextManager().startListening(
            TranslateManager().fromLanguageEntity.value.languageCode,
            _speechRes);
      } else {
        if (selectedUnder.value) {
          await SpeechToTextManager().stopListening();
        }
        selectedUnder.value = false;
        _scrollToBottom();
        SpeechToTextManager().startListening(
            TranslateManager().toLanguageEntity.value.languageCode, _speechRes);
      }
    } else {
      TBaseEasyLoading.toast('Speech not available');
    }
  }

  Future<void> _speechRes(SpeechRecognitionResult result) async {
    TPrint.d('识别结果：${result.recognizedWords}');
    if (TObjectUtils.isNotEmptyStr(result.recognizedWords)) {
      if (selectedUnder.value) {
        underText.value += '${result.recognizedWords}\n';
        var translateText = await _translation(result.recognizedWords);
        TPrint.d('翻译结果：${result.recognizedWords}');
        if (TObjectUtils.isNotEmptyStr(translateText)) {
          aboveText.value += '$translateText\n';
        }
      } else {
        aboveText.value += '${result.recognizedWords}\n';
        var translateText = await _translation(result.recognizedWords);
        TPrint.d('翻译结果：${result.recognizedWords}');
        if (TObjectUtils.isNotEmptyStr(translateText)) {
          underText.value += '$translateText\n';
        }
      }
      _scrollToBottom();
    }
  }

  Future<String> _translation(String sourceText, {int? translationType}) async {
    try {
      String from = selectedUnder.value
          ? TranslateManager().fromLanguageEntity.value.languageCode
          : TranslateManager().toLanguageEntity.value.languageCode;
      String to = selectedUnder.value
          ? TranslateManager().toLanguageEntity.value.languageCode
          : TranslateManager().fromLanguageEntity.value.languageCode;
      Translation translate = await _translator.translate(
        sourceText,
        from: from,
        to: to,
      );
      return translate.text;
    } catch (e) {
      TPrint.d('Translation failed,${e.toString()}');
    }
    return '';
  }

  /// 滚动到列表的最底部
  void _scrollToBottom() {
    if (aboveScrollController.hasClients) {
      aboveScrollController.animateTo(
        aboveScrollController.position.maxScrollExtent,
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeOut,
      );
    }
    if (underScrollController.hasClients) {
      underScrollController.animateTo(
        underScrollController.position.maxScrollExtent,
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeOut,
      );
    }
  }
}
