import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:get/get.dart';
import 'package:keyboard/utils/atmob_log.dart';

import '../../../data/bean/custom_config_info.dart';
import '../../../data/repository/config_repository.dart';
import '../../../dialog/custom_label_dialog.dart';
import '../../../resource/string.gen.dart';
import '../../../utils/toast_util.dart';

@injectable
class ChangeHobbiesController extends BaseController {
  final String tag = 'ChangeHobbiesController';
  final ConfigRepository configRepository;

  CustomConfigInfo? get currentCharacterCustomConfig =>
      configRepository.characterCustomConfig.value;

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

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

  ChangeHobbiesController(this.configRepository);

  @override
  void onInit() {
    super.onInit();
    initData();
    final List<Hobbies>? hobbies = Get.arguments["hobbies"];

    if (hobbies != null) {
      for (var hobby in hobbies) {
        final exists = hobbiesLabelsList.any((e) => e.name == hobby.name);
        if (!exists) {
          hobbiesLabelsList.add(hobby);
        }
      }

      hobbiesSelectLabels.assignAll(hobbies);
      AtmobLog.d(
        tag,
        "hobbiesSelectLabels: ${hobbiesSelectLabels.map((e) => e.name)}",
      );
    }
  }

  //  初始化数据
  void initData() {
    AtmobLog.d(tag, "initData");
    hobbiesLabelsList.value = currentCharacterCustomConfig?.hobbies ?? [];
  }

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

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

  void clickSave() {
    if (hobbiesSelectLabels.isEmpty) {
      ToastUtil.show(
        "至少选择${currentCharacterCustomConfig?.minHobbyNum ?? 1}个爱好",
      );
      return;
    }
    AtmobLog.d(
      tag,
      "clickSave hobbiesSelectLabels: ${hobbiesSelectLabels.toJson()}",
    );
    Get.back(result: hobbiesSelectLabels);
  }

  /// 选择爱好标签
  void selectHobby(Hobbies hobby) {
    handleSelection(
      name: hobby,
      selectedList: hobbiesSelectLabels,
      max: currentCharacterCustomConfig?.maxHobbyNum ?? 3,
      errorMessage: "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
    );
  }

  ///标签选择处理
  void handleSelection({
    required dynamic name,
    required RxList<dynamic> selectedList,
    required int max,
    required String errorMessage,
  }) {
    if (selectedList.contains(name)) {
      selectedList.remove(name);
    } else if (selectedList.length < max) {
      selectedList.add(name);
    } else {
      ToastUtil.show(errorMessage);
    }
  }

  void clickHobbiesCustom() {
    AtmobLog.d(tag, "clickHobbiesCustom");
    CustomLabelDialog.show(
      title: StringName.addHobbies,
      maxLength: currentCharacterCustomConfig?.maxHobbyWords ?? 5,
      hintText: StringName.customLabelHobbiesHint,
      clickCallback: (value) {
        final isExist = hobbiesLabelsList.map((e) => e.name).contains(value);
        if (isExist) {
          ToastUtil.show("添加失败，标签 $value 已存在");
          return;
        }

        final newHobby = Hobbies(name: value);
        hobbiesLabelsList.add(newHobby);

        // 如果当前已选未超限，自动选中
        if (hobbiesSelectLabels.length <
            (currentCharacterCustomConfig?.maxHobbyNum ?? 3)) {
          hobbiesSelectLabels.add(newHobby);
        } else {
          ToastUtil.show(
            "最多选择${currentCharacterCustomConfig?.maxHobbyNum ?? 3}个爱好",
          );
        }
      },
    );
  }
}
