import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/data/bean/custom_config_info.dart';
import 'package:keyboard/data/repository/characters_repository.dart';
import 'package:get/get.dart';
import 'package:keyboard/data/repository/config_repository.dart';
import 'package:keyboard/module/store/store_page.dart';
import 'package:keyboard/utils/atmob_log.dart';

import '../../../utils/http_handler.dart';
import '../../../utils/toast_util.dart';

// 定制人设详情页
@injectable
class CharacterCustomDetailController extends BaseController {
  final String tag = 'CharacterCustomDetailController';

  final CharactersRepository charactersRepository;

  final ConfigRepository configRepository;

  CustomConfigInfo? get currentCharacterCustomConfig =>
      configRepository.characterCustomConfig;

  final RxList<Hobbies> _hobbiesSelectLabels = <Hobbies>[].obs;

  List<Hobbies> get hobbiesSelectLabels => _hobbiesSelectLabels.toList();

  final RxList<CharactersList> _characterSelectLabels = <CharactersList>[].obs;

  List<CharactersList> get characterSelectLabels =>
      _characterSelectLabels.toList();

  final RxString _characterCustomName = "".obs;

  String get characterCustomName => _characterCustomName.value;

  final RxInt _gender = 1.obs;

  final RxString _birthday = "".obs;

  final RxString _avatarUrl = "".obs;

  String get avatarUrl => _avatarUrl.value;

  final List<String> _boyAvatars = [];

  final List<String> _girlAvatars = [];

  CharacterCustomDetailController(
    this.charactersRepository,
    this.configRepository,
  );

  @override
  void onInit() {
    _getArgs();

    super.onInit();
    initData();
  }

  void initData() {
    AtmobLog.d(tag, "initData");
    _boyAvatars.addAll(currentCharacterCustomConfig?.boyAvatars ?? []);
    _girlAvatars.addAll(currentCharacterCustomConfig?.girlAvatars ?? []);
    if (_gender.value == 1) {
      _avatarUrl.value = _boyAvatars[0];
    } else {
      _avatarUrl.value = _girlAvatars[0];
    }
  }

  void nextAvatar() {
    AtmobLog.d(tag, "nextAvatar");

    if (_gender.value == 1) {
      int currentIndex = _boyAvatars.indexOf(_avatarUrl.value);
      _avatarUrl.value = _boyAvatars[(currentIndex + 1) % _boyAvatars.length];
    } else {
      int currentIndex = _girlAvatars.indexOf(_avatarUrl.value);
      _avatarUrl.value = _girlAvatars[(currentIndex + 1) % _girlAvatars.length];
    }
  }

  void _getArgs() {
    final arguments = Get.arguments as Map<String, dynamic>?;

    if (arguments?['hobbiesSelectLabels'] == null) {
      AtmobLog.i(tag, '没有传递 hobbiesSelectLabels 参数');
    } else {
      _hobbiesSelectLabels.assignAll(arguments?['hobbiesSelectLabels'] ?? []);
      AtmobLog.i(tag, "hobbiesSelectLabels: $hobbiesSelectLabels");
    }

    if (arguments?['characterSelectLabels'] == null) {
      AtmobLog.i(tag, '没有传递 characterSelectLabels 参数');
    } else {
      _characterSelectLabels.assignAll(
        arguments?['characterSelectLabels'] ?? [],
      );
      AtmobLog.i(tag, "characterSelectLabels: $characterSelectLabels");
    }

    if (arguments?['characterCustomName'] == null) {
      AtmobLog.i(tag, '警告: 没有传递 characterCustomName 参数');
    } else {
      _characterCustomName(arguments?['characterCustomName'] ?? '');
      AtmobLog.i(tag, "characterCustomName: $characterCustomName");
    }
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void clickBack() {
    Get.back();
  }

  void clickUnlockButton() {
    AtmobLog.d(tag, "点击解锁按钮,生成专属人设");
    generateCharacterCustom();
  }

  // 生成定制人设
  Future<void> generateCharacterCustom() async {
    try {
      await charactersRepository.generateCharacterCustom(
        name: _characterCustomName.value,
        gender: 1,
        hobbies:
            _hobbiesSelectLabels
                .map((hobby) => hobby.name)
                .whereType<String>()
                .toList(),
        characters:
            _characterSelectLabels
                .map((character) => character.name)
                .whereType<String>()
                .toList(),
        birthday: _birthday.value,
        imageUrl: _avatarUrl.value,
      );
    } catch (error) {
      if (error is ServerErrorException && error.code == 1005) {
        ToastUtil.show('请开通会员解锁权益~');
        StorePage.start();
      }
      if (error is ServerErrorException) {
        ToastUtil.show(error.message);
      }
    }
  }
}
