import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { SearchUserLogParam } from '../domain/backEndField/search';

class MalReward {
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    count: number;
}

export class GateHpLog {
    @prop({ required: false })
    activePlayerCnt: number;
    @prop({ required: false })
    activePlayerCe: number;
    @prop({ required: false })
    activeTopPlayerCnt: number;
    @prop({ required: false })
    activeGuildCnt: number;
    @prop({ required: false })
    atk: number;
    @prop({ required: false })
    A: number;
    @prop({ required: false })
    hpBase: number;
    @prop({ required: false })
    N: number;
    @prop({ required: false })
    gateHp: number;
}

export class BossHpLog {
    @prop({ required: false })
    minBossHp: number;
    @prop({ required: false })
    bossHpRatio: number;
    @prop({ required: false })
    hp: number;
    @prop({ required: false })
    activeCe: number;
    @prop({ required: false })
    playerCnt: number;
    @prop({ required: false })
    subCe: number;
    @prop({ required: false })
    B: number;
    @prop({ required: false })
    bossHp: number;
}

/**
  * 玩家充值订单 
*/
@index({ createdAt: -1, type: 1 })
@index({ roleId: 1 })

export default class UserLog extends BaseModel {

    @prop({ required: true })
    type: string;		    // 类型 LOG_TYPE

    @prop({ required: false })
    uid: number;            // uid

    @prop({ required: false })
    serverId: number;		    // 小区id

    @prop({ required: false })
    roleId: string;		    // 角色id

    @prop({ required: false })
    roleName: string;		    // 角色名

    @prop({ required: false })
    ip: string;               // ip

    // 战力相关
    @prop({ required: false })
    inc: number;               // 改变值 战力改变/道具数量改变

    @prop({ required: false })
    count: number;               // 变化后的值 战力改变/道具数量改变

    @prop({ required: false })
    ceChangeReason: number;      // 战力变化原因

    @prop({ required: false, type: String })
    ceChangeTxt: string[];      // 战力变化原因信息，如武将id等

    // 充值相关
    @prop({ required: false })
    productID: string;               // 商品id

    @prop({ required: false })
    productName: string;               // 商品名字

    @prop({ required: false })
    price: number;               // 充值金额

    @prop({ required: false })
    isYuanbao: boolean;               // 是否为代币

    @prop({ required: false })
    orderID: string;               // 平台订单id

    @prop({ required: false })
    localOrderID: string;               // 订单id

    @prop({ required: false })
    totalPay: number;               // 总充值金额

    @prop({ required: false })
    orderStatus: number;               // 订单状态

    @prop({ required: false })
    useVoucher: boolean;           // 是否使用代金券

    // 道具相关
    @prop({ required: false })
    itemId: number;               // 道具id

    @prop({ required: false })
    itemName: string;               // 道具名

    @prop({ required: false })
    itid: number;               // 道具类型

    @prop({ required: false })
    itemChangeReason: number;         // 道具变更来源

    // 邮件日志
    @prop({ required: false })
    mailId: string;               // 邮件id

    @prop({ required: false, type: MalReward, _id: false })
    goods: MalReward[];               // 邮件附带道具

    @prop({ required: false })
    mailContent: string;               // 邮件正文

    @prop({ required: false })
    mailTitle: string;               // 邮件标题

    @prop({ required: false })
    mailSendName: string;               // 邮件发件人

    // 诸侯混战城门血条记录
    @prop({ required: false })
    cityId: number;               // 城id

    @prop({ required: false, type: GateHpLog, _id: false })
    gateHp: GateHpLog;               // 诸侯混战血量参数

    // 军团boss血量记录
    @prop({ required: false })
    guildCode: string;               // 军团code

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

    @prop({ required: false, type: BossHpLog, _id: false })
    bossHp: BossHpLog;               // 诸侯混战血量参数

    // 重生记录
    @prop({ required: false })
    hid: number;               // 武将id

    @prop({ required: false })
    heroInfo: string;               // 武将信息

    // 内存
    @prop({ required: false })
    sid: string;                  // 线程

    @prop({ required: false })
    memoryLogField: string;        // 类型

    @prop({ required: false })
    memoryData: string;           // 内存数据

    public static async createRecord(params: UserLogModelTypeParam) {
        const r = await UserLogModel.insertMany(params);
        return r;
    }

    public static async createRecords(params: UserLogModelTypeParam[]) {
        const r = await UserLogModel.insertMany(params);
        return r;
    }

    private static getSearchObj(form: SearchUserLogParam) {
        let searchObj = {};
        if (form.type) searchObj['type'] = form.type;
        if (form.serverId) searchObj['serverId'] = form.serverId;
        if (form.uid) searchObj['uid'] = form.uid;
        if (form.roleId) searchObj['roleId'] = form.roleId;
        if(form.roleName) searchObj['roleName'] = { $regex: new RegExp(form.roleName.toString(), 'i') };
        if (form.guildCode) searchObj['guildCode'] = form.guildCode;
        if (form.itemId) searchObj['itemId'] = form.itemId;
        if (form.itemChangeReason != undefined) searchObj['itemChangeReason'] = form.itemChangeReason;
        if (form.ceChangeReason != undefined) searchObj['ceChangeReason'] = form.ceChangeReason;
        if (form.createTimeStart && form.createTimeEnd) {
            searchObj['createdAt'] = { $lte: new Date(form.createTimeEnd * 1000), $gte: new Date(form.createTimeStart * 1000) };
        }
        return searchObj
    }

    public static async findByCondition(page: number, pageSize: number, form: SearchUserLogParam = {}) {
        let searchObj = UserLogModel.getSearchObj(form);
        const result: UserLogModelType[] = await UserLogModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort({ createdAt: -1 }).select('-_id').lean({ getters: true, virtuals: true });
        return result;

    }
}

export let UserLogModel = getModelForClass(UserLog);

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