import { GIFT_GENERATE_TYPE, GM_MAIL_TYPE, MAIL_TIME_TYPE, SERVER_TIMER } from "../../consts";
import { isArray, isNumber, isString } from 'underscore';
import ServerStategy, { GMMail } from "../../db/ServerStategy";
import { RegionType } from "../../db/Region";
import { RewardInter } from "../../pubUtils/interface";
import { isTimestamp, parseNumberList } from '../../pubUtils/util';
import { isBoolean, isDate } from "util";

export class UpdateMailParams {
    hasGoods: boolean = false;                // 是否有道具
    goods: {id: number; count: number}[];
    timeType: MAIL_TIME_TYPE;          // 邮件时间类型
    expire: number;                 // 有效时间，单位小时
    startTime: number;               // 发送时间，延时邮件使用
    circleStart: number;               // 循环邮件开始循环时间
    circleEnd: number;               // 循环邮件结束循环时间
    circleDay: number;               // 循环时间，每周几，0表示每天
    circleHour: string;               // 几点发送
    title: string;
    content: string;
    sendName: string;
    mailType: GM_MAIL_TYPE;         // 收件人类型
    receivers: {env: string; serverId: number; roleId: string; roleName: string; }[];
    reason: string;                   // 原因
    isSp: boolean = false;                   // 特殊邮件
    isSingle: boolean;

    constructor(obj: UpdateMailParams) {
        this.goods = obj.goods;
        this.hasGoods = obj.goods?.length > 0;
        this.timeType = obj.timeType;
        this.expire = obj.expire;
        this.startTime = obj.startTime;
        this.circleStart = obj.circleStart;
        this.circleEnd = obj.circleEnd;
        this.circleDay = obj.circleDay;
        this.circleHour = obj.circleHour;
        this.title = obj.title;
        this.content = obj.content;
        this.sendName = obj.sendName;
        if(obj.isSingle == true) {
            this.mailType = GM_MAIL_TYPE.SINGLE;
        } else if (obj.isSingle == false) {
            this.mailType = GM_MAIL_TYPE.SERVER;
        }
        this.receivers = obj.receivers;
        this.reason = obj.reason;
    }

    checkParams() {
        if(!this.title || !this.content || !this.sendName) return false;
        if(this.reason && !isString(this.reason)) return false;
        if(this.timeType == MAIL_TIME_TYPE.IMMEDIATE) {
            if(!this.expire) return false;
        } else if (this.timeType == MAIL_TIME_TYPE.DELAY) {
            if(!this.expire || !this.startTime) return false;
        } else if (this.timeType == MAIL_TIME_TYPE.CIRCLE) {
            if(!this.expire || !this.circleStart || !this.circleEnd || this.circleDay == undefined || !this.circleHour)
                return false;
        }
        if(!this.receivers || !isArray(this.receivers)) return false;
        if(!this.mailType) return false;
        for(let { serverId, roleId, roleName } of this.receivers) {
            if(!serverId) return false;
            if(this.mailType == GM_MAIL_TYPE.SINGLE||this.mailType == GM_MAIL_TYPE.GROUP) {
                if(!roleId || !roleName) return false;
            }
        }
        return true;
    }
}

export class UpdateRegionParams {
    id: number|'new' = 0;                // 大区id
    name: string = '';              // 大区名
    prefix: string = '';            // 区名前缀
    remark: string = '';
    minVersion: string = '';
    whitelistVersion: string = '';
    curVersion: string = '';
    updateResUrl: string = '';
    reviewVersion: string = '';
    reviewEnv: string = '';
    addressType: number = 0;

    maxPlayerCnt: number = 0;
    timers: SERVER_TIMER[] = [];
    activityGroupId: number[] = [];
    openMail?: GMMail;
    circleMail?: GMMail;
    stopRegisterTime: number = 0;
    isOpen: boolean = false;

    hasOpenMail: boolean = false;
    hasCircleMail: boolean = false;

    constructor(obj: UpdateRegionParams) {
        for(let key in obj) {
            this[key] = obj[key];
        }
    }

    checkParams() {
        if(!this.id || !this.name || !this.prefix || !isString(this.minVersion)|| !isString(this.whitelistVersion) || !isString(this.curVersion) || !isString(this.updateResUrl)) {
            // console.log('1111', !this.id, !this.name, !this.prefix, !isString(this.minVersion),!isString(this.curVersion),  !isString(this.updateResUrl))
            return false
        }
        if(this.isOpen && (!this.maxPlayerCnt || !isArray(this.timers) || this.timers.length <= 0 || !isArray(this.activityGroupId) || this.activityGroupId.length <= 0 )) {
            // console.log('2222')
            return false
        } 
        return true;
    }

    getUpdateParam(oldRegion?: RegionType) {
        if(!oldRegion) {
            let stategy = new ServerStategy(this);
            return {
                name: this.name,
                prefix: this.prefix,
                remark: this.remark,
                minVersion: this.minVersion,
                whitelistVersion: this.whitelistVersion,
                curVersion: this.curVersion,
                reviewVersion: this.reviewVersion,
                reviewEnv: this.reviewEnv,
                updateResUrl: this.updateResUrl,
                addressType: this.addressType,
                stategy
            }
        } else {
            let stategy = new ServerStategy(this);
            return {
                name: this.name||oldRegion.name,
                prefix: this.prefix||oldRegion.prefix,
                remark: this.remark||oldRegion.remark,
                minVersion: this.minVersion||oldRegion.minVersion,
                whitelistVersion: this.whitelistVersion||oldRegion.whitelistVersion,
                curVersion: this.curVersion||oldRegion.curVersion,
                reviewVersion: this.reviewVersion||oldRegion.reviewVersion,
                reviewEnv: this.reviewEnv||oldRegion.reviewEnv,
                updateResUrl: this.updateResUrl||oldRegion.updateResUrl,
                addressType: this.addressType||oldRegion.addressType,
                stategy: { ...(oldRegion.stategy||{}), ...stategy }
            }
        }
    }
}

export class CreateRegionParam {
    name: string = '';              // 大区名
    prefix: string = '';            // 区名前缀
    remark: string = '';
    minVersion: string = '';
    whitelistVersion: string = '';
    env: string = '';               // 环境变量
    gmLink: string;                 // 对应后台链接
    gameHost: string;               // 长链接
    webHost: string;               // 短链接
    gmPort: number;                 // 后台使用的connector端口
    updateResUrl: string;           // 热更新资源根目录
    addressType: number;           // 热更新资源根目录

    constructor(obj: any) {
        for(let key in obj) {
            this[key] = obj[key];
        }
    }

    checkParams() {
        if(!this.name || !this.prefix || !this.env || !this.gmLink || !this.gameHost || !this.gmPort || !this.webHost || !isNumber(this.gmPort) || !isString(this.minVersion) || !isString(this.whitelistVersion) || !this.updateResUrl) {
            return false
        }
        return true;
    }
}

export class CreateServerParam {
    env: string = '';
    openTime: number = 0;
    activityGroupId: number[] = [];
    hasOpenMail: boolean = false;
    openMail?: GMMail;
    hasCircleMail: boolean = false;
    circleMail?: GMMail;
    stopRegisterTime: number = 0;

    constructor(obj?: any) {
        if(obj) {
            for(let key in obj) {
                this[key] = obj[key];
            }
        }
    }

    setByRegionStategy(region: RegionType, openTime: number) {
        this.env = region.env;
        this.openTime = openTime;
        if(region.stategy) {
            this.activityGroupId = region.stategy.activityGroupId;
            this.hasOpenMail = !!region.stategy.openMail;
            this.openMail = region.stategy.openMail;
            this.hasCircleMail = !!region.stategy.circleMail;
            this.circleMail = region.stategy.circleMail;
            this.stopRegisterTime = region.stategy.stopRegisterTime;
        }
        
    }

    checkParams() {
        // console.log('##### createNew', this.env, this.openTime, this.stopRegisterTime, this.hasOpenMail, this.hasCircleMail)
        if(!this.env || !this.openTime  || !this.stopRegisterTime ) {
            return false
        }
        if(this.hasOpenMail && !this.openMail) return false;
        if(this.hasCircleMail && !this.circleMail) return false;
        return true;
    }
}

export class CreateGiftCode {
    name: string;           // 礼包码名
    goods: RewardInter[];         // 奖励
    beginTime: number;         // 开始时间
    endTime: number;           // 结束时间
    codeLen: number;           // 礼包码位数
    remark: string = '';           // 备注
    generateType: GIFT_GENERATE_TYPE;         // 生成类型
    channel: string[] = ['all'];
    code: string = '';

    constructor(obj: any) {
        this.name = obj.name;
        this.goods = obj.goods;
        this.beginTime = obj.beginTime;
        this.endTime = obj.endTime;
        this.codeLen = obj.codeLen;
        this.remark = obj.remark;
        this.generateType = obj.generateType;
        this.channel = obj.channel;
        this.code = obj.code;
    }

    checkParams() {
        if(!isString(this.name)) return false;
        if(!isArray(this.goods)) return false;
        for(let { id, count } of this.goods) {
            if(!isNumber(id) || !isNumber(count)) return false;
        }
        if(!isTimestamp(this.beginTime) || !isTimestamp(this.endTime)) return false;
        if(!isNumber(this.codeLen) || this.codeLen <= 0) return false;

        if(this.generateType != GIFT_GENERATE_TYPE.ONE_TO_MANY && this.generateType != GIFT_GENERATE_TYPE.ONE_TO_ONE) {
            return false
        }
        if(this.code != undefined && !isString(this.code)) return false
        if(!isArray(this.channel)) return false;
        return true;
    }
}

export class UpdateGiftCode {
    id: number;
    name: string;           // 礼包码名
    goods: RewardInter[];         // 奖励
    beginTime: number;         // 开始时间
    endTime: number;           // 结束时间
    codeLen: number;           // 礼包码位数
    remark: string = '';           // 备注

    constructor(id: number, obj: any) {
        this.id = id;
        this.name = obj.name;
        this.goods = obj.goods;
        this.beginTime = obj.beginTime;
        this.endTime = obj.endTime;
        this.codeLen = obj.codeLen;
        this.remark = obj.remark;
    }

    checkParams() {
        if(!isNumber(this.id) || this.id <= 0) return false
        if(!isString(this.name)) return false;
        if(!isArray(this.goods)) return false;
        for(let { id, count } of this.goods) {
            if(!isNumber(id) || !isNumber(count)) return false;
        }
        if(!isTimestamp(this.beginTime) || !isTimestamp(this.endTime)) return false;
        if(!isNumber(this.codeLen) || this.codeLen <= 0) return false;
        return true;
    }
}

export class GuildFormParam {
    code: string;
    name: string;
    notice: string;
    fund: number;
    lv: number;
    equipProduce: number;
    boss: number;
    train: number;
    wishPool: number;
    store: number;
    donate: number;
    trainLv: string;
    
    constructor(obj?: any) {
        if(obj) {
            for(let key in obj) {
                this[key] = obj[key];
            }
        }
    }

    checkParams() {
        if(!this.code || !isString(this.code)) return false;
        if(this.name && !isString(this.name)) return false;
        if(this.notice && !isString(this.notice)) return false;
        if(this.fund && !isNumber(this.fund)) return false;
        if(this.lv && !isNumber(this.lv)) return false;
        if(this.equipProduce && !isNumber(this.equipProduce)) return false;
        if(this.boss && !isNumber(this.boss)) return false;
        if(this.train && !isNumber(this.train)) return false;
        if(this.wishPool && !isNumber(this.wishPool)) return false;
        if(this.store && !isNumber(this.store)) return false;
        if(this.donate && !isNumber(this.donate)) return false;
        if(this.trainLv && !isString(this.trainLv)) return false;
        
        return true;
    }
}

export class SetHeroParam {
    lv?: number;
    expInc?: number;
    star?: number;
    quality?: number;
    colorStar?: number;
    job?: number;

    constructor(obj?: any) {
        if(obj) {
            for(let key in obj) {
                this[key] = obj[key];
            }
        }
    }

    checkParams() {
        if(this.lv && !isNumber(this.lv)) return false;
        if(this.expInc && !isNumber(this.expInc)) return false;
        if(this.star && !isNumber(this.star)) return false;
        if(this.quality && !isNumber(this.quality)) return false;
        if(this.colorStar && !isNumber(this.colorStar)) return false;
        return true;
    }
}


export class UpdateActivityParam {
    activityIds: number[] = [];
    groupId: number;
    beginTime: Date;
    endTime: Date;
    type: number;
    data: string;
    timeType: number;
    days: number;
    delayDay: number;
    interval: number;
    name: string;
    hideDayByServer: number;
    effectDay: number;
    multiTime: { id: number, beginTime: Date, endTime: Date}[];

    constructor(obj?: any) {
        if(!obj) return;
         if (typeof obj.activityId == 'number') {
            this.activityIds.push(obj.activityId);
        } else if (typeof obj.activityId == 'string'){
            obj.activityId.split(',').forEach(aidStr => {
                this.activityIds.push(parseInt(aidStr));
            });
        }
        if(obj.groupId)this.groupId = obj.groupId;
        if(obj.beginTime) this.beginTime = new Date(obj.beginTime);
        if(obj.endTime) this.endTime = new Date(obj.endTime);
        if(obj.type) this.type = obj.type;
        if(obj.data) this.data = obj.data;
        if(obj.timeType) this.timeType = obj.timeType;
        if(obj.days) this.days = obj.days;
        if(obj.delayDay) this.delayDay = obj.delayDay;
        if(obj.interval) this.interval = obj.interval;
        if(obj.name) this.name = obj.name;
        if(obj.hideDayByServer) this.hideDayByServer = obj.hideDayByServer;
        if(obj.effectDay) this.effectDay = obj.effectDay;
        if(obj.multiTime && obj.multiTime.length) {
            this.multiTime = [];
            for(let { id, beginTime, endTime } of obj.multiTime) {
                this.multiTime.push({ id, beginTime: new Date(beginTime), endTime: new Date(endTime)})
            }
        }
    }

    checkParams() {
        if(this.activityIds && this.activityIds.length && this.activityIds.find(activityId => !isNumber(activityId))) return false;
        if(this.groupId && !isNumber(this.groupId)) return false;
        if(this.beginTime && !isDate(this.beginTime)) return false;
        if(this.endTime && !isDate(this.endTime)) return false;
        if(this.type && !isNumber(this.type)) return false;
        if(this.data && !isString(this.data)) return false;
        if(this.timeType && !isNumber(this.timeType)) return false;
        if(this.days && !isNumber(this.days)) return false;
        if(this.delayDay && !isNumber(this.delayDay)) return false;
        if(this.interval && !isNumber(this.interval)) return false;
        if(this.name && !isString(this.name)) return false;
        if(this.hideDayByServer && !isNumber(this.hideDayByServer)) return false;
        if(this.effectDay && !isNumber(this.effectDay)) return false;
        if(this.timeType == 4 && !isArray(this.multiTime)) return false;
        return true;
    }

    checkTime() {
        if(this.timeType == 4) {
            let t = undefined;
            for(let { beginTime, endTime } of this.multiTime) {
                if(beginTime > endTime) return false;
                if(t && beginTime < t) return false;
                t = endTime;
            }
        }
        return true;
    }
}


export class UpdateChannelParam {
    code: string;              // 渠道标识
    platform: string;              // 渠道标识
    desc: string;                   // 渠道描述
    isDefaultPolicy: boolean;        // 是否使用默认协议
    userPolicyLink: string;        // 用户协议
    privacyPolicyLink: string;      // 隐私协议

    constructor(obj?: any) {
        if(!obj) return;
        this.code = obj.code;
        this.platform = obj.platform;
        this.desc = obj.desc;
        this.isDefaultPolicy = obj.isDefaultPolicy;
        this.userPolicyLink = obj.userPolicyLink;
        this.privacyPolicyLink = obj.privacyPolicyLink;
    }

    checkParams() {
        if(!this.code || !isString(this.code)) return false;
        if(!this.platform || !isString(this.platform)) return false;
        if(this.desc && !isString(this.desc)) return false;
        if(!isBoolean(this.isDefaultPolicy)) return false;
        if(this.userPolicyLink && !isString(this.userPolicyLink)) return false;
        if(this.privacyPolicyLink && !isString(this.privacyPolicyLink)) return false;
        return true;
    }
}


export class CreatePvpConfigParam {
    env: string = '';
    seasonNum: number|'new' = 0;
    seasonStartTime: number = 0;
    seasonEndTime: number = 0;
    seasonRewardTime: number = 0;
    warIds: string = '';

    constructor(obj?: any) {
        if(obj) {
            for(let key in obj) {
                this[key] = obj[key];
            }
        }
    }

    checkParams() {
        // console.log('##### createNew', this.env, this.openTime, this.stopRegisterTime, this.hasOpenMail, this.hasCircleMail)
        if(this.seasonNum != 'new' && !isNumber(this.seasonNum)) return false; 
        if(!this.env || !isNumber(this.seasonStartTime) || !isNumber(this.seasonEndTime) || !isNumber(this.seasonRewardTime) || !isString(this.warIds)) {
            return false
        }
        return true;
    }

    getUpdateParam() {
        let { seasonStartTime, seasonEndTime, seasonRewardTime, warIds } = this;
        return { seasonStartTime, seasonEndTime, seasonRewardTime, warIds: parseNumberList(warIds)}
    }
}

export class UpdateHiddenDataParam {
    env: string = '';
    arr: { type: number, id: number }[] = [];
    publishTime: number = 0;
    constructor(obj?: any) {
        if(obj) {
            for(let key in obj) {
                this[key] = obj[key];
            }
        }
    }

    checkParams() {
        // console.log('##### createNew', this.env, this.openTime, this.stopRegisterTime, this.hasOpenMail, this.hasCircleMail)
        if(!isArray(this.arr)) return false;
        for(let { type, id } of this.arr) {
            if(!isNumber(type) || !isNumber(id)) return false;
        }
        if(!this.env || !isNumber(this.publishTime)) {
            return false
        }
        return true;
    }
}