import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { CounterModel } from './Counter';
import { COUNTER } from '../consts';
import { RoleModel } from './Role';

/**
  * 宝物 
*/
@modelOptions({ schemaOptions: { id: false } })
@index({ roleId: 1, seqId: 1, id: 1 })
@index({ roleId: 1, quality: 1 })
@index({ batchCode: 1 })
@index({ status: 1 })

export default class Artifact extends BaseModel {

    // 主键： artifact，不同形态的宝物分开词条
    @prop({ required: true })
    seqId: number;     // 唯一id

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

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

    @prop({ required: true })
    id: number;        // 物品id

    @prop({ required: true })
    artifactId: number; // 宝物id

    @prop({ required: true, default: 0 })
    lv: number;        // 强化等级

    @prop({ required: true })
    quality: number;   // 品质 1-5 蓝紫橙红金

    @prop({ required: true })
    qualityStage: number; // 品质+n，0开始

    @prop({ required: true, default: 0 })
    hid: number;        // 装备的武将

    @prop({ required: true })
    batchCode: string;     // 一键合成批处理

    @prop({ required: true, default: 1 })
    status: number;     // 装备 1-生成 0-被合成删除

    public static async findbySeqIds(roleId: string, seqIds: number[], select?: string) {
        const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, seqId: { $in: seqIds }, status: 1 }).select(select).lean();
        return result;
    }

    public static async findbySeqId(roleId: string, seqId: number, select?: string) {
        const result: ArtifactModelType = await ArtifactModel.findOne({ roleId, seqId, status: 1 }).select(select).lean();
        return result;
    }


    public static async findbyRole(roleId: string, select = '') {
        const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, status: 1 }).select(select).lean();
        return result;
    }

    public static async createArtifact(artifactInfo: ArtifactModelUpdate) {
        const seqId = await CounterModel.getNewCounter(COUNTER.ARTIFACT_ID);

        const doc = new ArtifactModel();
        const update = Object.assign(doc.toJSON(), seqId, artifactInfo);
        delete update._id;
        const artifact: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ seqId }, update, { upsert: true, new: true }).lean();
        return artifact;
    }

    public static async createArtifacts(roleId: string, artifactInfos: ArtifactModelUpdate[]) {
        let result: ArtifactModelType[] = [];
        for (let artifactInfo of artifactInfos) {
            let artifact = await this.createArtifact(artifactInfo);
            result.push(artifact);
        }
        await RoleModel.increaseArtifact(roleId, result.length);
        return result;
    }

    public static async putOnOrOff(roleId: string, seqId: number, hid: number) {
        let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: { hid } }, { new: true }).lean();
        return rec;
    }

    public static async deleteBySeqIds(roleId: string, seqIds: number[]) {
        let result: ArtifactModelType[] = await ArtifactModel.findbySeqIds(roleId, seqIds);
        let delResult: { n: number, nModified: number, ok: number } = await ArtifactModel.updateMany({ roleId, seqId: { $in: seqIds } }, { $set: { status: 0 } });
        await RoleModel.increaseArtifact(roleId, -1 * delResult.nModified);
        return result;
    }

    public static async updateInfoBySeqId(roleId: string, seqId: number, update: ArtifactModelUpdate) {
        let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: update }, { new: true }).lean();
        return rec;
    }

    public static async findByQuality(roleId: string, quality: number) {
        let result: ArtifactModelType[] = await ArtifactModel.find({ roleId, quality, status: 1, hid: 0, qualityStage: 0 }).lean();
        return result;
    }

    public static async findByBatchCode(batchCode: string) {
        let result: ArtifactModelType[] = await ArtifactModel.find({ batchCode, status: 1 }).lean();
        return result;
    }
}

export const ArtifactModel = getModelForClass(Artifact);

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