import { Application, ChannelService } from 'pinus';
import { guildActivityStart, gateActivityEnd, cityActivityEnd, raceActivityEnd, guildActivitySchedule, auctionSchedule, initMaintenance, stopMaintenance, initAutoCreateServer, addMailsToSchedule, updateTimeLimitRank, setLadderCountDown, cancelLadderCountDown, initSumSchedule, setPvpSeasonSchedule, initGVGConfigSchedule, gvgBattleStartSchedule, gvgBattleEndSchedule } from '../../../services/timeTaskService';
import PvpDefenseType from '../../../db/PvpDefense';
import { DicGuildActivity } from '../../../pubUtils/dictionary/DicGuildActivity';
import { reloadResources } from '../../../pubUtils/data';
import { setMarquee, cancelMarquee } from '../../../services/gmService';
import { setDicAuctionTime, setDicGuildActivity } from '../../../services/guildActivity/guildActivityService';
import { setWeek } from '../../../pubUtils/timeUtil';
import { ServerlistType } from '../../../db/Serverlist';
import { MarqueeType } from '../../../db/Marquee';
import { taflush } from '../../../services/sdkService';
import { RegionType } from '../../../db/Region';
import { errlogger } from '../../../util/logger';
import { MailType } from '../../../db/Mail';
import { GroupMailType } from '../../../db/GroupMail';
import { ServerMailType } from '../../../db/ServerMail';
import { ActivityModelType } from '../../../db/Activity';
import { GUILD_ACTIVITY_TYPE, LADDER_STATUS } from '../../../consts';
import { setApiIsClose } from '../../../services/chatService';
import { setHiddenData } from '../../../services/dataService';
import { setKvToMemory } from '../../../services/pushService';
import { setGVGServerGroup } from '../../../services/gvg/gvgService';

export default function (app: Application) {
    return new SystimerRemote(app);
}

export class SystimerRemote {

    constructor(private app: Application) {
        this.app = app;
        this.channelService = app.get('channelService');
    }
    private channelService: ChannelService;

    public async guildActivityStart(dicGuildActivity: DicGuildActivity) {
        try {
            return await guildActivityStart(dicGuildActivity);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async gateActivityEnd() {
        try {
            return await gateActivityEnd();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async cityActivityEnd() {
        try {
            return await cityActivityEnd();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async raceActivityEnd() {
        try {
            return await raceActivityEnd();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }
        
    /**
     * 重载json资源
     */
    public async reloadResources() {
        try {
            reloadResources();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * @description 设置跑马灯配置
     * @param code 跑马灯唯一code
     */
    public async setMarquee(marquee: MarqueeType) {
        try {
            return await setMarquee(marquee);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async cancelMarquee(code: string) {
        try {
            return await cancelMarquee(code);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 改变字典表中的军团活动开始时间&持续时间
     * @param {number} aid 活动 
     * @param {number} startSeconds 从现在开始几秒后开启活动
     * @param {number} endSeconds 从开始之后几秒后结束活动
     */
     public async setDicGuildActivity(now: number, aid: number, startSeconds: number, endSeconds: number) {
        try {
            setDicGuildActivity(now, aid, startSeconds, endSeconds);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 改变字典表中的拍卖行时间
     */
     public async setDicAuctionTime(startTime: number, endActivity: number, startGuild: number, endGuild: number, startWorld: number, endWorld: number) {
        try {
            setDicAuctionTime(startTime, endActivity, startGuild, endGuild, startWorld, endWorld);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setGuildActivitySchedule() {
        try {
            await guildActivitySchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setAuctionSchedule() {
        try {
            auctionSchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * !! 注意，这条函数会改变内存时间，影响甚大，请考虑清楚后使用
     */
     async setDay(week: number) {
        try {
            setWeek(week);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async initMaintenance(servers?: ServerlistType[]) {
        try {
            await initMaintenance(servers);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async stopMaintenance(batchCode: string, serverIds: number[]) {
        try {
            await stopMaintenance(batchCode, serverIds);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public taflush() {
        try {
            return taflush();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public initAutoCreateServer(region: RegionType) {
        try {
            return initAutoCreateServer(region);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async addMailsToSchedule(mails: MailType[], groupMails: GroupMailType[], serverMails: ServerMailType[]) {
        try {
            await addMailsToSchedule(mails, groupMails, serverMails);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async updateTimeLimitRank(activities: ActivityModelType[]) {
        try {
            await updateTimeLimitRank(activities);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async initSumSchedule() {
        try {
            await initSumSchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setLadderCountDown(battleCode: string, time: number, status: LADDER_STATUS) {
        try {
            await setLadderCountDown(battleCode, time, status);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async cancelLadderCountDown(battleCode: string) {
        try {
            await cancelLadderCountDown(battleCode);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setApiIsClose(isClose: boolean) {
        try {
            setApiIsClose(isClose);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setPvpSeasonSchedule(fromBackend: boolean) {
        try {
            setPvpSeasonSchedule(fromBackend);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setHiddenData(heroes: number[], goods: number[], refTime: number) {
        try {
            setHiddenData(heroes, goods, refTime);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setKvToMemory(originK: string, originV: string, aesKey: string, aesIV: string, now: number) {
        try {
            setKvToMemory(originK, originV, aesKey, aesIV, now);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async initGVGConfigSchedule() {
        try {
            await initGVGConfigSchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        } 
    }

    public async gvgBattleStartSchedule() {
        try {
            await gvgBattleStartSchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        } 
    }

    public async gvgBattleEndSchedule() {
        try {
            await gvgBattleEndSchedule();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        } 
    }

    public async setGVGServerGroup() {
        try {
            await setGVGServerGroup();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        } 
    }
}
