import { Application, ChannelService, HandlerService, } from 'pinus';
import { sendAllGuildRanks, calWoodenHorseAndSend, sendRaceStartMsg, setDicGuildActivity, setDicAuctionTime, settleGuildActivityReward, clearActivityObj, setGuildActivityIndexInPinus, guildActivityStart, debugSendGateHp, debugAddHorse, leaveRaceActivity, leaveRaceWhenQuitGuild, leaveRaceWhenDismiss } from '../../../services/guildActivity/guildActivityService';
import { gameData } from '../../../pubUtils/data';
import { setWeek } from '../../../pubUtils/timeUtil';
import { sendUngotDividend } from '../../../services/auctionService';
import { errlogger } from '../../../util/logger';
import { GUILD_ACTIVITY_TYPE } from '../../../consts';
import { saveGuildActivityIndexMemory } from '../../../services/log/memoryLogService';

export default function (app: Application) {
    new HandlerService(app, {});
    return new GuildActivityRemote(app);
}

export class GuildActivityRemote {

    constructor(private app: Application) {
        this.app = app;
    }

    private currentTime: number = Date.now();
    private setTime: number = Date.now();
    private isAuctionPopUp: boolean = false;

    /**
     * 从systimer服分发到guild各个服，发送排行榜数据
     */
    public async sendRankToGuilds(aid: number) {
        try {
            await sendAllGuildRanks(aid);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 发送结束活动消息
     */
    public async guildActivityEnd(aid: number) {
        try {
            await settleGuildActivityReward(aid);
            this.isAuctionPopUp = true;
            setTimeout(() => { // 军团活动结束的15分钟内就不会再弹出了
                this.isAuctionPopUp = false;
            }, 15 * 60 * 1000);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 计算粮草先行木马状态并发送
     */
    public async calWoodenHorseAndSend(serverId: number) {
        try {
            await calWoodenHorseAndSend(serverId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 诸侯混战和蛮夷入侵开始
     */
     async guildActivityStart(aid: number) {
        try {
            await guildActivityStart(aid);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 发送粮草先行活动开始通知
     */
    async raceActivityStart(serverId: number) {
        try {
            await sendRaceStartMsg(serverId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * !! 注意，这条函数会改变内存中的字典表，影响甚大，请考虑清楚后使用
     */
    async updateGuildActivityData(aid: number, isStart: boolean) {
        try {
            let guildActivity = gameData.guildActivity;
            let cur = guildActivity.get(aid);
    
            if (isStart) {
                let now = new Date();
                cur.startMinute = now.getMinutes();
                cur.startTime = now.getHours();
                cur.startSeconds = now.getSeconds();
            } else {
                let now = new Date(Date.now() - cur.duringTime * 1000);
                cur.startMinute = now.getMinutes();
                cur.startTime = now.getHours();
                cur.startSeconds = now.getSeconds();
            }
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

    /**
     * 改变字典表中的拍卖行时间
     */
     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}`);
        }
    }

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

    setCurrentTime(time: number) {
        try {
            this.currentTime = time;
            this.setTime = Date.now();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    getCurrentTime() {
        try {
            return Date.now() - this.setTime + this.currentTime;
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }
    
    public async sendUngotDividend() {
        try {
            await sendUngotDividend();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

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

    async setGuildActivityIndex(aid: number, index: number) {
        try {
            setGuildActivityIndexInPinus(aid, index);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async debugSendGateHp(serverId: number, cityId: number) {
        try {
            await debugSendGateHp(serverId, cityId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async debugAddHorse(serverId: number, guildCode: string, memberCnt: number, itemInterval: number) {
        try {
            await debugAddHorse(serverId, guildCode, memberCnt, itemInterval);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

    public leaveRaceActivity(serverId: number, guildCode: string, roleId: string) {
        try {
            return leaveRaceActivity(serverId, guildCode, roleId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public leaveRaceWhenQuitGuild(serverId: number, guildCode: string, roleId: string) {
        try {
            return leaveRaceWhenQuitGuild(serverId, guildCode, roleId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public leaveRaceWhenDismiss(guildCode: string, serverId: number) {
        try {
            return leaveRaceWhenDismiss(guildCode, serverId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }
}