import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, Ref, mongoose } from '@typegoose/typegoose';
import Role, { RoleType } from './Role';
import { GUILD_STRUCTURE, GUILD_STATUS, GUILD_PER_PAGE, GUILD_SELECT, REDIS_KEY, SHOP_REFRESH_TYPE } from '../consts';
import { getZeroPoint, getZeroPointD, nowSeconds } from '../pubUtils/timeUtil';
import { gameData } from '../pubUtils/data';
import { SearchGuildParam } from '../domain/backEndField/search';
import { GVG } from '../pubUtils/dicParam';

export class Structure {
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    lv: number;    
}

@index({ code: 1 })
export default class Guild extends BaseModel {

    @prop({ required: true })
    code: string;

    @prop({ required: true })
    name: string;

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

    @prop({ required: true, default: 1 })
    lv: number;

    @prop({ required: true, default: 1 })
    lvUpdateTime: number;

    @prop({ required: true, default: 1 })
    memberCnt: number;

    @prop({ required: true, default: false, select: false })
    isMemberMax: boolean;

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

    @prop({ required: true, ref: () => Role, type: mongoose.Schema.Types.ObjectId })
    leader: Ref<Role>;

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

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

    @prop({ required: true, default: '' })
    notice: string;

    @prop({ required: true, default: '' })
    introduce: string;

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

    @prop({ required: true, default: 0 })
    todayFund: number;   // 今天获得的资金，只增不减

    @prop({ required: true, default: 0 })
    refTodayFund: Date;

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

    @prop({ required: true, default: nowSeconds() })
    activeUpdateTime: number;

    @prop({ required: true, default: 0 })
    guildCe: number;                         // 总战力
    
    @prop({ required: true, type: String, default: [], select: false })
    members: string[];                     // 军团成员的roleId，用于增加战力的时候加入总战力

    @prop({ required: true, type: String, default: [], select: false })
    invitedMembers: string[];               // 今天已邀请的玩家
    @prop({ required: true, default: new Date(), select: false })
    inviteTime: Date;               // 今天已邀请的玩家

    @prop({ required: true, type: Structure, default: getInitStructure(), _id: false })
    structure: Structure[]

    @prop({ required: true, default: GUILD_STATUS.RUNNING, enum: GUILD_STATUS })
    status: number;                      // 军团状态

    @prop({ required: true, default: 1, select: false })
    serverId: number;                 // 分服

    @prop({ required: true, default: 1 })
    trainId: number;                         // 试炼id

    @prop({ required: true, default: 1 })
    trainLv: number;                         // 试炼等级

    @prop({ required: true, default: () => { return getZeroPoint(SHOP_REFRESH_TYPE.WEEKLY) } })
    resetTrainTime: number;//上次刷新挑战训练场次数的时间

    @prop({ required: true, default: () => { return new Date()} })
    resetTrainTimeDaily: Date;

    @prop({ required: true, default: getZeroPointD })
    refOpenBossTime: Date;   // 开启boss本刷新时间

    @prop({ required: true, default: 0 })
    openBossCnt: number;   // 开启boss本次数

    @prop({ required: true })
    sdkMark: boolean;        // 37sdk标记有问题

    @prop({ required: true, default: '' })
    leagueCode: string;        // 参与联军

    public static async createGuild(params: { guildCode: string, name: string, icon: number, notice: string }, role: RoleType, serverId: number) {
        const doc = new GuildModel();
        const update = Object.assign(doc.toJSON(), params, { leader: role._id, members: [role.roleId], guildCe: role.ce, serverId, lvUpdateTime: nowSeconds() });
        delete update._id;
        const code = params.guildCode;
        const result: GuildType = await GuildModel.findOneAndUpdate({ code }, update, { upsert: true, new: true })
        .select({ _id: 0, __v: 0, createdAt: 0, updatedAt: 0 })
        .populate('leader', { roleId: 1, roleName: 1, head: 1, frame: 1, spine: 1, lv: 1, quitTime: 1, ce: 1, title: 1, serverId: 1, _id: 0 }, 'Role')
        .lean({ getters: true, virtuals: true });
        
        return result;
    }

    public static async checkName(name: string, serverId: number) {
        const result = await GuildModel.exists({ name, status: GUILD_STATUS.RUNNING, serverId });
        return result;
    }

    public static async getGuildList(page: number, showPeopleMax: boolean, name: string, serverId: number) {
        const condition = { status: GUILD_STATUS.RUNNING, serverId };
        if(!showPeopleMax) {
            condition['isMemberMax'] = false;
        }
        if(!!name) {
            condition['name'] = { $regex: new RegExp(name, 'i') }
        }
        const guildList: GuildType[] = await GuildModel.find(condition)
        .sort({ lv: -1, guildCe: -1 })
        .limit(GUILD_PER_PAGE).skip((page - 1) * GUILD_PER_PAGE)
        .select('code icon name lv memberCnt leader ceLimit isAuto')
        .populate('leader', { roleName: 1, _id: 0 }, 'Role')
        .lean({ getters: true, virtuals: true });
        return guildList;
    }


    public static async getRank(type: string, serverId: number, page: number = 1) {
        let sort = {}, condition = {};
        switch(type) {
            case REDIS_KEY.GUILD_LV_RANK:
                sort = { lv: -1, activeWeekly: -1, lvUpdateTime: 1 }; break;
            case REDIS_KEY.GUILD_ACTIVE_RANK:
                sort = { activeWeekly: -1, activeUpdateTime: 1 }; condition = { activeWeekly: { $gt: 0 } }; break;
        }

        const guildList: GuildType[] = await GuildModel.find({ status: GUILD_STATUS.RUNNING, serverId, ...condition }, {_id: 0})
        .sort(sort)
        .limit(100).skip((page - 1) * 100)
        .select(GUILD_SELECT.RANK)
        .populate('leader', {roleId: 1, roleName: 1, title: 1, frame: 1, head: 1, spine: 1, frames: 1, heads: 1, spines: 1, lv: 1, _id: 0}, 'Role')
        .lean({ virtuals: true });
        return guildList;
    }

    public static async findAllGuild(select?: string) {
        const guildList: GuildType[] = await GuildModel.find({ status: GUILD_STATUS.RUNNING }).select(select).lean();
        return guildList;
    }

    public static async findByCode(code: string, serverId?: number, select?: string) {
        let condition: any = { code, status: GUILD_STATUS.RUNNING };
        if(serverId) condition.serverId = serverId;
        const result: GuildType = await GuildModel.findOne(condition)
        .select(select)
        .populate('leader', {roleId: 1, roleName: 1, frame: 1, head: 1, spine: 1, frames: 1, heads: 1, spines: 1, lv: 1, quitTime: 1, ce: 1, title: 1}, 'Role')
        .lean({ getters: true, virtuals: true });
        return result;
    }

    public static async addMember(code: string, roleId: string, maxMemberCnt: number, serverId: number, ce: number) {
        let result: GuildType = await GuildModel.findOneAndUpdate({ code, memberCnt: {$lt: maxMemberCnt }, serverId }, { $inc: { memberCnt: 1, guildCe: ce }, $push: { members: roleId } }, { new: true }).lean({getters: true});
        if(result && result.memberCnt >= maxMemberCnt) {
            result = await GuildModel.findOneAndUpdate({ code }, { $set: { isMemberMax: true } }).lean({getters: true});
        }
        return result;
    }

    public static async notAddMember(code: string, roleId: string, ce: number) {
        let result: GuildType = await GuildModel.findOneAndUpdate({ code }, { $inc: { memberCnt: -1, guildCe: -ce }, $pull: { members: roleId }, $set: { isMemberMax: false } }, { new: true }).lean({getters: true});
        return result;
    }

    public static async dismiss(code: string, serverId: number) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code, status: GUILD_STATUS.RUNNING, serverId }, { status: GUILD_STATUS.DISMISSED }, { new: true })
        .populate('leader', {roleId: 1, _id: 0}, 'Role')
        .select('+members').lean();
        return result;
    }

    public static async quit(code: string, roleId: string, serverId: number, ce: number, isManage: boolean) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code, serverId }, { $inc: { memberCnt: -1, guildCe: -ce, managerCnt: isManage?-1:0}, $pull: { members: roleId }, isMemberMax: false }, { new: true }).lean({ getters: true });
        return result;
    }

    public static async updateInfo(code: string, update: GuildUpdateParam, incParam?: { managerCnt?: number, fund?: number, activeDaily?: number, activeWeekly?: number }, select?: string) {
        let param = { $set: update };
        if(incParam) param['$inc'] = incParam;
        const result: GuildType = await GuildModel.findOneAndUpdate({ code }, param, { new: true }).select(select).lean();
        return result;
    }

    public static async updateInfoWithLeader(code: string, update: GuildUpdateParam, incParam?: { managerCnt?: number, fund?: number, activeDaily?: number, activeWeekly?: number }, select?: string) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code }, { $set: update, $inc: incParam }, { new: true })
        .populate('leader', {roleId: 1, roleName: 1, frame: 1, head: 1, lv: 1, spine: 1, quitTime: 1, ce: 1, title: 1, _id: 0}, 'Role')
        .select(select).lean({ virtuals: true });
        return result;
    }

    public static async upStructure(code: string, id: number, cost: number, select?: string) {
        if (id == GUILD_STRUCTURE.ARMY_CENTER) {
            const result: GuildType = await GuildModel.findOneAndUpdate({ code, 'structure.id': id, fund: { $gte: cost } }, { $inc: { 'structure.$.lv': 1, lv: 1, fund: -1 * cost }, $set: { isMemberMax: false, lvUpdateTime: nowSeconds() } }, { new: true }).select(select).lean();
            return result;
        } else {
            const result: GuildType = await GuildModel.findOneAndUpdate({ code, 'structure.id': id, fund: { $gte: cost } }, { $inc: { 'structure.$.lv': 1, fund: -1 * cost } }, { new: true }).select(select).lean();
            return result;
        }
    }

    public static async costFund(code: string, cost: number) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code, fund: {$gte: cost}}, { $inc: { fund: -1 * cost } }, {new: true}).lean();
        return result;
    }

    public static async findGuild(code: string, serverId: number, select?: string, lean = true) {
        const result: GuildType = await GuildModel.findOne({ code, status: GUILD_STATUS.RUNNING, serverId }).select(select).lean(lean);
        return result;
    }

    public static async updateCe(roleId: string, inc: number, populate: boolean = false ) {
        let result = await GuildModel.findOneAndUpdate({ members: { $elemMatch: { $eq: roleId } }, status: GUILD_STATUS.RUNNING}, {$inc: {guildCe: inc}}, {new: true}).lean({getters: true});
        if(populate) {
            result = await GuildModel.findOneAndUpdate({ members: { $elemMatch: { $eq: roleId } }, status: GUILD_STATUS.RUNNING}, {$inc: {guildCe: inc}}, {new: true})
                .populate('leader', { roleId: 1, roleName: 1, head: 1, frame: 1, spine: 1, lv: 1, quitTime: 1, ce: 1, title: 1, _id: 0 }, 'Role').lean({getters: true});
        }
        return result;
    }
    /**
     * 每周一，5点刷新,若满足刷新条件，则重置试炼等级并返回
     * @param code 
     * @param serverId 
     * @param trainLv 
     * @param lean 
     */
    public static async resetGuildTrain(code: string, serverId: number, trainLv: number, lean = true) {
        const time = getZeroPoint(SHOP_REFRESH_TYPE.WEEKLY);//每周一，5点刷新
        const newTrainId = gameData.firstGuildTrainIdOfLv.get(trainLv);
        const result = await GuildModel.findOneAndUpdate({ code, status: GUILD_STATUS.RUNNING, serverId, resetTrainTime:{$ne:time}},{$set: {trainId: newTrainId, resetTrainTime: time, trainLv}}, {new: true}).lean(lean);
        return result;
    }

    // 记录已邀请过的人
    public static async recordInvitedMember(code: string, serverId: number, roleIds: string[], shouldRefresh: boolean) {
        let result: GuildType;
        if(shouldRefresh) {
            result = await GuildModel.findOneAndUpdate( { code, serverId }, { $set: { invitedMembers: roleIds, inviteTime: new Date() }}, { new: true})
            .select(GUILD_SELECT.INVITED_MEMBER)
            .lean();
        } else {
            result = await GuildModel.findOneAndUpdate( { code, serverId }, { $push: { invitedMembers: { $each: roleIds } }, $set: { inviteTime: new Date() }}, { new: true})
            .select(GUILD_SELECT.INVITED_MEMBER)
            .lean();
        }
        return result;
    }

    public static async findByCodeAndSetMark(code: string, serverId: number, select?: string, getters = false) {
        const role: RoleType = await GuildModel.findOneAndUpdate({ code, serverId }, { $set: { sdkMark: true } }, { new: true }).select(select).lean({ getters, virtuals: true });
        return role;
    }


    private static getSearchObj(form: SearchGuildParam) {
        let searchObj = {};
        if (form.serverId) searchObj['serverId'] = form.serverId;
        if (form.name) searchObj['name'] = { $regex: new RegExp(form.name.toString(), 'i') };
        return searchObj
    }

    public static async findByCondition(page: number, pageSize: number, sortField: string = 'updatedAt', sortOrder: string = 'descend', form: SearchGuildParam = {}) {

        let searchObj = this.getSearchObj(form);
        let sort = {};
        if (sortField && sortOrder) {
            if (sortOrder == 'ascend') {
                sort[sortField] = 1;
            } else if (sortOrder == 'descend') {
                sort[sortField] = -1;
            }
        }
        const result: GuildType[] = await GuildModel
            .find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort)
            .select('-_id -__v -updatedAt +serverId')
            .populate('leader', { roleId: 1, roleName: 1, _id: 0 }, 'Role')
            .lean({ getters: true, virtuals: true });
        return result;

    }

    public static async countByCondition(form: SearchGuildParam = {}) {

        let searchObj = this.getSearchObj(form);
        const result = await GuildModel.count(searchObj);
        return result;
    }

    public static async joinLeague(guildCode: string, leagueCode: string) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code: guildCode }, { $set: { leagueCode } }, { new: true }).lean();
        return result
    }

    public static async findGuildsForGVG(page: number, name: string, serverIds: number[]) {
        let query: any = { status: GUILD_STATUS.RUNNING, lv: { $gte: GVG.GVG_ARMY_LEAGUE_TIME }, leagueCode: '', serverId: { $in: serverIds } };
        if(name) {
            query['name'] = { $regex: new RegExp(name, 'i') }
        }
        const result: GuildType[] = await GuildModel.find(query)
        .populate('leader', { roleId: 1, roleName: 1, _id: 0 }, 'Role')
        .skip((page - 1) * 30).limit(30)
        .select('+serverId')
        .lean();
        return result;
    }

    public static async findByCodes(codes: string[]) {
        const result: GuildType[] = await GuildModel.find({ status: GUILD_STATUS.RUNNING, code: { $in: codes } })
        .populate('leader', { roleId: 1, roleName: 1, _id: 0 }, 'Role')
        .select('+serverId')
        .lean();
        return result;
    }

    public static async findByCodesWithoutPopulate(codes: string[]) {
        const result: GuildType[] = await GuildModel.find({ status: GUILD_STATUS.RUNNING, code: { $in: codes } }).select('+serverId').lean();
        return result;
    }
    public static async quitFromLeague(guildCode: string, leagueCode: string, select = null) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ code: guildCode, leagueCode }, { $set: { leagueCode: '' } }, { new: true }).select(select).lean();
        return result;
    }

    public static async dissmissLeague(leagueCode: string) {
        const result: GuildType = await GuildModel.findOneAndUpdate({ leagueCode }, { $set: { leagueCode: '' }}).lean();
        return result;
    }

    public static async leagueAutoDismiss(leagueCodes: string[]) {
        await GuildModel.updateMany({ leagueCode: { $in: leagueCodes } }, { $set: { leagueCode: '' }});
    }
}

export const GuildModel = getModelForClass(Guild);

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

function getInitStructure() {
    let structure = new Array<Structure>();
    for (let id = GUILD_STRUCTURE.START; id < GUILD_STRUCTURE.END; id++) {
        structure.push({ id, lv: 1 });
    }
    return structure;
}