import dayjs from 'dayjs';
import {TypeItemStatus, TypeLayoutItem} from '../src/module-defs/ui-types';

export function range(start: number, end: number, step = 1) {
    const result = []
    for (let i = start; i <= end; i += step) {
        result.push(i)
    }
    return result
}

export const titleRows = '一二三四五六日'
    .split('')
    .map(text => ({text}))

function getDateIndex(date: number, week: number) {
    return date + week * 7 + 28
}

const dateStatusMap: Record<string, TypeItemStatus> = {
    [31 + 4]: {selected: true},
    [31 + 5]: {selected: true, disabled: true},
    [31 + 6]: {selected: true, disabled: true, today: true},
    [31 + 7]: {selected: true, today: true},
    [31 + 8]: {today: true},
    [31 + 9]: {disabled: true, today: true},
    [31 + 4 + 7]: {rangeType: 'start'},
    [31 + 4 + 7 + 7 + 2]: {rangeType: 'end', today: true},
    [31 + 4 + 7 + 7 + 3]: {today: true},
};

range(31 + 4 + 7 + 1, 31 + 4 + 7 + 7 + 1)
    .forEach(index => (dateStatusMap[index] = {rangeType: 'inside'}))

range(28, 31)
    .concat(range(31 + 30 + 1, 31 + 30 + 8))
    .forEach(index => (dateStatusMap[index] = {outside: true}))

dateStatusMap[31 + 4 + 7 + 7 + 1].today = true

export {dateStatusMap}

const getDateText = (date: number, week: number) => {
    const result = getDateIndex(date, week)
    if (result > 31 + 30) {
        return result - 31 - 30
    }
    if (result > 31) {
        return result - 31
    }
    return result
}

type TypeRows = Array<{ items: TypeLayoutItem[] }>;

export const dateRows: TypeRows = range(0, 5)
    .map(
        week => {
            const today = dayjs();

            return ({
                items: range(0, 6)
                    .map(
                        date => {
                            const dateIndex = getDateIndex(date, week)

                            return {
                                key: dateIndex,
                                text: getDateText(date, week),
                                data: {
                                    type: 'date',
                                    date: today,
                                },
                            }
                        }
                    ),
            });
        }
    )

export const monthRows: TypeRows = range(0, 3)
    .map(
        rowIndex => ({
            items: range(0, 2)
                .map(
                    colIndex => {
                        const month = rowIndex * 3 + colIndex + 1;

                        return ({
                            key: month,
                            text: `${month}月`,
                            status: 'none',
                            type: 'month',
                        });
                    }
                ),
        })
    )

export const yearAsideList = range(2019 - 12, 2019 + 12)
    .map(
        year => ({text: year, key: year})
    )

export function quarterRows() {
    return range(1, 4)
        .map(quarter => {
            return {
                items: [{
                    key: quarter,
                    text: `Q${quarter}`,
                }],
            }
        })
}
