import { prop } from '@typegoose/typegoose';
import { UserGachaType } from '../../db/UserGacha';
import { DicGacha } from '../../pubUtils/dictionary/DicGacha';
import { getGachaRemainFloor } from '../../pubUtils/util';
import { RewardInter } from '../../pubUtils/interface';

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

/**
 * @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
    gachaType: number;
    isFree: boolean = false;      // 免费次数
    cost: RewardInter[] = [];    // 消耗
    count: number = 0;          // 整体已抽卡次数
    remainFloor: number = 0;    // 还剩多少次可以保底
    hope: Hope[] = [];          // 心愿单
    point: number = 0;          // 积分
    turntable: Turntable[] = [];     // 转盘记录
    pickHero: number = 0;       // 玩家选择的武将

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

        if (userGacha) {
            this.isFree = userGacha.freeCount < dicGacha.free.count;
            this.count = userGacha.count;
            this.hope = userGacha.hope;
            this.point = userGacha.point;
            this.turntable = userGacha.turntable;
            this.pickHero = userGacha.pickHero;
        }
        this.remainFloor = getGachaRemainFloor(this.gachaId, userGacha?.floor||[]);
    }
}

export class GachaResultIndb {
    @prop({ required: true })
    planId: number = 0;     // 计划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(gachaResult: Partial<GachaResultIndb>) {
        this.planId = gachaResult.planId;
        this.hid = gachaResult.hid;
        this.isTransfer = gachaResult.isTransfer;
        this.id = gachaResult.id;
        this.count = gachaResult.count;
    }


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