import 'package:get/get.dart';
import 'package:trans_lark/entity/language_entity.dart';
import 'package:trans_lark/entity/scene_entity.dart';
import 'package:trans_lark/global/flutter_tts_manager.dart';
import 'package:trans_lark/widget/language_scene_bottom_sheet.dart';

class SceneListController extends GetxController {
  var languageScene = <LanguageEntity>[
    LanguageEntity(languageCode: 'ar', languageName: 'Arabic'),
    LanguageEntity(languageCode: 'bn', languageName: 'Bengali'),
    LanguageEntity(languageCode: 'zh-cn', languageName: 'Chinese (Simplified)'),
    LanguageEntity(languageCode: 'en', languageName: 'English'),
    LanguageEntity(languageCode: 'fr', languageName: 'French'),
    LanguageEntity(languageCode: 'de', languageName: 'German'),
    LanguageEntity(languageCode: 'hi', languageName: 'Hindi'),
    LanguageEntity(languageCode: 'ja', languageName: 'Japanese'),
    LanguageEntity(languageCode: 'jv', languageName: 'Javanese'),
    LanguageEntity(languageCode: 'ko', languageName: 'Korean'),
    LanguageEntity(languageCode: 'pt', languageName: 'Portuguese'),
    LanguageEntity(languageCode: 'pa', languageName: 'Punjabi'),
    LanguageEntity(languageCode: 'ru', languageName: 'Russian'),
    LanguageEntity(languageCode: 'es', languageName: 'Spanish'),
  ];
  late Rx<LanguageEntity> fromLanguage;
  late Rx<LanguageEntity> toLanguage;

  var sceneList = <SceneList>[];

  @override
  void onInit() {
    super.onInit();
    sceneList = Get.arguments;
    fromLanguage = languageScene[3].copyWith().obs;
    toLanguage = languageScene[2].copyWith().obs;
  }

  void onTapFrom() {
    Get.bottomSheet(
      LanguageSceneBottomSheet(
        languageScene: languageScene,
        selectedLanguage: fromLanguage,
      ),
    );
  }

  void onTapTo() {
    Get.bottomSheet(
      LanguageSceneBottomSheet(
        languageScene: languageScene,
        selectedLanguage: toLanguage,
      ),
    );
  }

  String getSentence(bool isFrom, SceneList item) {
    List keys = item.toMap().keys.toList();
    int index = 0;
    for (var i = 0; i < keys.length; ++i) {
      var e = keys[i];
      if (e ==
          (isFrom
              ? fromLanguage.value.languageName
              : toLanguage.value.languageName)) {
        index = i;
        break;
      }
    }
    if (item.toMap().values.toList()[index] != null) {
      return item.toMap().values.toList()[index];
    }
    return 'loading';
  }

  void translatorTtsPlay(bool isFrom, SceneList item) {
    FlutterTtsManager().translatorTtsPlay(
      getSentence(isFrom, item),
      isFrom ? fromLanguage.value.languageCode : toLanguage.value.languageCode,
    );
  }
}
