import { SHOP_REFRESH_TYPE, TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityRefreshTaskModelType } from '../../db/ActivityRefreshTask';
import { ActivityRefreshTaskPointModelType } from '../../db/ActivityRefreshTaskPoint';
import { getZeroPoint } from '../../pubUtils/timeUtil';
import { parseNumberList } from '../../pubUtils/util';
import { ActivityBase } from './activityField';

interface RefreshTaskDataInDb {
    refreshType: number;
    name: string;
    consumePoint: number;
    reward: string;
    data: RefreshTaskPageInDb[];
}

interface RefreshTaskPageInDb {
    pageIndex: number;
    name: string;
    items: RefreshTaskItemInDb[];
}

interface RefreshTaskItemInDb {
    id: number;
    name: string;
    taskType: number;
    taskParam: string;
    condition: number;
    reward: string;
    point: number;
    skip: string;
}

// 任务配置数据
export class RefreshTaskItem {
    pageIndex: number;       // 第几页
    id: number;             // id
    name: string;           // 任务名称
    taskType: number;       // 任务类型 dic_zyz_taskType.json
    taskParam: string;      //任务数据 dic_zyz_taskType.json
    condition: number;      //任务数据条件 dic_zyz_taskType.jsonT
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    skip: string;      //跳转客户端用
    point: number;      //奖励的点数

    taskParamArray: number[] = [];
    totalCount: number = 0; //完成任务累计次数
    receiveRewardCount: number = 0;          //领取奖励次数
    refreshType: SHOP_REFRESH_TYPE = SHOP_REFRESH_TYPE.FOREVER;

    constructor(data: RefreshTaskItemInDb, pageIndex: number, refreshType: number) {
        this.pageIndex = pageIndex;
        this.id = data.id;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.condition = data.condition;
        this.reward = data.reward;
        this.skip = data.skip;
        this.point = data.point;
        this.totalCount = 0;
        this.receiveRewardCount = 0;
        this.taskParamArray = parseNumberList(data.taskParam);
        this.refreshType = refreshType;
    }

    public getRefTime() {
        return getZeroPoint(this.refreshType);
    }
}

// 任务配置数据
export class RefreshTaskPage {
    pageIndex: number;       // 第几页
    name: string;           // 任务名称
    items: Array<RefreshTaskItem> = [];

    constructor(data: RefreshTaskPageInDb, refreshType: number) {
        this.pageIndex = data.pageIndex;
        this.name = data.name;
        for (let obj of data.items) {
            this.items.push(new RefreshTaskItem(obj, data.pageIndex, refreshType));
        }
    }
}

// 任务活动数据
export class RefreshTaskData extends ActivityBase {
    name: string = '';//活动名称
    list: Array<RefreshTaskPage> = [];

    consumePoint: number = 0;//消耗点数兑换奖品
    reward: string = ''//消耗点数的奖品内容

    totalPoint: number = 0;//获得的总点数
    exchangePoint: number = 0;//兑换了点数奖品

    public findItem(pageIndex: number, id: number, type: TASK_TYPE) {
        let index = this.list.findIndex(obj => { return obj && obj.pageIndex === pageIndex });
        if (index != -1) {
            let itemIndex = this.list[index].items.findIndex(obj => { return obj && obj.id == id && obj.taskType == type });
            if (itemIndex != -1) {
                return this.list[index].items[itemIndex];
            }
        }
        return null
    }

    public findTaskByType(type: TASK_TYPE) {
        let arr: RefreshTaskItem[] = [];
        for (let pageData of this.list) {
            let items = pageData.items.filter(item => { return item.taskType == type });
            arr.push(...items);
        }
        return arr;
    }

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityRefreshTaskModelType[], pointRecordData: ActivityRefreshTaskPointModelType) {
        for (let pageData of this.list) {
            for (let item of pageData.items) {
                let refTime = item.getRefTime();
                let index = data.findIndex(record => { return item.id == record.id && item.pageIndex == record.pageIndex && item.taskType == record.taskType && refTime == record.refTime});
                if (index != -1) {
                    item.totalCount = data[index].totalCount ? data[index].totalCount : 0;
                    item.receiveRewardCount = data[index].receiveRewardCount ? data[index].receiveRewardCount : 0;
                }
            }
        }

        this.totalPoint = pointRecordData && pointRecordData.totalPoint ? pointRecordData.totalPoint : 0;
        let pointRecords = pointRecordData && pointRecordData.records ? pointRecordData.records : [];
        for (let record of pointRecords) {
            this.exchangePoint += record.point;
        }
    }

    public initData(data: string) {
        // this.nextRefreshTime = this.endTime;
        let dataObj: RefreshTaskDataInDb = JSON.parse(data);
        this.name = dataObj.name;
        this.consumePoint = dataObj.consumePoint;
        this.reward = dataObj.reward;
        this.totalPoint = 0;
        this.exchangePoint = 0;
        let arr = dataObj.data;
        for (let obj of arr) {
            this.list.push(new RefreshTaskPage(obj, dataObj.refreshType))
        }
    }

    constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
        super(activityData, createTime, serverTime)
        this.initData(activityData.data)
    }
}