import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:keyboard/utils/toast_util.dart';

import '../../../../data/bean/default_avatar_info.dart';
import '../../../../data/repository/config_repository.dart';
import '../../../../dialog/select_birthday_dialog.dart';
import '../../../../utils/age_zodiac_sign_util.dart';
import '../../../../utils/atmob_log.dart';

mixin StepPartnerLogic {
  final tag = "StepPartnerLogic";

  final ConfigRepository configRepository = ConfigRepository.getInstance();

  Rxn<DefaultAvatarInfo> get currentDefaultAvatarInfo =>
      configRepository.defaultAvatarInfo;

  final RxString _partnerAvatarUrl = "".obs;

  String get partnerAvatarUrl => _partnerAvatarUrl.value;

  final partnerName = "".obs;

  // 加这个controller是因为在防止在返回其他页面时，输入框的内容会被清空
  TextEditingController partnerNameController = TextEditingController();

  final RxnInt _partnerGender = RxnInt(null);

  int? get partnerGender => _partnerGender.value;

  final RxList<String> _girlAvatars = <String>[].obs;

  final RxList<String> _boyAvatars = <String>[].obs;

  final RxString _currentPartnerDate = "".obs;

  String get currentPartnerDate => _currentPartnerDate.value;

  final Rxn<DateTime> _currentPartnerBirthday = Rxn<DateTime>(null);

  DateTime? get currentPartnerBirthday => _currentPartnerBirthday.value;


  DateComponents get dateComponents =>
      DateComponents.fromDateTime(_currentPartnerBirthday.value!);


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

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

  void updateAvatarListsAndSelectFirst(DefaultAvatarInfo? info) {
    _boyAvatars.assignAll(info?.maleAvatars ?? []);
    _girlAvatars.assignAll(info?.femaleAvatars ?? []);
    if (_partnerGender.value == 1) {
      _partnerAvatarUrl.value = _boyAvatars.isNotEmpty ? _boyAvatars.first : "";
    } else {
      _partnerAvatarUrl.value = _girlAvatars.isNotEmpty ? _girlAvatars.first : "";
    }
  }

  void onNicknameChange(String name) {
    AtmobLog.d(tag, "onNicknameChange: $name");
    if (name.isNotEmpty) {
      partnerName.value = name;
    } else {
      partnerName.value = "";
    }
  }

  void onPartnerGenderChanged(int gender) {
    AtmobLog.d(tag, gender.toString());
    _partnerGender.value = gender;

    // 切换性别时自动切换对应性别的第一个头像
    if (gender == 1 && _boyAvatars.isNotEmpty) {
      _partnerAvatarUrl.value = _boyAvatars.first;
    } else if (_girlAvatars.isNotEmpty) {
      _partnerAvatarUrl.value = _girlAvatars.first;
    } else {
      _partnerAvatarUrl.value = "";
    }

  }

  void changePartnerBirthday() {

    SelectBirthdayDialog.show(
      initialDate: _currentPartnerBirthday.value ?? DateTime.now(),
      minimumDate: DateTime.parse("1921-01-01"),
      onDateChanged: (dataTime) {
        _currentPartnerBirthday.value = dataTime;
      },
    );
  }


}
