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/log_print.dart';
import 'package:translator/translator.dart';
import 'package:trans_lark/global/speech_to_text_manager.dart';
import 'package:trans_lark/util/obj_util.dart';
import 'package:trans_lark/widget/base_easyloading.dart';

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

  var angle = 0.0.obs;

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

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

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

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

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

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

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

  Future<void> onTapSpeak(bool isUnder) async {
    if (!SpeechToTextManager().hasSpeech) {
      await SpeechToTextManager().initSpeech();
    }
    if (SpeechToTextManager().hasSpeech) {
      if (isUnder) {
        if (!selectedUnder.value) {
          await SpeechToTextManager().stopListening();
        }
        selectedUnder.value = true;
        _scrollToBottom();
        SpeechToTextManager().startListening(TranslateManager().fromLanguageEntity.value.languageCode, _onSpeechResult);
      } else {
        if (selectedUnder.value) {
          await SpeechToTextManager().stopListening();
        }
        selectedUnder.value = false;
        _scrollToBottom();
        SpeechToTextManager().startListening(TranslateManager().toLanguageEntity.value.languageCode, _onSpeechResult);
      }
    } else {
      BaseEasyLoading.toast('Speech not available');
    }
  }

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

  Future<String> _translation(String sourceText) 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) {
      LogPrint.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,
      );
    }
  }
}
