type ValueProps = { startTime: string, endTime: string }
/**
 * @param value string[]
 * @param onChange (data: string[]) => void
 */
export interface Props {
    value?: string
    onChange?: (value?: string) => void
}

export const TIME_GAP = 5
export const TIME_LIST = Array(24 * 60 / TIME_GAP).fill('').map((_, index) => index * TIME_GAP)

// 头部分钟间隔展示
export const MINUTE_LIST = Array.from({ length: 60 / TIME_GAP }, (v, i) => i * TIME_GAP + TIME_GAP);

// 左边小时展示
export const HOUR_LIST = Array.from({ length: 24 }, (v, i) => i);

export const initTimerPicker = (value: any[] = []) => {
    const result: any[] = [];
    let index = 0;
    HOUR_LIST.forEach((hour, rowNum) => {
        const list = [];
        let hourSpan = 0;
        while (hourSpan < MINUTE_LIST.length) {
            const curHour = ('00' + hour).slice(-2);
            const startTime = ('00' + (MINUTE_LIST[hourSpan] - TIME_GAP)).slice(-2);
            const endTime = ('00' + (MINUTE_LIST[hourSpan])).slice(-2);
            const time = `${curHour}:${startTime}-${curHour}:${endTime}`;
            const selected = value[index] === '1';
            const timerBox = {
                key: `${hour}_${time}`,
                name: `${time}`,
                value: index,
                rowNum: rowNum + 1,
                colNum: (index % MINUTE_LIST.length) + 1,
                selected,
            };
            list.push(timerBox);
            if (!selected) {
                value[index] = '0';
            }
            index++;
            hourSpan++;
        }
        result.push(list);
    });
    return result;
};

export const getInitalFormatList = () => {
    return new Array(24 * (60 / TIME_GAP)).fill('0');
}

export function formatData(data: any, timeBox: any) {
    data[timeBox.value] = timeBox.selected ? '1' : '0';
    return data;
}

// 辅助函数：将分钟格式化为时间字符串
const formatMinutesToTime = (minutes: number): string => {
    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;

    const formattedHours = String(hours).padStart(2, '0');
    const formattedMinutes = String(remainingMinutes).padStart(2, '0');

    return `${formattedHours}:${formattedMinutes}:00`;
}


interface TimeInterval {
    startTime: string;
    endTime: string;
};
// 将 timeList 转换为时间区间数组
export const timeListToTimeIntervals = (timeList: string): TimeInterval[] => {
    const intervals: TimeInterval[] = [];
    let currentInterval: TimeInterval | null = null;

    for (let i = 0; i < timeList.length; i++) {
        if (timeList[i] === '1') {
            if (!currentInterval) {
                // 如果当前没有正在构建的时间区间，则创建新的时间区间
                currentInterval = { startTime: formatMinutesToTime(i * 5), endTime: '' };
            }
        } else {
            if (currentInterval) {
                // 如果当前正在构建的时间区间存在，并且遇到了 0，则表示当前时间区间结束
                let endTime = formatMinutesToTime(i * 5)
                currentInterval.endTime = endTime === '24:00:00' ? '23:59:59' : endTime;
                intervals.push(currentInterval);
                currentInterval = null; // 重置当前时间区间
            }
        }
    }

    // 处理最后一个时间区间
    if (currentInterval) {
        let endTime = formatMinutesToTime(timeList.length * 5);
        currentInterval.endTime = endTime === '24:00:00' ? '23:59:59' : endTime;
        intervals.push(currentInterval);
    }

    return intervals;
}