import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { LineupParam } from '../domain/rank';

/**
  * 战斗记录接口
  */

class Record {
    @prop({ required: true, type: Number })
    heroes?: Array<number>; // 武将id
    @prop({ required: true, type: Number })
    seqIds?: Array<number>; // 武将seqid
    @prop({ required: true, type: () => LineupParam, _id: false })
    lineup?: Array<LineupParam>; // 武将详细信息
    @prop({ required: false })
    oppRoleId?: string; // pvp对手
    @prop({ required: false })
    pos?: number; // pvp位置
    @prop({ required: false })
    trainId?:number;
    @prop({ required: false })
    trainLv?:number;
    @prop({ required: false })
    hid?:number;
    @prop({ required: false })
    guildCode?: string;
    @prop({ required: false })
    difficulty?: number;
    @prop({ required: false })
    bossInstanceCode?: string;
    @prop({ required: false })
    bossHp?: number;
    @prop({ required: false })
    bossDamage?: number;			// boss本造成的伤害
    @prop({ required: false })
    encourageCnt?: number;
    @prop({ required: false })
    pvpSeasonNum?: number;
}

@index({ roleId: 1, battleId: 1 })
@index({ battleCode: 1 })
export default class BattleRecord extends BaseModel {
    @prop({ required: true })
    roleId: string;					// 角色 id
    @prop({ required: true })
    roleName: string;				// 角色名称

    @prop({ required: true })
    battleCode: string;						// 关卡记录唯一标识
    @prop({ required: true })
    battleId: number;						// 关卡 id
    @prop({ required: true })
    warName: string;						// 关卡 名
    @prop({ required: true })
    warType: number;						// 关卡 类型

    @prop({ required: true, default: 0 })
    status: number;						// 关卡状态 0-挑战中 1-挑战成功 2-挑战失败
    @prop({ required: true, default: 0 })
    star: number;						// 星级
    @prop({ required: true, default: 0 })
    stars: number[];					// 达成星星
    @prop({ required: true, _id: false })
    record: Record;		         // 使用的武将等记录

    @prop({ required: true, default: 0 })
    regretCnt: number;           // 悔棋次数

    public static async getBattleRecordByCode(battleCode: string, lean = true) {
        const result: BattleRecordType = await BattleRecordModel.findOne({ battleCode }).lean(lean);
        return result;
    }

    public static async getBattleRecordByIdAndStatus(roleId: string, battleId: number, status: number, lean = true) {
        const result: BattleRecordType = await BattleRecordModel.findOne({ roleId, battleId, status }).lean(lean);
        return result;
    }

    public static async getBattleRecordByIdAndStar(roleId: string, battleId: number, star: number, lean = true) {
        const result: BattleRecordType = await BattleRecordModel.findOne({ roleId, battleId, star: { $lte: star } }).lean(lean);
        return result;
    }

    public static async updateBattleRecordByCode(battleCode: string, params: { $set: BattleRecordParam }, lean = true) {
        const result: BattleRecordType = await BattleRecordModel.findOneAndUpdate({ battleCode }, params, { new: true, upsert: true }).lean(lean);
        return result;
    }

    public static async deleteAccount(roleId: string) {
        let result = await BattleRecordModel.deleteMany({ roleId });
        return result || {};
    }

    public static async incBossDamage(battleCode: string, damage: number, bossHp: number) {
        if(bossHp < 0) { // 最后一击，被扣到负了
            damage += bossHp;
        }
        let result: BattleRecordType = await BattleRecordModel.findOneAndUpdate({ battleCode }, { $inc: { 'record.bossDamage': damage } }, { new: true }).lean();
        return result;
    }

    public static async incRegretCnt(battleCode: string, count: number) {
        let result: BattleRecordType = await BattleRecordModel.findOneAndUpdate({ battleCode }, { $inc: { regretCnt: count } }, { new: true }).lean();
        return result;
    }

    public static async findByWarTypeAndStatus(roleId: string, warType: number, status: number, beginTime: Date, endTime: Date) {
        let result: BattleRecordType[] = await BattleRecordModel.find({ roleId, warType, status, createdAt: { $gte: beginTime, $lt: endTime } }).lean();
        return result;
    }
}

export const BattleRecordModel = getModelForClass(BattleRecord);

export interface BattleRecordType extends Pick<DocumentType<BattleRecord>, keyof BattleRecord> { }
export type BattleRecordParam = Partial<BattleRecordType>; // 将所有字段变成可选项