import 'dart:convert';
import 'dart:math';
import 'dart:ui';

import 'package:devicelocale/devicelocale.dart';
import 'package:flutter/services.dart';
import 'package:flutter_translate/common/components/common_language_selector.dart';
import 'package:flutter_translate/common/utils/number_utils.dart';
import 'package:flutter_translate/generated/assets.dart';
import 'package:flutter_translate/model/language_model.dart';
import 'package:flutter_translate/pages/home/home_controller.dart';
import 'package:get/get.dart';

class Translate {
  Translate._();

  factory Translate() {
    return _instance;
  }

  static final Translate _instance = Translate._();

  /// 支持翻译的所有语言
  var languageList = <LanguageModel>[].obs;

  /// 是否选择的源语言
  var isSelectFromLanguage = true.obs;

  /// 源语言
  var fromLanguageEntity = LanguageModel(
    code: 'zh-cn',
    name: 'Chinese (Simplified)',
  ).obs;

  /// 目标语言
  var toLanguageEntity = LanguageModel(
    code: 'en',
    name: 'English',
  ).obs;

  Future<void> init() async {
    var data = jsonDecode(await rootBundle.loadString(Assets.jsonLanguage));
    if (data != null && data is List) {
      languageList.value = data.map((e) => LanguageModel.fromMap(e)).toList();
    }

    List<Locale> languages = await Devicelocale.preferredLanguagesAsLocales;
    if (languages.isNotEmpty) {
      List<dynamic> list = data.map((e) => LanguageModel.fromMap(e)).toList();
      var languageEntity = list
          .firstWhereOrNull((e) => e.code.contains(languages[0].languageCode));

      if (languageEntity != null) {
        fromLanguageEntity.value = languageEntity;
      }
      if (toLanguageEntity.value.code == fromLanguageEntity.value.code) {
        int index = list.indexWhere(
            (element) => element.code == toLanguageEntity.value.code);

        var num =
            NumberUtils.getRandomNumberExcludingCurrent(0, list.length, index);

        toLanguageEntity.value = list[num];
      }
    }
  }

  void languageConvert() async {
    var code = toLanguageEntity.value.code;
    var name = toLanguageEntity.value.name;

    toLanguageEntity.update((fn) {
      fn?.code = fromLanguageEntity.value.code;
      fn?.name = fromLanguageEntity.value.name;
    });

    fromLanguageEntity.update((fn) {
      fn?.code = code;
      fn?.name = name;
    });
    if (Get.isRegistered<HomeController>()) {
      HomeController.to.initData();
    }
  }

  void changeLanguage(LanguageModel item) {
    if (isSelectFromLanguage.value) {
      fromLanguageEntity.update((fn) {
        fn?.code = item.code;
        fn?.name = item.name;
      });
    } else {
      toLanguageEntity.update((fn) {
        fn?.code = item.code;
        fn?.name = item.name;
      });
    }
    if (Get.isRegistered<HomeController>()) {
      HomeController.to.initData();
    }
  }

  void languageChangeBottomSheet(bool isFrom) {
    isSelectFromLanguage.value = isFrom;
    Get.bottomSheet(
      isScrollControlled: true,
      const CommonLanguageSelector(),
    );
  }

  void changeSelectLanguage(bool isFrom) {
    isSelectFromLanguage.value = isFrom;
  }
}
