/**
 * 选择类型
 */
export const DateSelectType: any = [
  { value: 1, name: '今日'},
  { value: 2, name: '昨日'},
  { value: 3, name: '本周'},
  { value: 4, name: '上周'},
  { value: 5, name: '本月'},
  { value: 6, name: '上月'},
];

/**
 * 时间工具类
 */
export class DateUtils {

  /**
   * 获取时间
   * @param currentDate 当前时间
   * @param type  类型  1：今日  2：昨日  3：本周  4：上周  5：本月   6：上月
   */
  public static getDate(currentDate: Date, type: number): any {
    const returnObj = {
      timeS: 0,
      timeE: 0,
    }

    if (type === 1) {
      currentDate.setHours(0);
      currentDate.setMinutes(0);
      currentDate.setSeconds(0);
      returnObj.timeS = currentDate.getTime();
      currentDate.setHours(23);
      currentDate.setMinutes(59);
      currentDate.setSeconds(59);
      returnObj.timeE = currentDate.getTime();

    } else if (type === 2) {
      currentDate.setDate(currentDate.getDate() - 1)
      currentDate.setHours(0);
      currentDate.setMinutes(0);
      currentDate.setSeconds(0);
      returnObj.timeS = currentDate.getTime();
      currentDate.setHours(23);
      currentDate.setMinutes(59);
      currentDate.setSeconds(59);
      returnObj.timeE = currentDate.getTime();

    } else if (type === 3) {
      const dayOfWeek = currentDate.getDay();
      const dayOffset = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;

      const start = new Date(currentDate);
      start.setDate(start.getDate() + dayOffset)
      start.setHours(0);
      start.setMinutes(0);
      start.setSeconds(0);
      returnObj.timeS = start.getTime();

      const end = new Date(currentDate);
      end.setDate(end.getDate() + 6)
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      returnObj.timeE = end.getTime();

    } else if (type === 4) {
      const startOfThisWeek = new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate() - currentDate.getDay() + 1);
      const endOfLastWeek = new Date(startOfThisWeek.getTime() - 1);
      const startOfLastWeek = new Date((endOfLastWeek.getTime() + 1) - (7 * 24 * 60 * 60 * 1000));

      startOfLastWeek .setHours(0);
      startOfLastWeek .setMinutes(0);
      startOfLastWeek .setSeconds(0);
      returnObj.timeS = startOfLastWeek .getTime();

      endOfLastWeek.setHours(23);
      endOfLastWeek.setMinutes(59);
      endOfLastWeek.setSeconds(59);
      returnObj.timeE = endOfLastWeek.getTime();

    } else if (type === 5) {
      var daysInMonth = new Date(currentDate.getFullYear(), currentDate.getMonth()+1, 0).getDate();

      const start = new Date(currentDate);
      start.setDate(1)
      start.setHours(0);
      start.setMinutes(0);
      start.setSeconds(0);
      returnObj.timeS = start.getTime();

      const end = new Date(currentDate);
      end.setDate(daysInMonth)
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      returnObj.timeE = end.getTime();

    } else if (type === 6) {
      var daysInMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();

      const start = new Date(currentDate);
      start.setMonth(currentDate.getMonth() - 1);
      start.setDate(1)
      start.setHours(0);
      start.setMinutes(0);
      start.setSeconds(0);
      returnObj.timeS = start.getTime();

      const end = new Date(currentDate);
      end.setMonth(currentDate.getMonth() - 1);
      end.setDate(daysInMonth)
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      returnObj.timeE = end.getTime();
    }

    return returnObj;
  }

  /**
   * 获取时间
   * @param startTime 开始时间
   * @param endTime   结束时间
   */
  public static getDateSelectType(startTime: Date, endTime: Date): number  {
    const type = 0; // 未知
    for (let type of DateSelectType) {
       let date = this.getDate(new Date(), type.value);
       if (startTime.toLocaleDateString() == new Date(date.timeS).toLocaleDateString()
          && endTime.toLocaleDateString() == new Date(date.timeE).toLocaleDateString()) {
          return type.value;
       }
    }
    return type;
  }
}
