import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { RoleType, TopHero } from './Role';
import { getZeroPoint } from '../pubUtils/timeUtil';

export class ActivePlayer {
  @prop({ required: true })
  roleId: string;
  @prop({ required: true })
  ce: number;
  @prop({ required: true })
  topLineupCe: number;
  @prop({ required: true, type: TopHero, default: [], _id: false })
  topLineup: Array<TopHero>;							// 总战力
  
  constructor(role: RoleType) {
    this.roleId = role.roleId;
    this.ce = role.ce;
    this.topLineupCe = role.topLineupCe;
    this.topLineup = role.topLineup;
  }
}

export class GuildRecord {
  @prop({ required: true })
  guildCode: string;
  @prop({ required: true, type: ActivePlayer, _id: false })
  players: ActivePlayer[];
}

/**
  * 游戏字段接口
*/
@index({ serverId: 1, today: 1 })
@modelOptions({ schemaOptions: { id: false } })
export default class ServerRecord extends BaseModel {

  @prop({ required: true, default: 1 })
  serverId: number;            // 小区id

  @prop({ required: true, default: 1 })
  today: number;            // 天

  @prop({ required: true, default: 0 })
  activePlayerCe: number;          // 前一天活跃玩家前十的最强6人战力和

  @prop({ required: true, default: 0 })
  activePlayerCnt: number;          // 前一天活跃玩家人数

  @prop({ required: true, type: ActivePlayer, _id: false })
  activePlayers: ActivePlayer[];          // 前一天的活跃玩家

  @prop({ required: true, type: String, default: [] })
  activeGuildCodes: string[];          // 前一天参与军团活动的军团

  @prop({ required: true, type: String, default: [] })
  activeGuildMembers: string[];          // 前一天参与军团活动的玩家

  @prop({ required: true, type: GuildRecord, _id: false })
  activeGuilds: GuildRecord[];

  public static async updateData(serverId: number, update: ServerRecordUpdate) {
    let today = getZeroPoint();
    let rec: ServerRecordType = await ServerRecordModel.findOneAndUpdate({ serverId, today }, { $set: update }, { new: true, upsert: true }).lean();
    return rec;
  }

  public static async addActiveGuild(serverId: number, roleId: string, guildCode: string) {
    let tomorrow = getZeroPoint() + 86400;
    let rec: ServerRecordType = await ServerRecordModel.findOneAndUpdate({ serverId, today: tomorrow }, { $addToSet: { activeGuildCodes: guildCode, activeGuildMembers: roleId } }, { new: true, upsert: true }).lean();
    return rec;
  }

  public static async findTodayData(serverId: number) {
    let today = getZeroPoint();
    let rec: ServerRecordType = await ServerRecordModel.findOne({ serverId, today }).lean();
    return rec;
  }

  public static async findTomorrowData(serverId: number) {
    let tomorrow = getZeroPoint() + 86400;
    let rec: ServerRecordType = await ServerRecordModel.findOne({ serverId, today: tomorrow }).lean();
    return rec;
  }
}

export const ServerRecordModel = getModelForClass(ServerRecord);

export interface ServerRecordType extends Pick<DocumentType<ServerRecord>, keyof ServerRecord> {
  id: number;
};
export type ServerRecordUpdate = Partial<ServerRecordType>; // 将所有字段变成可选项
