import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { getTimeFun } from '../pubUtils/timeUtil';
import { genCode } from '../pubUtils/util';
import { PvpHeroInfo } from '../domain/dbGeneral';
import { EXTERIOR } from '../pubUtils/dicParam';

export class HeroesRecord {
    @prop({ required: true, default: 0 })
    hid: number;               // 武将id
    @prop({ required: true, default: 0 })
    skinId: number;               // 皮肤id，fashions表的heroId
    @prop({ required: true, default: 0 })
    quality: number;           // 品质
    @prop({ required: true, default: 0 })
    star: number;               // 星级
    @prop({ required: true, default: 0 })
    colorStar: number;          // 觉醒
    @prop({ required: true, default: 0 })
    lv: number;                 // 等级
    @prop({ required: true, default: 0 })
    damage: number;             // 伤害
    @prop({ required: true, default: 0 })
    heal: number;               // 治疗
    @prop({ required: true, default: 0 })
    underDamage: number;        // 承伤
    
    constructor(pvpHero: PvpHeroInfo, damage: number, heal: number, underDamage: number) {
        this.hid = pvpHero.actorId;
        this.skinId = pvpHero.skinId;
        this.quality = pvpHero.quality;
        this.star = pvpHero.star;
        this.colorStar = pvpHero.colorStar;
        this.lv = pvpHero.lv;
        this.damage = damage;
        this.heal = heal;
        this.underDamage = underDamage;
    }
}

export class PlayerInfo {
    @prop({ required: true })
    roleId: string;					// 角色 id
    @prop({ required: true })
    roleName: string;					// 角色 名
    @prop({ required: true, default: 0 })
    lv: number;               // 等级
    @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 })
    title: number;            // 爵位
    @prop({ required: true, default: 0 })
    ce: number;               // 最高五人战力
    @prop({ required: true, default: false })
    isSuccess: boolean;			// 是否胜利
    @prop({ required: true, default: 0 })
    score: number;			   	// 获得的总积分
    @prop({ required: true, type: HeroesRecord, default: [], _id: false })
    heroes: HeroesRecord[];			   	// 获得的总积分
}

@index({ code: 1 })
@index({ roleId1: 1, updatedAt: -1 })
@index({ roleId2: 1, updatedAt: -1 })

export default class PvpRecord extends BaseModel {
    @prop({ required: true })
    roleId1: string;					// 角色 id
    @prop({ required: true })
    roleId2: string;					// 角色 id

    @prop({ required: true, default: 0 })
    code: string;			   	// code
    @prop({ required: true, default: 0 })
    warId: number;			   	// 这一关使用的地图
    @prop({ required: true, type: PlayerInfo, default: {}, _id: false })
    attackInfo: PlayerInfo;			     	// 攻方信息
    @prop({ required: true, type: PlayerInfo, default: {}, _id: false })
    defenseInfo: PlayerInfo;			     	// 守方信息

    @prop({ required: true, default: 0 })
    createTime: number;

    public static async createRec(param: { roleId1: string, roleId2: string, warId: number, attackInfo: PlayerInfo, defenseInfo: PlayerInfo, createTime: number }) {
        await this.delPvpRecords();
        let code = genCode(6);
        const result = await PvpRecordModel.findOneAndUpdate({ code }, param, { new: true, upsert: true }).lean();
        return result;
    }

    public static async getRecByRoleId( roleId: string, limit = 15, lean = true) {
        await this.delPvpRecords();
        const result: PvpRecord[] = await PvpRecordModel.find({ $or:[{roleId1: roleId}, {roleId2: roleId}] }).limit(limit).sort({createTime: -1}).lean(lean);
        return result;
    }

    public static async delPvpRecords() {
        let t = <number>getTimeFun().getBeforeDayWithHour(3);
        let result = await PvpRecordModel.deleteMany({ createTime: {$lt: t}});//删除小于三天的战报
        return result;
    }
}

export const PvpRecordModel = getModelForClass(PvpRecord);

export interface PvpRecordType extends Pick<DocumentType<PvpRecord>, keyof PvpRecord> { };