import { TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityRefreshTaskModelType } from '../../db/ActivityRefreshTask';
import { ActivityRefreshTaskPointModelType } from '../../db/ActivityRefreshTaskPoint';
import { ActivityBase } from './activityField';


// 任务配置数据
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: number;      //跳转客户端用
    point: number;      //奖励的点数

    totalCount: number = 0; //完成任务累计次数
    receiveRewardCount: number = 0;          //领取奖励次数

    constructor(data: any, pageIndex: 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;
    }
}

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

    constructor(data: any) {
        this.pageIndex = data.pageIndex;
        this.name = data.name;
        this.items = [];
        for (let obj of data.items) {
            this.items.push(new RefreshTaskItem(obj, data.pageIndex))
        }
    }
}

// 任务活动数据
export class RefreshTaskData extends ActivityBase {
    name: string = '';//活动名称
    interval: number = 0;//周期间隔（秒）
    list: Array<RefreshTaskPage> = [];
    nextRefreshTime: number;//下次刷新时间
    roundIndex: number = 1;//周期数从1开始
    addPointActivityId: number = 0;//获得的点数，关联其他活动id(新将礼物)

    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 = [];
        for (let pageData of this.list) {
            let items = pageData.items.filter(item => { return item.taskType == type });
            arr = arr.concat(items)
        }
        return arr;
    }

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityRefreshTaskModelType[], pointRecordData: ActivityRefreshTaskPointModelType) {
        for (let pageData of this.list) {
            for (let item of pageData.items) {
                let index = data.findIndex(record => { return item.id == record.id && item.pageIndex == record.pageIndex && item.taskType == record.type })
                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 = JSON.parse(data);
        this.name = dataObj.name;
        this.interval = dataObj.interval;
        this.consumePoint = dataObj.consumePoint;
        this.reward = dataObj.reward;
        this.addPointActivityId = dataObj.addPointActivityId ? dataObj.addPointActivityId : 0;
        this.totalPoint = 0;
        this.exchangePoint = 0;

        // this.beginTime = moment(this.beginTime).startOf('d').add(REFRESH_TIME, 'hour').valueOf();
        // if (this.interval > 0) {
        //     this.roundIndex = Math.ceil((moment(new Date).valueOf() - this.beginTime) / (this.interval * 1000));
        //     this.nextRefreshTime = moment(this.beginTime).add(this.interval * this.roundIndex, 'second').valueOf();
        // }
        // console.log('ddddddddddddbbbbbbb', moment(new Date).valueOf(), moment(this.beginTime).valueOf(), this.roundIndex, this.nextRefreshTime)
        let arr = dataObj.data;
        for (let obj of arr) {
            this.list.push(new RefreshTaskPage(obj))
        }
    }

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