import { COUNTER } from './../consts';
import { CounterAllModal } from './CounterAll';
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';
import ServerStategy from './ServerStategy';
import { ServerlistType } from './Serverlist';


/**
  * 大区数据
*/
@index({ id: 1 })
@index({ env: 1 })

export default class Region extends BaseModel {
    @prop({ required: true })
    id: number;                // 大区id

    @prop({ required: true })
    name: string;              // 大区名

    @prop({ required: true })
    prefix: string;            // 区名前缀

    @prop({ required: true })
    env: string;               // 环境变量

    @prop({ required: true })
    gmLink: string;            // 短链接地址

    @prop({ required: true })
    gameHost: string;          // 长链接地址

    @prop({ required: true })
    gmPort: number;          // 长链接地址

    @prop({ required: true })
    webHost: string;          // 短链接

    @prop({ required: true })
    latestServer: number;          // 最新服

    @prop({ required: true })
    minVersion: string;          // 支持的最小版本号

    @prop({ required: true })
    curVersion: string;         // 当前版本号，格式 x.x.x.x

    @prop({ required: true })
    whitelistVersion: string;          // 白名单版本号

    @prop({ required: true }) // 热更新资源根目录
    updateResUrl: string;

    @prop({ required: true })
    latestServerUniqId: number;       // 最新服唯一id

    @prop({ required: true })
    addressType: number;            // 参数表的那个addressType

    @prop({ required: true })
    serverCount: number;          // 总数

    @prop({ required: true })
    remark: string;          // 备注

    @prop({ required: true, type: () => ServerStategy, _id: false })
    stategy: ServerStategy;     // 策略配置

    @prop({ required: true })
    reviewEnv: string;               // 对应的审核服

    @prop({ required: true })
    reviewVersion: string;               // 大于这个版本的都是审核服

    @prop({ required: true })
    isCloseApi: boolean;               // 大于这个版本的都是审核服

    public static async createNewRegion(params: RegionUpdate, uid = 1) {
        let id = await CounterAllModal.getNewCounter(COUNTER.REGION);

        const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, { $setOnInsert: { ...params, createdBy: uid}, $set: { updatedBy: uid } }, { new: true, upsert: true }).lean();
        return rec;
    }

    public static async updateRegion(id: number, params: RegionUpdate, uid = 1) {
        const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, { $set: { ...params, updatedBy: uid } }, { new: true }).lean();
        return rec;
    }

    public static async newServer(id: number, newServer: ServerlistType, uid = 1) {
        const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, { 
            $set: { latestServer: newServer.serverId, latestServerUniqId: newServer.id, updatedBy: uid },
            $inc: { serverCount: 1 }
        }, { new: true }).lean();
        return rec;
    }
    public static async getAllRegion() {
        const rec: RegionType[] = await RegionModel.find().select('-_id -stategy').lean();
        return rec;
    }

    public static async findRegionById(id: number) {
        const rec: RegionType = await RegionModel.findOne({ id }).select('-_id').lean();
        return rec;
    }

    public static async findRegionByEnv(env: string) {
        const rec: RegionType = await RegionModel.findOne({ env }).select('-_id').lean();
        return rec;
    }

    public static async findRegionByEnvs(envs: string[]) {
        const rec: RegionType[] = await RegionModel.find({ env: { $in: envs } }).select('-_id').lean();
        return rec;
    }
}

export let RegionModel: ReturnModelType<typeof Region, {}>;
export function loadRegionModel(connect: mongoose.Connection) {
    RegionModel = getModelForClass(Region, {
        existingConnection: connect
    });
}

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