import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { getZeroPointD } from '../pubUtils/timeUtil';
import { SHOP_REFRESH_TYPE } from '../consts';

@index({ serverId: 1, cityId: 1, createdAt: 1   })
export default class GuildActivityCity extends BaseModel {

    @prop({ required: true })
    serverId: number;       // 分服
    
    @prop({ required: true })
    cityId: number;       // 城池id

    @prop({ required: true, type: String, default: [] })
    declareGuilds: string[];       // 宣战的公会的code

    @prop({ required: true, default: 0 })
    declareCount: number;       // 宣战数量

    @prop({ required: true, default: "" })
    guardGuildCode: string;       // 占领的军团的code

    @prop({ required: true, default: "" })
    guardGuildName: string;       // 占领的军团名

    @prop({ required: true })
    guardTime: Date;       // 占领日期

    // 每天宣战一次
    public static async getAllCities(serverId: number) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType[] = await GuildActivityCityModel.find({ serverId, createdAt: { $gte: curWeek }}).lean();
        
        return rec;
    }

    public static async getCity(serverId: number, cityId: number) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType = await GuildActivityCityModel.findOne({ serverId, cityId, createdAt: { $gte: curWeek }}).lean();
        
        return rec;
    }

    // 检查是否宣战过
    public static async checkDeclartion(serverId: number, guildCode: string) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType = await GuildActivityCityModel.findOne(
            { serverId, createdAt: { $gte: curWeek }, declareGuilds: { $in: [guildCode]}}).lean();

        return rec;
    }

    // 宣言
    public static async declare(serverId: number, cityId: number, guildCode: string) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType = await GuildActivityCityModel.findOneAndUpdate(
            { serverId, cityId, createdAt: { $gte: curWeek } }, 
            { $setOnInsert: { serverId, cityId }, $push: { declareGuilds: guildCode }, $inc: {declareCount: 1 } }, 
            {new: true, upsert: true}).lean();

        return rec;
    }

    // 占领
    public static async guard(serverId: number, cityId: number, guildCode: string, guildName: string) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType = await GuildActivityCityModel.findOneAndUpdate(
            { serverId, cityId, createdAt: { $gte: curWeek } }, 
            { $set: { guardGuildCode: guildCode, guardGuildName: guildName, guardTime: new Date()} }, 
            { new: true }).lean();

        return rec;
    }

    // 活动结束后需要清空占领情况
    public static async clearGuard(serverId: number, cityId: number) {
        let curWeek = getZeroPointD(SHOP_REFRESH_TYPE.WEEKLY);
        let rec: GuildActivityCityType = await GuildActivityCityModel.findOneAndUpdate(
            { serverId, cityId, createdAt: { $gte: curWeek } }, 
            { $set: { guardGuildCode: "", guardGuildName: "", declareCount: 0, declareGuilds: [] } }, 
            { new: true }).lean();

        return rec;
    }
}

export const GuildActivityCityModel = getModelForClass(GuildActivityCity);

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