import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:get/get.dart';
import 'package:keyboard/utils/age_zodiac_sign_util.dart';
import 'package:intl/intl.dart';
import 'package:keyboard/utils/toast_util.dart';

import '../../../resource/assets.gen.dart';

@injectable
class ChangeBirthdayController extends BaseController {
  final tag = "ChangeBirthdayController";

  late DateTime initialDate;
  // 最小日期
  var minimumDate = DateTime.parse("1921-01-01");
  late String currentDate;

  //星座
  Rx<Zodiac> constellation = Zodiac(name: '', image: AssetGenImage('')).obs;
  var age = 0.obs;

  ChangeBirthdayController();

  @override
  void onInit() {
    super.onInit();
    final String? birthday = Get.arguments?["birthday"];
    if (birthday != null) {
      initialDate = DateTime.parse(birthday);
      currentDate = birthday;
      constellation.value = AgeZodiacSignUtil.getZodiacSign(initialDate);
      age.value = AgeZodiacSignUtil.calculateAge(initialDate);
    } else {
      initialDate = DateTime.now();
      currentDate = DateFormat('yyyy-MM-dd').format(initialDate);
      constellation.value = AgeZodiacSignUtil.getZodiacSign(initialDate);
      age.value = AgeZodiacSignUtil.calculateAge(initialDate);
    }
  }

  updateConstellation(DateTime date) {
    constellation.value = AgeZodiacSignUtil.getZodiacSign(date);
    age.value = AgeZodiacSignUtil.calculateAge(date);
    print("星座：${constellation.value}");
    print("年龄：${age.value}");
    print("日期：${date}");
    currentDate = DateFormat('yyyy-MM-dd').format(date);
  }

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

  void clickSave() {
    if (currentDate.isEmpty) {
      ToastUtil.show("请选择日期");
      return;
    }
    Get.back(result: currentDate);
  }

}
