import { ActivityModelType } from '../../db/Activity';
import { ActivityThirtyDaysModelType } from '../../db/ActivityThirtyDays';
import { ActivityThirtyDaysPointRewardModelType } from '../../db/ActivityThirtyDaysPointReward';
import { splitString } from '../../pubUtils/util';
import { ActivityBase } from './activityField';

// 总数据
interface ThirtyDaysDataInDb {
    days: number;                           // 活动周期天数
    icon: string;             // 图标
    banner: string;           // banner
    data: ThirtyDaysPageInDb[];                 // 任务完成奖励
    pointRewardData: ThirtyDaysPointItemInDb[]; // 点数兑换奖励
}

// 30天目标每个标签页的数据
interface ThirtyDaysPageInDb {
    pageIndex: number;       // 第几页,从1开始
    name: string;            // 名称
    item: ThiryDaysCellInDb[];      //ThirtyDaysItem每项奖励和任务内容
}

interface ThiryDaysCellInDb {
    cellIndex: number;
    taskList: ThiryDayCellTaskListInDb[];
}

interface ThiryDayCellTaskListInDb {
    tab: number;             // id
    name: string;            // 名称
    taskType: number;        // 任务类型 dic_zyz_taskType.json
    taskParam: string;       // 任务数据 dic_zyz_taskType.json
    condition: number;       // 任务条件
    reward: string;          // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    point: number;           // 任务达成获得的奖励点，只在当前活动中有用，虚拟
}

// 30天目标每个点兑换奖励的数据
interface ThirtyDaysPointItemInDb {
    cellIndex: number;       // 第几个，从1开始
    consumePoint: number;    // 消费点数
    reward: string;          // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    expiredReward: string;   // 过期任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
}

// 30天目标每个点兑换奖励的数据
export class ThirtyDaysPointItem {
    cellIndex: number;      // 第几个，从1开始
    consumePoint: number;    //消费点数
    reward: string;         //任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    expiredReward: string;   //过期任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品

    isReceive: boolean = false;          //是否领取过奖励

    isExpired: boolean = false;         //奖励是否已经过期

    constructor(data: ThirtyDaysPointItemInDb, isExpired: boolean) {
        this.cellIndex = data.cellIndex;
        this.consumePoint = data.consumePoint;
        this.reward = data.reward;
        this.expiredReward = data.expiredReward;
        this.isExpired = isExpired;
    }

}

// 30天目标每个奖励的数据
export class ThirtyDaysItem {
    tab: number;       // id
    pageIndex: number;       // 第几页,从1开始
    cellIndex: number;      // 第几个，从1开始
    name: string;            //名称
    taskType: number;       // 任务类型 dic_zyz_taskType.json
    taskParam: string;      //任务数据 dic_zyz_taskType.json
    condition: number;       //任务条件
    taskParamArray: Array<number>;      //任务数据 dic_zyz_taskType.json
    reward: string;         //任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    point: number;          // 任务达成获得的奖励点，只在当前活动中有用，虚拟

    totalCount: number = 0; //任务完成次数
    isComplete: boolean = false;          //是否完成任务
    isReceive: boolean = false;          //是否领取过奖励

    constructor(data: ThiryDayCellTaskListInDb, pageIndex: number, cellIndex: number) {
        this.tab = data.tab;
        this.pageIndex = pageIndex;
        this.cellIndex = cellIndex;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.condition = data.condition;
        this.reward = data.reward;
        this.point = data.point;

        this.totalCount = 0;
        this.isComplete = false;
        this.isReceive = false;

        this.taskParamArray = splitString(data.taskParam, '&')
    }

    public setPlayerRecord(record: ActivityThirtyDaysModelType) {
        this.isReceive = record.isReceive === true;
        this.totalCount = record.totalCount ? record.totalCount : 0;
        this.isComplete = record.totalCount >= this.condition;
    }

    public canReceive(): boolean {
        return !this.isReceive;
    }
}

// 30天目标每个标签页的数据
export class ThirtyDaysPage {
    pageIndex: number;       // 第几页,从1开始
    name: string;            //名称
    item: ThirtyDaysItem[];      //ThirtyDaysItem每项奖励和任务内容
    constructor(data: ThirtyDaysPageInDb) {
        this.pageIndex = data.pageIndex;
        this.name = data.name;
        this.item = [];
        for (let obj of data.item) {
            for (let cellData of obj.taskList) {
                this.item.push(new ThirtyDaysItem(cellData, data.pageIndex, obj.cellIndex));
            }
        }
    }
}

// 30天任务活动数据
export class ThirtyDaysData extends ActivityBase {
    days: number = 0;//活动周期天数
    icon: string;             // 图标
    banner: string;           // banner
    list: Array<ThirtyDaysPage> = [];//任务完成奖励
    pointRewardList: Array<ThirtyDaysPointItem> = [];//点数兑换奖励
    totalPoint: number = 0;//总共点数

    public findTaskByType(type: number) {
        let task: ThirtyDaysItem[] = [];
        for (let i = 0; i < this.list.length; i++) {
            let items = this.list[i].item;
            for (let itemData of items) {
                if (itemData.taskType == type) {
                    task.push(itemData);
                }
            }
        }
        return task;
    }

    //全部领取完成
    public isComplete() {
        for (let i = 0; i < this.list.length; i++) {
            let items = this.list[i].item;
            for (let itemData of items) {
                if (!itemData.isReceive) {
                    return false
                }
            }
        }
        return true;
    }

    //查询过期但未标记的点数奖励
    public needMark(): Array<ThirtyDaysPointItem> {
        let data = this.pointRewardList.sort((a, b) => { return a.cellIndex - b.cellIndex });
        let point = this.totalPoint;
        let expiredData = [];
        for (let item of data) {
            if (point < item.consumePoint) {
                if (!item.isExpired)
                    expiredData.push(item);
            } else {
                point -= item.consumePoint;
            }
        }
        return expiredData
    }

    //查询过期的点数奖励
    public expiredPointItem(): Array<ThirtyDaysPointItem> {
        let data = this.pointRewardList.sort((a, b) => { return a.cellIndex - b.cellIndex });
        let point = this.totalPoint;
        let expiredData = [];
        for (let item of data) {
            if (point < item.consumePoint) {
                expiredData.push(item);
            } else {
                point -= item.consumePoint;
            }
        }
        return expiredData
    }

    //还未兑换的点数奖励
    public notReceivedPointItem(): Array<ThirtyDaysPointItem> {
        return this.pointRewardList.filter(obj => { return obj && !obj.isReceive })
    }

    //点数兑换的奖励内容
    public findThirtyDaysPointItem(cellIndex: number): ThirtyDaysPointItem {
        let index = this.pointRewardList.findIndex(obj => { return obj && obj.cellIndex == cellIndex })
        return (index != -1) ? this.pointRewardList[index] : null;
    }

    //完成的任务奖励内容
    public findThirtyDaysItem(pageIndex: number, cellIndex: number, tab: number): ThirtyDaysItem {
        let listIndex = this.list.findIndex(obj => { return obj && obj.pageIndex == pageIndex });
        if (listIndex != -1) {
            let itemIndex = this.list[listIndex].item.findIndex(obj => { return obj && obj.cellIndex == cellIndex && obj.tab == tab });
            if (itemIndex != -1) {
                return this.list[listIndex].item[itemIndex];
            }
        }
        return null;
    }

    //解析玩家点数兑换领取记录
    public setPlayerPointRecords(data: ActivityThirtyDaysPointRewardModelType[]) {
        for (let obj of this.pointRewardList) {
            let index = data.findIndex(record => { return obj.cellIndex == record.cellIndex })
            if (index != -1) {
                obj.isReceive = data[index].isReceive === true;
            }
        }
    }

    //解析玩家任务领取记录
    public setPlayerRecords(data: ActivityThirtyDaysModelType[]) {
        this.totalPoint = 0;
        for (let record of data) {
            let itemData = this.findThirtyDaysItem(record.pageIndex, record.cellIndex, record.tab)
            if (itemData) {
                itemData.setPlayerRecord(record);
                if (itemData.isReceive) {
                    this.totalPoint += itemData.point;
                }
            }
        }
    }


    public initData(data: string) {
        let dataObj: ThirtyDaysDataInDb = JSON.parse(data);
        this.days = dataObj.days;
        this.icon = dataObj.icon;
        this.banner = dataObj.banner;

        let pointRewardData = dataObj.pointRewardData||[];
        for (let obj of pointRewardData) {
            this.pointRewardList.push(new ThirtyDaysPointItem(obj, this.todayIndex > this.days))
        }

        let arr = dataObj.data||[];
        for (let obj of arr) {
            this.list.push(new ThirtyDaysPage(obj))
        }
    }

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