// 军团限时充值人数活动

import { ActivityModelType } from "../../db/Activity";
import { ActivityGuildPayModelType } from "../../db/ActivityGuildPay";
import { ActivityGuildPayRecordModelType } from "../../db/ActivityGuildPayRecord";
import { ActivityBase } from "./activityField";

interface GuildPayRewardInDb {
    cellIndex: number; // 第几条
    memberCnt: number; // 充值金额达到目标金额的玩家人数
    reward: string;    // 奖励 type&id&count
}

interface GuildPayDataInDb {
    rewards: GuildPayRewardInDb[];
    payDays: number;   // 充值计入的天数，这之后到活动endTime为公示期，充值不会计入活动
}

export class GuildPayReward {
    cellIndex: number; // 第几条
    memberCnt: number; // 充值金额达到目标金额的玩家人数
    reward: string;    // 奖励 type&id&count

    hasReceived: boolean = false; // 玩家是否已领取

    constructor(data: GuildPayRewardInDb) {
        this.cellIndex = data.cellIndex;
        this.memberCnt = data.memberCnt;
        this.reward = data.reward;
    }

    public setHasReceived(hasReceived: boolean) {
        this.hasReceived = hasReceived;
    }
}

export class GuildPayData extends ActivityBase {
    payEndTime: number;
    rewards: GuildPayReward[] = [];
    map: Map<number, number> = new Map();
    
    payMemberCnt: number = 0;
    payRecord: string[] = [];

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

    public initData(data: string) {
        let dataObj: GuildPayDataInDb = JSON.parse(data);
        this.payEndTime = this.beginTime + dataObj.payDays * 24 * 60 * 60 * 1000;
        for(let reward of dataObj.rewards) {
            let obj = new GuildPayReward(reward);
            this.rewards.push(obj);
            this.map.set(obj.cellIndex, this.rewards.length - 1);
        }
    }

    public findRewardItemByCellIndex(cellIndex: number) {
        let index = this.map.get(cellIndex);
        return this.rewards[index];
    }

    public setReceiveRecord(receiveRecord: ActivityGuildPayRecordModelType) {
        if(!receiveRecord) return;
        for(let cellIndex of receiveRecord.receiveRecord) {
            let reward = this.findRewardItemByCellIndex(cellIndex);
            reward.setHasReceived(true);
        }
    }

    public setGuildRecord(guildRecord: ActivityGuildPayModelType) {
        if(!guildRecord) return;
        let records = guildRecord.memberRecord.filter(cur => !cur.hasQuit);
        this.payMemberCnt = records.length;
        this.payRecord = records.map(cur => cur.roleName);
    }

    public getShowResult() {
        return {
            ...this.getBaseKeys(),
            payEndTime: this.payEndTime,
            rewards: this.rewards,
            payMemberCnt: this.payMemberCnt,
            payRecord: this.payRecord
        }
    }

    public receiveItems(cellIndex: number) {
        let items: GuildPayReward[] = [];
        if(cellIndex == 0) {
            for(let reward of this.rewards) {
                if(reward.memberCnt <= this.payMemberCnt && !reward.hasReceived) {
                    reward.setHasReceived(true);
                    items.push(reward);
                }
            }
        } else {
            let reward = this.findRewardItemByCellIndex(cellIndex);
            if(reward && !reward.hasReceived && reward.memberCnt <= this.payMemberCnt) {
                reward.setHasReceived(true);
                items.push(reward);
            }
        }
        if(items.length <= 0) return false;
        return items
    }

    public canPay() {
        if(!this.isEnable) return false;
        if(this.beginTime > Date.now() || this.endTime < Date.now()) return false;
        if(this.nextRefreshTime && this.nextRefreshTime < Date.now()) return false;
        if(this.payEndTime && this.payEndTime < Date.now()) return false;
        return true;
    }
}