/// <reference path="../../picker/definition.d.ts" />
/// <reference path="./definition.d.ts" />

/**
 * 某月的总天数，从0开始
 */
export function getDaysInMonth(year: number, month: number, type: 'object' | 'array'): any[] {
  const thirtyOneDays = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
  const thirty = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
  const twentyNine = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
  const twentyEight = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28];

  switch (month) {
    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11:
      if (type === 'object') {
        return thirtyOneDays.map((value) => {
          return { value, label: value };
        });
      }
      return thirtyOneDays;
    case 1:
      if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) {
        if (type === 'object') {
          return twentyNine.map((value) => {
            return { value, label: value };
          });
        }
        return twentyNine;
      }
      if (type === 'object') {
        return twentyEight.map((value) => {
          return { value, label: value };
        });
      }
      return twentyEight;
    case 3:
    case 5:
    case 8:
    case 10:
      if (type === 'object') {
        return thirty.map((value) => {{
          return { value, label: value };
        }});
      }
      return thirty;
    default: break;
  }
}

/**
 * 蔡勒公式，month从0开始
 */
export function zellerFormula(year: number, month: number, day: number): number {
  let yearString = year.toString();
  let m = month + 1;
  const d = day;
  if (m < 3) {
    m += 12;
    yearString = (year - 1).toString();
  }
  const c = parseInt(yearString.substr(0, 2));
  const y = parseInt(yearString.substr(2, 3));
  const result = parseInt((y + y / 4).toString()) + parseInt((c / 4).toString()) - 2 * c + parseInt((26 * (m + 1) / 10).toString()) + d - 1;

  return (result % 7 + 7) % 7;
}

/**
 * 创建年份的数组
 */
export function createYearArray(startYear: number, endYear: number, type: 'object' | 'array'): number[] {
  const temp = [];
  let i = startYear;
  for (i; i <= endYear; i++) {
    if (type === 'object') {
      temp.push({ value: i, label: i });
    } else {
      temp.push(i);
    }
  }
  return temp;
}

/**
 * 创建月份的数组
 */
export function createMonthArray(type: 'object' | 'array'): number[] {
  const temp = [];
  let i = 0;
  for (i; i < 12; i++) {
    if (type === 'object') {
      temp.push({ value: i, label: i });
    } else {
      temp.push(i);
    }
  }
  return temp;
}

/**
 * 创建小时的数组
 */
export function createHoursArray(type: 'object' | 'array'): number[] {
  const temp = [];
  let i = 0;
  for (i; i < 24; i++) {
    if (type === 'object') {
      temp.push({ value: i, label: i });
    } else {
      temp.push(i);
    }
  }
  return temp;
}

/**
 * 创建分秒的数组
 */
export function createMinutesAndSecondsArray(type: 'object' | 'array'): number[] {
  const temp = [];
  let i = 0;
  for (i; i < 60; i++) {
    if (type === 'object') {
      temp.push({ value: i, label: i });
    } else {
      temp.push(i);
    }
  }
  return temp;
}

/**
 * 获取默认值，mode: yyMMddHHmm
 */
export function getDefaultValue(timestamp?: number, type?: 'string' | 'array'): TspComponentPickerValues[] {
  const date = timestamp ? new Date(timestamp) : new Date();
  const year = valueFormat(date.getFullYear(), 'string');
  const month = valueFormat(date.getMonth() + 1, 'string');
  const day = valueFormat(date.getDate(), 'string');
  const hour = valueFormat(date.getHours(), 'string');
  const min = valueFormat(date.getMinutes(), 'string');
  const sec = valueFormat(date.getSeconds(), 'string');

  if (type === 'string') {
    return `${year}-${month}-${day} ${hour}:${min}:${sec}` as any;
  } else {
    return [{
      value: year
    }, {
      value: month
    }, {
      value: day
    }, {
      value: hour
    }, {
      value: min
    }, {
      value: sec
    }];
  }
}

/**
 * 对小于10的数添加0
 */
export function valueFormat(value: number | string, type: 'object' | 'string', unit?: string): any {
  let result;
  let label;

  if (value < 10) {
    result = '0' + value;
  } else {
    result = value.toString();
  }

  label = unit ? result + unit : result;

  if (type === 'object') {
    return { value: result, label };
  } else {
    return label;
  }
}

/**
 * 获得今天的日期
 */
export function getTodayDate(mode?: 'yy-MM' | 'yy-MM-dd'| 'yy-MM-dd HH:mm', type?: 'string' | 'array'): any {
  const date = new Date();
  const year = valueFormat(date.getFullYear(), 'string');
  const month = valueFormat(date.getMonth() + 1, 'string');
  const day = valueFormat(date.getDate(), 'string');
  const hour = valueFormat(date.getHours(), 'string');
  const minute = valueFormat(date.getMinutes(), 'string');

  switch (mode) {
    case 'yy-MM': return type === 'string' ? `${year}-${month}` : [year, month];
    case 'yy-MM-dd': return type === 'string' ? `${year}-${month}-${day}` : [year, month, day];
    case 'yy-MM-dd HH:mm': return type === 'string' ? `${year}-${month}-${day} ${hour}:${minute}` : [year, month, day, hour, minute];
    default: return [year, month, day];
  }
}

/**
 * 字符串数组格式化日期字符串
 */
export function arrayFormatDateString(value: string[], mode: 'yy-MM' | 'yy-MM-dd' | 'yy-MM-dd HH:mm'): string {
  switch (mode) {
    case 'yy-MM':
    case 'yy-MM-dd':
      return value.join('-');
    case 'yy-MM-dd HH:mm':
      return value.slice(0, 3).join('-') + ' ' + value.slice(3).join(':');
    default: break;
  }
}

/**
 * 日期字符串格式化数组
 */
export function dateStringFormatArray(value: string, mode: 'yy-MM' | 'yy-MM-dd' | 'yy-MM-dd HH:mm'): any[] {
  switch (mode) {
    case 'yy-MM':
    case 'yy-MM-dd':
      return value.split('-');
    case 'yy-MM-dd HH:mm':
      return value.substr(0, 10).split('-').concat(value.substr(11).split(':')) ;
    default: break;
  }
}

/**
 * 日期字符串格式化DatePicker的value值
 */
export function dateStrignFormatDatePickerValue(dateString: string, mode: 'yy-MM' | 'yy-MM-dd' | 'yy-MM-dd HH:mm'): any[] {
  return dateStringFormatArray(dateString, mode).map((value) => {
    return { value };
  });
}

/**
 * 某天是否在一个日期范围内
 */
export function isDayInDateRange(range: DateUtilDateRange, startStr: string, endStr?: string): boolean {
  const date = new Date(startStr);
  const time = date.getTime();
  let dateRange;

  switch (range) {
    case 'today':
    case 'yesterday': return startStr === getDateStrByDateRange(range).startDate.str;
    case 'month':
      const todayStr = getDateStrByDateRange('today').startDate.str;
      return startStr.substr(0, 7) === todayStr.substr(0, 7);
    case 'prevWeek':
      dateRange = getDateStrByDateRange('prevWeek');
      return startStr === dateRange.startDate.str && endStr === dateRange.endDate.str;
    case 'prevMonth':
      dateRange = getDateStrByDateRange('prevMonth');
      return startStr === dateRange.startDate.str && endStr === dateRange.endDate.str;
    default:
      dateRange = getDateStrByDateRange(range);
      const startTime = dateRange.startDate.time;
      const endTime = dateRange.todayDate.time;
      return time >= startTime && time <= endTime && time !== endTime;
  }
}

/**
 * 根据日期范围获取日期字符串
 */
export function getDateStrByDateRange(range?: string | number): getDateStrByDateRangeReturn  {
  let todayDate = new Date();
  const todayWeek = todayDate.getDay();
  const todayYear = todayDate.getFullYear().toString();
  const todayMonth = valueFormat((todayDate.getMonth() + 1).toString(), 'string');
  const todayDay = valueFormat(todayDate.getDate().toString(), 'string');
  todayDate = new Date(`${todayYear}-${todayMonth}-${todayDay}`);
  const todayTime = todayDate.getTime();
  let [startDate, endDate] = [undefined, undefined];

  if (typeof range === 'string') {
    switch (range) {
      case 'today':
        startDate = todayDate;
        endDate = todayDate;
        break;
      case 'yesterday':
        startDate = new Date(todayTime - 3600 * 24 * 1000);
        endDate = startDate;
        break;
      case 'week':
        startDate = new Date(todayTime - 3600 * 24 * 1000 * (todayWeek - 1));
        endDate = todayDate;
        break;
      case 'month':
        startDate = new Date(`${todayYear}-${todayMonth}-01`);
        endDate = todayDate;
        break;
      case 'prevWeek':
        const startTimeStamp = todayTime - 3600 * 24 * 1000 * todayWeek - 3600 * 24 * 1000 * 6;
        startDate = new Date(startTimeStamp);
        endDate = new Date(startTimeStamp + 3600 * 24 * 1000 * 6);
        break;
      case 'prevMonth':
        startDate = new Date(parseInt(todayYear), parseInt(todayMonth) - 2);
        endDate = new Date(parseInt(todayYear), parseInt(todayMonth) - 1, 0);
        break;
      default: break;
    }
  } else {
    startDate = new Date(todayTime - 3600 * 24 * 1000 * (range - 1));
    endDate = todayDate;
  }

  const startYear = startDate.getFullYear().toString();
  const startMonth = valueFormat((startDate.getMonth() + 1).toString(), 'string');
  const startDay = valueFormat(startDate.getDate().toString(), 'string');
  const startTime = startDate.getTime();
  const endYear = endDate.getFullYear().toString();
  const endMonth = valueFormat((endDate.getMonth() + 1).toString(), 'string');
  const endDay = valueFormat(endDate.getDate().toString(), 'string');
  const endTime = endDate.getTime();

  return {
    startDate: { str: `${startYear}-${startMonth}-${startDay}`, time: startTime },
    endDate: { str: `${endYear}-${endMonth}-${endDay}`, time: endTime },
    todayDate: { str: `${todayYear}-${todayMonth}-${todayDay}`, time: todayTime },
  };
}

/**
 * 时间戳转小时分钟
 */
export function timestampToHour(timestamp: number): number[] {
  const hour = parseInt((timestamp / 3600000).toString());
  const minus = parseInt(((timestamp - 3600000 * hour) / 60000).toString());

  return [hour, minus];
}