
import { ReturnCurrentDateValue, ReturnFirstDayOfMonth, ReturnMiladyDate, ReturnNext10Year, ReturnNext4Year, ReturnNextDay, ReturnNextMonth, ReturnNextSeason, ReturnNextWeek, ReturnNextYear, ReturnPrevious10Year, ReturnPrevious4Year, ReturnPreviousDay, ReturnPreviousMonth, ReturnPreviousSeason, ReturnPreviousWeek, ReturnPreviousYear, ReturnToday } from 'nums2persian';
import { IMainStateFactory } from '../../Types';

type ITypeShowDatePicker = 'days' | 'months' | 'years';
export interface IDateBoxFactory {
    value: string;
    setValue: (value: string) => void;
    mainStateFactory: IMainStateFactory;
}
export class DatePickerFactory {
    public element?: HTMLElement;

    public get any(): any {
        return this;
    }

    public get today(): string {
        return this.selfDateBoxFactory.value ? this.selfDateBoxFactory.value : ReturnToday();
    }

    public currentTypeOfShowDatePicker: ITypeShowDatePicker;
    public currentDateMiladi: string;
    public _currentDate: string;
    public currentYear: number;
    public currentMonth: number;
    public currentDay: number;

    public get currentDate() {
        return this._currentDate;
    }
    public set currentDate(value: string) {
        this._currentDate = value;
        const [year, month, day] = ReturnCurrentDateValue(this._currentDate);
        this.currentDay = day;
        this.currentMonth = month;
        this.currentYear = year;
    }

    constructor(
        public selfDateBoxFactory: IDateBoxFactory,
        public forceUpdate: () => void,
    ) {
        this.currentDateMiladi = '';
        this.currentTypeOfShowDatePicker = 'days';
        this._currentDate = this.today;
        const [year, month, day] = ReturnCurrentDateValue(this._currentDate);
        this.currentDay = day;
        this.currentMonth = month;
        this.currentYear = year;
    }

    ///** Select And Close */
    public onSelectDate = (date: string) => {
        this.currentDate = date;
        this.selfDateBoxFactory.setValue(this.currentDate);
        this.selfDateBoxFactory.mainStateFactory.elementsOfForm.closeModal();
    }

    ///** Only Select */
    public onSelectDateOnly = (date: string) => {
        this.currentDate = date;
        this.selfDateBoxFactory.setValue(this.currentDate);
    }

    public setMiladiDate = (date: string) => {
        this.currentDateMiladi = date;
        this.forceUpdate();
    }

    public setCurrentDate = (date: string) => {
        this.currentDate = date;
        this.forceUpdate();
    }
    public setCurrentTypeShow = (type: ITypeShowDatePicker) => {
        this.currentTypeOfShowDatePicker = type;
        this.forceUpdate();
    }


    public onSelectMonth = (newMonth: number) => {
        this.setCurrentDate(ReturnFirstDayOfMonth(this.currentYear, newMonth));
        this.setCurrentTypeShow('days');
    };
    public onSelectYear = (newYear: number) => {
        this.setCurrentDate(ReturnFirstDayOfMonth(newYear, this.currentMonth));
        this.setCurrentTypeShow('months');
    };

    public onMouseEnter = (date: string) => {
        this.setMiladiDate(ReturnMiladyDate(date));
    };
    public onMouseOut = () => {
        this.setMiladiDate('');
    };

    public onClickTypeShowMonth = () => {
        this.setCurrentTypeShow('months');
    };
    public onClickTypeShowYears = () => {
        this.setCurrentTypeShow('years');
    };
    public onClickTypeShowDays = () => {
        this.setCurrentTypeShow('days');
    };

    public gotoNextDay = () => {
        this.setCurrentDate(ReturnNextDay(this.currentDate));
    };
    public gotoPreviousDay = () => {
        this.setCurrentDate(ReturnPreviousDay(this.currentDate));
    };

    public gotoNextWeek = () => {
        this.setCurrentDate(ReturnNextWeek(this.currentDate));
    };
    public gotoPreviousWeek = () => {
        this.setCurrentDate(ReturnPreviousWeek(this.currentDate));
    };

    public gotoNextMonth = () => {
        this.setCurrentDate(ReturnNextMonth(this.currentDate));
    };
    public gotoPreviousMonth = () => {
        this.setCurrentDate(ReturnPreviousMonth(this.currentDate));
    };

    public gotoNextSeason = () => {
        this.setCurrentDate(ReturnNextSeason(this.currentDate));
    };
    public gotoPreviousSeason = () => {
        this.setCurrentDate(ReturnPreviousSeason(this.currentDate));
    };

    public gotoToday = () => {
        this.setCurrentDate(ReturnToday());
        this.setCurrentTypeShow('days');
    };

    public gotoNextYear = () => {
        this.setCurrentDate(ReturnNextYear(this.currentDate));
    };
    public gotoPreviousYear = () => {
        this.setCurrentDate(ReturnPreviousYear(this.currentDate));
    };

    public gotoNext4Year = () => {
        this.setCurrentDate(ReturnNext4Year(this.currentDate));
    };
    public gotoPrevious4Year = () => {
        this.setCurrentDate(ReturnPrevious4Year(this.currentDate));
    };

    public gotoNext10Year = () => {
        this.setCurrentDate(ReturnNext10Year(this.currentDate));
    };
    public gotoPrevious10Year = () => {
        this.setCurrentDate(ReturnPrevious10Year(this.currentDate));
    };

    public onEnterKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.onSelectDate(this.currentDate);
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.onClickTypeShowDays();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.onClickTypeShowMonth();
        }
    };

    public onSpaceKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.onClickTypeShowYears();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.onClickTypeShowDays();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.onClickTypeShowMonth();
        }
    };

    public onDownKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoNextWeek();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoNextSeason();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoNext4Year();
        }
    };

    public onUpKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoPreviousWeek();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoPreviousSeason();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoPrevious4Year();
        }
    };

    public onRightKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoPreviousDay();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoPreviousMonth();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoPreviousYear();
        }
    };

    public onLeftKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoNextDay();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoNextMonth();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoNextYear();
        }
    };

    public onPageUpKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoNextMonth();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoNextYear();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoNext10Year();
        }
    };

    public onPageDownKeyHandler = () => {
        if (this.currentTypeOfShowDatePicker === 'days') {
            this.gotoPreviousMonth();
        } else if (this.currentTypeOfShowDatePicker === 'months') {
            this.gotoPreviousYear();
        } else if (this.currentTypeOfShowDatePicker === 'years') {
            this.gotoPrevious10Year();
        }
    };

}