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

class ActiveRec {
    @prop({ required: true, default: 1 })
    id: number;       // 1-内政令消耗 2-征战令消耗 3-科技树捐献
    @prop({ required: true, default: 0 })
    count: number;
    @prop({ required: true, default: 0 })
    time: number;

    constructor(id: number, count: number) {
        this.id = id;
        this.count = count;
        this.time = nowSeconds();
    }
}

export class Contribute {
    @prop({ required: true, default: 0 })
    food: number = 0;        // 粮食
    @prop({ required: true, default: 0 })
    mineral: number = 0;     // 矿物
    @prop({ required: true, default: 0 })
    wood: number = 0;        // 木材
    @prop({ required: true, default: 0 })
    score: number = 0;       // 据点积分
}

@index({ leagueCode: 1, roleId: 1, configId: 1, status: 1 })
export default class GVGUserData extends BaseModel {

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

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

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

    @prop({ required: true, default: 0 })
    job: number;             // 职能 1-贤臣 2-猛将

    // 备战期
    @prop({ required: true, default: 0 })
    active: number;       // 活跃度

    @prop({ required: true, default: 0, type: ActiveRec, _id: false })
    activeRec: ActiveRec[];         // 活跃度记录

    @prop({ required: true, default: 0 })
    receivedLv: number; // 领取到的等级奖励

    @prop({ required: true, default: 0 })
    receiveCurrencyTime: number; // 领取内政令&征战令时间

    @prop({ required: true, default: [], type: Number })
    box: number[];

    @prop({ required: true, default: {}, _id: false })
    contribute: Contribute;

    @prop({ required: true, default: 0 })
    sendFightRewardTime: number; // 发送征战奖励的时间
    
    // 激战期

    @prop({ required: true, default: 0 })
    battleStatus: number;    // 1-被挑战中 0-可被挑战
    
    @prop({ required: true, default: 0 })
    area: number;            // 所处区域 1-进攻北站 2-混战 3-据点 4-防守备战
    
    @prop({ required: true, default: 0 })
    protectTime: number;  // 保护期 

    @prop({ required: false })
    cityId: number;         // 城池id

    @prop({ required: false })
    reviveCnt: number;         // 城池id

    public static async findByRole(configId: number, leagueCode: string, roleId: string) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, {}, { new: true, upsert: true}).lean();
        return result;
    }

    public static async findByRoles(configId: number, leagueCode: string, roleIds: string[]) {
        const result: GVGUserDataType[] = await GVGUserDataModel.find({ configId, leagueCode, roleId: { $in: roleIds } }).lean();
        return result;
    }

    public static async chooseJob(configId: number, leagueCode: string, roleId: string, job: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $set: { job } }, { new: true, upsert: true}).lean();
        return result;
    }

    public static async addActive(configId: number, leagueCode: string, roleId: string, activeType: number, active: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $inc: { active }, $push: { activeRec: new ActiveRec(activeType, active) } }, { new: true, upsert: true}).select('active').lean();
        return result;
    }

    public static async addResource(configId: number, leagueCode: string, roleId: string, food: number, mineral: number, wood: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ 
            configId, leagueCode, roleId
        }, { 
            $inc: { 'contribute.food': food, 'contribute.mineral': mineral, 'contribute.wood': wood }
        }, { new: true, upsert: true }).lean();
        return result;
    }

    public static async addVestigeScores(configId: number, memberScores: { leagueCode: string, roleId: string, score: number }[]) {
        await GVGUserDataModel.bulkWrite(memberScores.map(({ leagueCode, roleId, score }) => {
            return { updateOne: { filter: { configId, leagueCode, roleId }, update: { $inc: { 'contribute.score': score } } } }
        }));
    }

    public static async receiveBox(configId: number, leagueCode: string, roleId: string, boxId: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $push: { box: boxId }}, { new: true }).lean();
        return result;
    }

    public static async receiveLv(configId: number, leagueCode: string, roleId: string, lv: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $set: { receivedLv: lv }}, { new: true }).lean();
        return result;
    }

    // 更换城池
    public static async changeCity(configId: number, leagueCode: string, roleId: string, cityId: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $set: { cityId }}, { new: true }).lean();
        return result;
    }

    public static async addReviveCnt(configId: number, leagueCode: string, roleId: string, cnt: number) {
        const result: GVGUserDataType = await GVGUserDataModel.findOneAndUpdate({ configId, leagueCode, roleId }, { $inc: { reviveCnt: cnt } }, { new: true, upsert: true}).lean();
        return result;
    }

}

export const GVGUserDataModel = getModelForClass(GVGUserData);

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