const { calculatePlumBlossom, calculateBoneWeight, HEAVEN_STEMS, EARTH_BRANCHES, LUNAR_MONTH_NAMES, LUNAR_DAY_NAMES, HOUR_NAMES } = require('../../utils/core/index.js');

Page({
  data: {
    plumDate: new Date().toISOString().split('T')[0],
    plumTime: '12:00',
    plumResult: null,
    yearGanzhiList: [],
    monthList: LUNAR_MONTH_NAMES,
    dayList: LUNAR_DAY_NAMES,
    hourList: HOUR_NAMES,
    boneYearIndex: 0,
    boneMonthIndex: 0,
    boneDayIndex: 0,
    boneHourIndex: 0,
    boneResult: null
  },

  onLoad() {
    const yearGanzhiList = [];
    for (let i = 0; i < 60; i++) {
      yearGanzhiList.push(HEAVEN_STEMS[i % 10] + EARTH_BRANCHES[i % 12]);
    }
    this.setData({ yearGanzhiList });
  },

  onPlumDateChange(e) {
    this.setData({ plumDate: e.detail.value });
  },

  onPlumTimeChange(e) {
    this.setData({ plumTime: e.detail.value });
  },

  calcPlum() {
    const { plumDate, plumTime } = this.data;
    const [year, month, day] = plumDate.split('-').map(Number);
    const [hour] = plumTime.split(':').map(Number);
    const result = calculatePlumBlossom(year, month, day, hour);
    this.setData({ plumResult: result });
  },

  onBoneYearChange(e) {
    this.setData({ boneYearIndex: e.detail.value });
  },

  onBoneMonthChange(e) {
    this.setData({ boneMonthIndex: e.detail.value });
  },

  onBoneDayChange(e) {
    this.setData({ boneDayIndex: e.detail.value });
  },

  onBoneHourChange(e) {
    this.setData({ boneHourIndex: e.detail.value });
  },

  calcBone() {
    const { boneYearIndex, boneMonthIndex, boneDayIndex, boneHourIndex } = this.data;
    const result = calculateBoneWeight(boneYearIndex, boneMonthIndex + 1, boneDayIndex + 1, boneHourIndex);
    this.setData({ boneResult: result });
  }
});
