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

import { PvpEnemies, PvpOtherHeroes } from '../domain/dbGeneral';
import { EXTERIOR } from '../pubUtils/dicParam';

export class HeroScores {
    @prop({ required: true })
    hid: number;
    @prop({ required: true })
    score: number;
}

@index({ roleId: 1, oppRoleId: 1 })
export default class PvpHistoryOpp extends BaseModel {
    @prop({ required: true })
    roleId: string;					// 玩家id
    @prop({ required: true })
    pos: number;					// 对手位置
    @prop({ required: true })
    oppRoleId: string;		    	// 对手角色 id
    @prop({ required: true })
    isRobot: boolean;		    	// 是否是机器人
    @prop({ required: true })
    roleName: string;				// 角色名称
    @prop({ required: true, default: EXTERIOR.EXTERIOR_FACE })
    head: number;                // 对手头像
    @prop({ required: true, default: EXTERIOR.EXTERIOR_FACECASE })
    frame: number;                   // 对手相框
    @prop({ required: true, default: EXTERIOR.EXTERIOR_APPEARANCE })
    spine: number;                   // 对手形象
    @prop({ required: true, default: 0 })
    score: number;                  // 对手军功
    @prop({ required: true, default: 1 })
    pLv: number;                    // 对手等级
    @prop({ required: true, default: 1 })
    lv: number;                    // 对手角色等级
    @prop({ required: true, default: 0 })
    job: number;                    // 对手职业
    @prop({ required: true, default: 1 })
    title: number;                    // 对手等级
    @prop({ required: true, default: 0 })
    defCe: number;                 // 防守战力，缩小过的
    @prop({ required: true, default: 0 })
    rankLv: number;                 // 对手排名
    @prop({ required: true })
    warId: number;                // 对手选择的地图id
    @prop({ required: true })
    buff: number;                // 对手选择的buff
    @prop({ required: true, type: PvpEnemies, default: [], _id: false })
    heroes: PvpEnemies[];                // 对手阵容
    @prop({ required: true, type: PvpOtherHeroes, default: [] })
    otherHeroes: PvpOtherHeroes[];             // 阵容以外所有武将信息
    @prop({ required: true, default: 0 })
    status: number;                 // 状态 0-仅刷出 1-挑战过

    public static async createPvpOpp(params: PvpOppCreateParam) {
        const doc = new PvpHistoryOppModel();
        const update = Object.assign(doc.toJSON(), params);
        delete update._id;
        delete update.updatedAt;
        delete update.createdAt;
        const defense: PvpHistoryOppType = await PvpHistoryOppModel.findOneAndUpdate({ roleId: params.roleId, pos: params.pos, status: 0 }, { $set: update }, { upsert: true, new: true }).lean();
        return defense;
    }

    public static async findByRoleIdAndOppId(roleId: string, oppRoleId: string) {
        const result: PvpHistoryOppType = await PvpHistoryOppModel.findOne({ roleId, oppRoleId, status: 0 }).lean();
        return result;
    }

    public static async setStatus(_id: string, status: number) {
        const result: PvpHistoryOppType = await PvpHistoryOppModel.findByIdAndUpdate(_id, { $set: {status}}, {new: true}).lean();
        return result;
    }

}

export const PvpHistoryOppModel = getModelForClass(PvpHistoryOpp);

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