/**
 * 日历工具类
 *
 * @export
 * @class AppCalendarUtil
 */
export class AppCalendarUtil {
    /**
     * 当前时间
     *
     * @type {Date}
     * @memberof AppCalendarUtil
     */
    public readonly date: Date = new Date();
    /**
     * 当前年份
     *
     * @type {number}
     * @memberof AppCalendarUtil
     */
    public readonly year: number = this.date.getFullYear();
    /**
     * 当前月
     *
     * @type {number}
     * @memberof AppCalendarUtil
     */
    public readonly month: number = this.date.getMonth();
    /**
     * 当前天
     *
     * @type {number}
     * @memberof AppCalendarUtil
     */
    public readonly day: number = this.date.getDate();
    /**
     * 月份
     *
     * @type {string[]}
     * @memberof AppCalendarUtil
     */
    public readonly months: string[] = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    /**
     * 星期
     *
     * @type {string[]}
     * @memberof AppCalendarUtil
     */
    public readonly weeks: string[] = ['日', '一', '二', '三', '四', '五', '六'];
    /**
     * 获取指定年月的天数对应星期，每七天一个数组
     *
     * @param {number} [month=this.month] 月，默认为当前月
     * @param {number} [year=this.year] 年，默认为当前年
     * @memberof AppCalendarUtil
     */
    public getDaysCurrentMonth(month: number = this.month, year: number = this.year): Array<any>[] {
        const days: Array<any>[] = [];
        const week: number = new Date(year, month).getDay();
        const lastDate: number = this.getLastDayCurrentMonth(month);
        const lastMonthLastDay: number = this.getLastDayCurrentMonth(month - 1);
        let day: number = 0;
        let nextDay: number = 1;
        for (let num: number = 0; num < 6; num++) {
            const arr: any[] = [];
            for (let i: number = 0; i < 7; i++) {
                if ((i >= week || days.length > 0) && day < lastDate) {
                    day++;
                    arr.push({
                        year,
                        month,
                        day: day,
                        date: `${year}-${month + 1}-${day}`
                    });
                } else {
                    if (day === 0) {
                        const dayNum: number = lastMonthLastDay - (week - i) + 1;
                        arr.push({
                            year: month === 0 ? year - 1 : year,
                            month: month - 1,
                            day: dayNum,
                            date: `${year}-${month - 1 + 1}-${dayNum}`
                        });
                    } else {
                        arr.push({
                            year: month === 11 ? year + 1 : year,
                            month: month + 1,
                            day: nextDay,
                            date: `${year}-${month + 1 + 1}-${nextDay}`
                        });
                        nextDay++;
                    }
                }
            }
            days.push(arr);
        }
        return days;
    }

    /**
     * 获取当前月最后一天
     *
     * @param {number} month 月份
     * @param {number} [year=this.year] 年，默认为当前年
     * @returns {number}
     * @memberof AppCalendarUtil
     */
    public getLastDayCurrentMonth(month: number, year: number = this.year): number {
        return new Date(year, month + 1, 1 - 1).getDate();
    }

}