import { abstractDataStrategyLeft } from "./abstractDataStrategyLeft";
import paramChecker from "../../../../util/paramChecker";
import excelSerialToJSDate from "../../../../util/excelDateToJSDate";

export default class ticketAnalysisStrategy extends abstractDataStrategyLeft {
    static readonly FILENAME = '票务系统.xlsx';
    static readonly SHEETNAME = '票务系统-订单主表';

    execute(params?: any): any {
        paramChecker.checkDiscreteParam(params, 'category', 'total', 'group', 'solo');
        paramChecker.checkDiscreteParam(params, 'timeFrame', '12month', '3year');
        paramChecker.checkDiscreteParam(params, 'type', 'amount', 'ticketNumber');
        paramChecker.checkDiscreteParam(params, 'ticketCategory', 'sales', 'verified', 'refund', 'conversion');
        const data = this.extractor.getData(ticketAnalysisStrategy.FILENAME, ticketAnalysisStrategy.SHEETNAME);
        return this.processData(data, params.query['category'], params.query['timeFrame'], params.query['type'], params.query['ticketCategory']);
    }

    processData(data: any, category: string, timeFrame: string, type: string, ticketCategory: string): any {
        return this.processTrendData(data, timeFrame);
    }


    private processTrendData(data: any, timeFrame: string): any {
        const currentDate = new Date();
        const startMonth = currentDate.getMonth() + 1;
        let startDate = new Date();
        startDate.setMonth(startMonth - 1);
        startDate.setFullYear(currentDate.getFullYear());

        const trendData = [];
        for (let line = 1; line <= 3; line++) {
            if (line > 1 && timeFrame === '12month') break;
            const monthlyTrend: Array<{ key: string, value: number }> = [];
            for (let i = 0; i < 12; i++) {
                const date = new Date(startDate);
                date.setMonth(startDate.getMonth() + i - (line - 1) * 12);
                const month = (date.getMonth() + 1).toString().padStart(2, '0'); // Format the month to MM
                monthlyTrend.push({ key: month, value: Math.floor(Math.random() * 100) + 20 });
            }

            // monthlyTrend.sort((a, b) => parseInt(a.key) - parseInt(b.key));

            trendData.push({
                line,
                monthlyTrend: monthlyTrend
            });
        }

        return trendData;
    }


}
