import { ActivityModelType } from '../../db/Activity';
import { ActivityBase } from './activityField';
import { prop } from '@typegoose/typegoose';
import { UserGachaType } from '../../db/UserGacha';
import { getTimeFun } from '../../pubUtils/timeUtil';
import { DicGacha } from '../../pubUtils/dictionary/DicGacha';
import { GACHA_OCCUPY_HID, GACHA_ID } from '../../consts';
import { gameData } from '../../pubUtils/data';
import { getFloorStatus } from '../../pubUtils/util';


// 抽卡数据
export class GachaData extends ActivityBase {
    gachaId: number = 0;
    heroes: Array<number> = [];

    pickHero: number;
    freeCount: number;
    refFreeTime: number = 0;
    count: number;
    floor: Floor[];

    public initData(data: string) {
        let obj = JSON.parse(data);
        this.gachaId = obj.gachaId;
        this.heroes = obj.heroes;
    }

    public setUserGacha(userGacha: UserGachaType) {
        let dicGacha = gameData.gacha.get(this.gachaId);
        this.pickHero = userGacha.pickHero;
        this.freeCount = userGacha.freeCount;
        if (dicGacha.free.count > 0) {
            let f = getTimeFun(userGacha.refFreeTime);
            this.refFreeTime = <number>f.getAfterDayWithHour(dicGacha.free.day);
        }
        this.count = userGacha.count;
        this.floor = userGacha.floor;
    }

    constructor(activityData: ActivityModelType, createTime: number) {
        super(activityData, createTime)
    }
}

/**
 * @description 保底记录
 * @memberof UserGacha
 */
export class Floor {
    @prop({ required: true })
    id: number;            // 保底类型 1-紫将保底 2-金将保底 3-指定将保底
    @prop({ required: true })
    count: number;         // 抽卡次数
}

/**
 * @description 心愿单
 * @memberof UserGacha
 */
export class Hope {
    @prop({ required: true })
    id: number;            // 位置
    @prop({ required: true })
    hid: number;          // 武将id
    @prop({ required: true })
    hasGet: boolean;       // 是否得到
}

/**
 * @description 转盘记录
 * @memberof UserGacha
 */
export class Turntable {
    @prop({ required: true })
    quality: number;       // 品质
    @prop({ required: true })
    hasGet: boolean;       // 是否得到
}

/**
 * @description 获取抽卡界面的返回参数
 */
export class GachaListReturn {
    gachaId: number;            // dicGacha里的id
    freeCount: number = 0;      // 免费次数
    refFreeTime: number = 0;    // 免费次数下次刷新时间
    count: number = 0;          // 整体已抽卡次数
    floor: Floor[] = [];        // 整体保底
    hope: Hope[] = [];          // 心愿单
    point: number = 0;          // 积分
    turntable: Turntable[] = [];     // 转盘记录
    pickHero: number = 0;       // 玩家选择的武将

    constructor(dicGacha: DicGacha, userGacha: UserGachaType) {
        this.gachaId = dicGacha.id;

        if (userGacha) {
            this.freeCount = userGacha.freeCount;
            if (dicGacha.free.count > 0) {
                let f = getTimeFun(userGacha.refFreeTime);
                this.refFreeTime = <number>f.getAfterDayWithHour(dicGacha.free.day);
            }
            this.count = userGacha.count;
            this.floor = getFloorStatus(dicGacha.id, userGacha.floor);
            this.hope = userGacha.hope;
            this.point = userGacha.point;
            this.turntable = userGacha.turntable;
            this.pickHero = userGacha.pickHero;
        } else {
            this.floor = getFloorStatus(dicGacha.id, []);
        }
    }
}

export class GachaResult {
    @prop({ required: true })
    contentId: number;   // 抽卡内容id
    @prop({ required: true })
    hid: number = 0;     // 武将id
    @prop({ required: true })
    isTransfer: boolean = false; // 是否转换为碎片
    @prop({ required: true })
    id: number = 0;      // 道具id
    @prop({ required: true })
    count: number = 0;   // 道具数量

    constructor(contentId: number) {
        this.contentId = contentId;
    }

    setSetPickHero(hid: number) {
        if (hid > 0 && this.hid == GACHA_OCCUPY_HID) {
            this.hid = hid;
        }
    }

    setHero(hid: number) {
        this.hid = hid;
        this.count = 1;
    }

    setItem(id: number, count: number) {
        this.id = id;
        this.count = count;
    }

    transferToPiece(id: number, count: number) {
        this.isTransfer = true;
        this.id = id;
        this.count = count;
    }
}