// GVGBattleRec 数据库表，挑战记录信息
import BaseModel from "./BaseModel";
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { genCode } from "../pubUtils/util";
import { GVGTeamType } from "./GVGTeam";
import { GVGHeroInfo } from "../domain/dbGeneral";
import { nowSeconds } from "../pubUtils/timeUtil";

class TeamInfo {
    @prop({ required: true })
    roleId: string;     // 玩家id

    @prop({ required: true })
    roleName: string;     // 玩家id

    @prop({ required: true })
    teamCode: string;     // 玩家队伍唯一标识

    @prop({ required: true })
    index: number;     // 队伍id，1-3，玩家的第几个队伍

    @prop({ required: true })
    leagueCode: string; // 联军

    @prop({ required: true })
    guildCode: string; // 军团

    @prop({ required: false })
    areaId: number;

    @prop({ required: false })
    cityId: number;

    @prop({ required: false })
    pointId: number;

    @prop({ required: true })
    head: number;       // 头像

    @prop({ required: true })
    spine: number;      // 形象

    @prop({ required: true })
    frame: number;      // 相框

    @prop({ required: true, default: 0 })
    durability: number; // 耐久

    @prop({ required: true, type: () => GVGHeroInfo, _id: false })
    lineup: GVGHeroInfo[]

    @prop({ required: true, default: 0 })
    isRobot: boolean; // 是否是机器人

    @prop({ required: true, default: 0 })
    isCatapult: boolean; // 是否是投石车

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

    @prop({ required: true, default: 0 })
    curTeamBreak: boolean;
}

@index({ battleCode: 1 })
export default class GVGBattleRec extends BaseModel {
    @prop({ required: true })
    battleCode: string;     // 战斗记录

    @prop({ required: true })
    configId: number;     // 赛季

    @prop({ required: true })
    groupKey: string;     // 1-单服 2-跨服

    @prop({ required: true })
    warId: number;     // 关卡id

    @prop({ required: true, type: TeamInfo, _id: false })
    attackTeam: TeamInfo;     // 挑战队伍

    @prop({ required: true, type: TeamInfo, _id: false })
    defenseTeam: TeamInfo;     // 防守队伍

    @prop({ required: true, type: TeamInfo, _id: false })
    attackTeamAfter: TeamInfo;     // 挑战队伍

    @prop({ required: true, type: TeamInfo, _id: false })
    defenseTeamAfter: TeamInfo;     // 防守队伍

    @prop({ required: true })
    isSuccess: boolean;

    @prop({ required: true })
    battleEndTime: number;

    @prop({ required: true, default: false })
    hasRpl: boolean;            // 是否存在对应录像

    public static async createRec(configId: number, groupKey: string, warId: number, attackTeam: GVGTeamType, defenseTeam: GVGTeamType) {
        const battleCode = genCode(8);
        const result: GVGBattleRecType = await GVGBattleRecModel.findOneAndUpdate({ battleCode }, { $set: { configId, groupKey, warId, attackTeam, defenseTeam, isSuccess: false } }, { new: true, upsert: true }).lean();
        return result;
    }

    public static async findByBattleCode(battleCode: string) {
        const result: GVGBattleRecType = await GVGBattleRecModel.findOne({ battleCode }).lean();
        return result;
    }
    
    public static async battleEnd(battleCode: string, isSuccess: boolean, attackTeam: GVGTeamType, defenseTeam: GVGTeamType) {
        const result: GVGBattleRecType = await GVGBattleRecModel.findOneAndUpdate({ battleCode }, { $set: { isSuccess, battleEndTime: nowSeconds(), attackTeamAfter: attackTeam, defenseTeamAfter: defenseTeam } }, { new: true }).lean();
        return result;
    }

    public static async updateRplStatus(battleCode: string, hasRpl: boolean) {
        let result = await GVGBattleRecModel.findOneAndUpdate({ battleCode }, { hasRpl }, { new: true }).lean();
        return result;
    }
}

export const GVGBattleRecModel = getModelForClass(GVGBattleRec);

export interface GVGBattleRecType extends Pick<DocumentType<GVGBattleRec>, keyof GVGBattleRec> {};

export type GVGBattleRecUpdate = Partial<GVGBattleRecType>; // 将所有字段变成可选项