import {useState} from "react";



export interface CronDayTimeProps
{
    hour :number,
    minute :number,
}

export interface CronDefinDayProps
{
    monDay:boolean,
    tuesDay:boolean,
    wednesDay:boolean,
    thursday:boolean,
    friday:boolean,
    saturday:boolean,
    sunday:boolean,
    cronDayTime : CronDayTimeProps
}

const useCreateCronExpression = ( ) => {

    const [cronExpression, setCronExpression] = useState<string>("");
    const [cronExpressionText, setCronExpressionText] = useState<string>("")

    const createCronEveryMinExpression =  (min:number) => {
        setCronExpression(`0 0/${min} * 1/1 * ?`);
        setCronExpressionText(`${min}분 마다 실행`);
    };

    const createCronEveryHourExpression =  (hour:number) => {
        setCronExpression(`0 0 0/${hour} 1/1 * ?`);
        setCronExpressionText(`${hour}시간 마다 실행`);
    };

    const createCronEveryDayExpression =  (props:CronDayTimeProps) => {
        setCronExpression(`0 ${props.minute} ${props.hour} 1/1 * ?`);
        setCronExpressionText(`매일 ${props.hour}시 ${props.minute}분마다 실행`);
    };

    const createCronEveryWeekDayExpression =  (props:CronDayTimeProps) => {
        setCronExpression(`0 ${props.minute} ${props.hour} ? * MON-FRI`);
        setCronExpressionText(`평일 ${props.hour}시 ${props.minute}분마다 실행`);
    };

    const createCronEveryWeekEndExpression =  (props:CronDayTimeProps) => {
        setCronExpression(`0 ${props.minute} ${props.hour} ? * SAT-SUN`);
        setCronExpressionText(`주말 ${props.hour}시 ${props.minute}분마다 실행`);
    };

    const initExpression =  () => {
        setCronExpression("");
        setCronExpressionText("");
    };

    const createCronDefineDayExpression =  (props:CronDefinDayProps) => {
        const {monDay, tuesDay, wednesDay, thursday, friday, saturday, sunday, cronDayTime} = props;
        let cron:string = "";
        let cronText:string = "";
        if (monDay)  {
            if (cron.length === 0)   cron = "MON";
            else cron += ",MON";

            if (cronText.length === 0)   cronText = "월";
            else cronText += ",월";
        }
        if (tuesDay)  {
            if (cron.length === 0)   cron = "TUE";
            else cron += ",TUE";

            if (cronText.length === 0)   cronText = "화";
            else cronText += ",화";
        }
        if (wednesDay)  {
            if (cron.length === 0)   cron = "WED";
            else cron += ",WED";

            if (cronText.length === 0)   cronText = "수";
            else cronText += ",수";
        }
        if (thursday)  {
            if (cron.length === 0)   cron = "THU";
            else cron += ",THU";

            if (cronText.length === 0)   cronText = "목";
            else cronText += ",목";
        }
        if (friday)  {
            if (cron.length === 0)   cron = "FRI";
            else cron += ",FRI";

            if (cronText.length === 0)   cronText = "금";
            else cronText += ",금";
        }
        if (saturday)  {
            if (cron.length === 0)   cron = "SAT";
            else cron += ",SAT";

            if (cronText.length === 0)   cronText = "토";
            else cronText += ",토";
        }
        if (sunday)  {
            if (cron.length === 0)   cron = "SUN";
            else cron += ",SUN";

            if (cronText.length === 0)   cronText = "일";
            else cronText += ",일";
        }

        setCronExpression(`0 ${cronDayTime.minute} ${cronDayTime.hour} ? * ${cron}`);
        setCronExpressionText(`${cronText}요일 ${cronDayTime.hour}시 ${cronDayTime.minute}분마다 실행`);
    };

    return { cronExpression, cronExpressionText
        , createCronEveryMinExpression
        , createCronEveryHourExpression
        , createCronEveryDayExpression
        , createCronEveryWeekDayExpression
        , createCronEveryWeekEndExpression
        , createCronDefineDayExpression
        , initExpression
    };
};

export default useCreateCronExpression;