import { TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityGrowthModelType } from '../../db/ActivityGrowth';
import { ActivityGrowthPointModelType } from '../../db/ActivityGrowthPoint';
import { HeroType } from '../../db/Hero';
import { RoleModel } from '../../db/Role';
import { splitString } from '../../pubUtils/util';
import { ActivityBase } from './activityField';


// 积分兑换奖励数据
export class PointRewardItem {
    id: number;       // 第几天,从1开始
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    progress: number;       // 需要的奖章数量
    quality: string;      //
    star: number;      //

    getPointReward: boolean = false;       // 是否兑换领取奖章奖励

    constructor(data: any) {
        this.id = data.id;
        this.reward = data.reward;
        this.progress = data.progress;
        this.quality = data.quality;
        this.star = data.star;
        this.getPointReward = false;
    }
}

// 每日配置数据
export class GrowthItem {
    id: number;
    dayIndex: number;       // 第几天,从1开始
    cellIndex: number;      // 当天第几行，从1开始
    name: string;           // 任务名称
    taskType: number;       // 任务类型 dic_zyz_taskType.json
    taskParam: string;      //任务数据 dic_zyz_taskType.jsonT
    condition: number;      //任务数据条件 dic_zyz_taskType.jsonT
    point: number;          // 任务达成获得的奖章数量，只在当前活动中有用，虚拟
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    taskParamArray: number[];

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

    constructor(data: any) {
        this.dayIndex = data.dayIndex;
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.condition = data.condition;
        this.point = data.point;
        this.reward = data.reward;
        this.totalCount = 0;
        this.receiveRewardCount = 0;
        this.taskParamArray = splitString(data.taskParam, '&')

    }

    public canReceive(): boolean {
        return this.receiveRewardCount == 0;
    }

    public isComplete(): boolean {
        let complete = this.totalCount >= this.condition;
        return complete;
    }
}


// 成长活动数据
export class GrowthData extends ActivityBase {
    list: Array<GrowthItem> = [];
    pointRewardList: Array<PointRewardItem> = [];

    public getTotalPoint() {
        let total = 0;
        for (let obj of this.list) {
            if (obj.receiveRewardCount) {
                total += obj.point;
            }
        }
        return total;
    }

    //第几天的奖章兑换
    public findPointItem(id: number) {
        let index = this.pointRewardList.findIndex(obj => { return obj && obj.id === id })
        return (index != -1) ? this.pointRewardList[index] : null;
    }

    public findGrowthItem(dayIndex: number, cellIndex: number, type: number) {
        let index = this.list.findIndex(obj => { return obj && obj.dayIndex == dayIndex && obj.cellIndex == cellIndex && obj.taskType == type })
        return (index != -1) ? this.list[index] : null;
    }

    public findTaskByType(type: TASK_TYPE) {
        return this.list.filter(obj => {
            return obj && obj.taskType == type;
        })
    }

    //解析玩家积分兑换记录
    public setPlayerPointRecord(data: ActivityGrowthPointModelType) {
        if (!data) {
            return;
        }
        for (let obj of this.pointRewardList) {
            let index = data.ids.findIndex(id => { return id === obj.id })
            if (index != -1) {
                obj.getPointReward = true;
            }
        }
    }

    //解析玩家领取记录
    public async setPlayerRecords(data: ActivityGrowthModelType[], roleId: string, userHeroes: HeroType[]) {
        let { heroNum, towerLv } = await RoleModel.findByRoleId(roleId);
        for (let obj of this.list) {
            let index = data.findIndex(record => { return obj.dayIndex == record.dayIndex && obj.cellIndex == record.cellIndex })
            if (index != -1) {
                obj.totalCount = data[index].totalCount ? data[index].totalCount : 0;
                obj.receiveRewardCount = data[index].receiveRewardCount ? data[index].receiveRewardCount : 0;
            }
            if (obj.taskType === TASK_TYPE.HERO_NUM) {
                obj.totalCount = heroNum;
            } else if (obj.taskType === TASK_TYPE.HERO_LV) {
                let lv = obj.taskParamArray[1];
                let heroes = userHeroes.filter(hero => { return hero.lv >= lv })
                obj.totalCount = heroes.length;
            } else if (obj.taskType === TASK_TYPE.BATTLE_TOWER_LV) {
                obj.totalCount = towerLv - 1;
            }
        }
    }

    public initData(data: string) {
        let objData = JSON.parse(data);

        let arr = objData.data;
        for (let obj of arr) {
            this.list.push(new GrowthItem(obj))
        }
        let pointRewardArray = objData.pointReward;
        for (let obj of pointRewardArray) {
            this.pointRewardList.push(new PointRewardItem(obj))
        }
    }

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