import { ActivityModelType } from '../../db/Activity';
import { UserGachaType } from '../../db/UserGacha';
import { DicGacha } from '../../pubUtils/dictionary/DicGacha';
import { ActivityBase } from './activityField';

interface NewHeroGachaItemInDb {
    hid: number;
    isDefault: boolean;
}

interface NewHeroGachaDataInDb {
    gachaId: number;       // dic_zyz_gacha表的id，卡池的概率，消耗都通过这个表读取
    heroes: NewHeroGachaItemInDb[];  // 本期活动pick的武将
}

class NewHeroGachaItem {
    hid: number;
    isDefault: boolean;
    constructor(hero: NewHeroGachaItemInDb) {
        this.hid = hero.hid;
        this.isDefault = hero.isDefault;
    }
}

// 每日关卡活动数据
export class NewHeroGachaData extends ActivityBase {
    gachaId: number;
    heroes: NewHeroGachaItem[] = [];
    pickHero: number = 0;
    count: number = 0;
    isFree: boolean = false;      // 免费次数

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

    public initData(data: string) {
        let dataObj: NewHeroGachaDataInDb = JSON.parse(data);
        this.gachaId = dataObj.gachaId;
        let heroes = dataObj.heroes||[];
        for(let hero of heroes) {
            this.heroes.push(new NewHeroGachaItem(hero))
        }
    }

    public findItem(hid: number) {
        return this.heroes.find(obj => { return obj && obj.hid == hid })
    }

    public getDefaultHero() {
        let hero = this.heroes.find(obj => obj.isDefault)||this.heroes[0];
        return hero?.hid||0;
    }

    //解析玩家记录
    public setPlayerRecords(data: UserGachaType, dic: DicGacha) {
        if (data) {
            this.pickHero = data.pickHero;
            this.count = data.count;
            this.isFree = data.freeCount < dic?.free.count;
        }
    }
    

    public isPickHero(hid: number) {
        return this.heroes.findIndex(cur => cur.hid == hid) != -1;
    }
}
