export interface DateTime {
  year: number
  month: number
  day: number
  hours: number
  minutes: number
}

export interface DateTimePickerType {
  name: string
  id: number | string

  [key: string]: any
}


export type DateTimePickers = Array<DateTimePickerType>[]

/**
 * 获取一个月的天数
 * @param year 年
 * @param month 月
 */
const getDaysInOneMonth = function (year: number, month: number): number {
  let _month = parseInt(String(month), 10);
  let d = new Date(year, _month, 0);
  return d.getDate();
};


/**
 * 获取时间
 * @param date 时间
 */
const dateDate = function (date: Date): DateTime {
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  return {
    year, month, day, hours, minutes
  }
};


/**
 * 时间选择器
 * 获取date time  年份，月份，天数，小时，分钟推后30分
 * @param startyear 开始年
 * @param endyear 结束月
 */
const dateTimePicker = function (startyear: number, endyear: number) {
  const years: DateTimePickerType[] = [];
  const months: DateTimePickerType[] = [];
  const hours: DateTimePickerType[] = [];
  const minutes: DateTimePickerType[] = [];
  for (let i = startyear - 20; i <= endyear + 20; i++) {
    years.push({name: i + '年', id: i})
  }

  //获取月份
  for (let i: string | number = 1; i <= 12; i = Number(i) + 1) {
    if (i < 10) {
      i = "0" + i;
    }
    months.push({name: i + '月', id: i});
  }

  //获取小时
  for (let i: string | number = 0; i < 24; i = Number(i) + 1) {
    if (i < 10) {
      i = "0" + i
    }
    hours.push({name: i + '时', id: i})
  }
  //获取分钟
  for (let i: string | number = 0; i < 60; i = Number(i) + 1) {
    if (i < 10) {
      i = "0" + i;
    }
    minutes.push({
      name: i + '分',
      id: i
    });
  }
  return function (_year: number | string, _month: string | number): DateTimePickers {
    const days: DateTimePickerType[] = [];
    _year = parseInt(String(_year));
    _month = parseInt(String(_month));
    //获取日期
    for (let i: string | number = 1; i <= getDaysInOneMonth(_year, _month); i = Number(i) + 1) {
      if (i < 10) {
        i = "0" + i;
      }
      days.push({
        name: i + '日',
        id: i
      });
    }
    return [years, months, days, hours, minutes];
  }
};


/**
 * 获取时间下标
 */
function getIndex(pickerData: DateTimePickers, arr: DateTime) {
  let timeIndex: number[] = [];
  let indexKey = ["year", "month", "day", "hours", "minutes"];
  pickerData.forEach((picker, index) => {
    const _index = picker.findIndex(d => parseInt(String(d.id)) === parseInt(arr[indexKey[index]]))
    timeIndex[index] = _index >= 0 ? _index : 0;
  })
  return timeIndex;
}


/** 获取时间 */
function getTime(pickerData: DateTimePickers, arr: number[]): string {
  let time = ''
  pickerData.forEach((picker, index) => {
    if (index < 2) {
      time += picker[arr[index]].id + '-'
    } else if (index === 3) {
      time += " " + picker[arr[index]].id + ':'
    } else {
      time += picker[arr[index]].id
    }
  })
  return time
}


export {
  dateTimePicker,
  getDaysInOneMonth,
  getIndex,
  getTime,
  dateDate
}
