import { Application, ChannelService, HandlerService, } from 'pinus';
import { sendAllGuildRanks, sendGuildActEndMsg, calWoodenHorseAndSend, sendRaceStartMsg, setDicGuildActivity, setDicAuctionTime } from '../../../services/guildActivityService';
import { gameData } from '../../../pubUtils/data';
import { setWeek } from '../../../pubUtils/timeUtil';
import { sendUngotDividend } from '../../../services/auctionService';

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

export class GuildActivityRemote {

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

    private channelService: ChannelService;
    private currentTime: number = Date.now();
    private setTime: number = Date.now();

    /**
     * 从systimer服分发到guild各个服，发送排行榜数据
     */
    public async sendRankToGuilds(aid: number) {
        await sendAllGuildRanks(aid);
    }

    /**
     * 发送结束活动消息
     */
    public async guildActivityEnd(aid: number) {
        await sendGuildActEndMsg(aid);
    }

    /**
     * 计算粮草先行木马状态并发送
     */
    public async calWoodenHorseAndSend(serverId: number) {
        await calWoodenHorseAndSend(serverId);
    }

    /**
     * 发送粮草先行活动开始通知
     */
    async raceActivityStart(serverId: number) {
        await sendRaceStartMsg(serverId);
    }

    /**
     * !! 注意，这条函数会改变内存中的字典表，影响甚大，请考虑清楚后使用
     */
    async updateGuildActivityData(aid: number, isStart: boolean) {
        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();
        }
    }

    /**
     * 改变字典表中的军团活动开始时间&持续时间
     * @param {number} aid 活动 
     * @param {number} startSeconds 从现在开始几秒后开启活动
     * @param {number} endSeconds 从开始之后几秒后结束活动
     */
    async setDicGuildActivity(now: number, aid: number, startSeconds: number, endSeconds: number) {
        return setDicGuildActivity(now, aid, startSeconds, endSeconds);
    }

    /**
     * 改变字典表中的拍卖行时间
     */
     async setDicAuctionTime(startTime: number, endActivity: number, startGuild: number, endGuild: number, startWorld: number, endWorld: number) {
        setDicAuctionTime(startTime, endActivity, startGuild, endGuild, startWorld, endWorld);
    }

    /**
     * !! 注意，这条函数会改变内存时间，影响甚大，请考虑清楚后使用
     */
    async setDay(week: number) {
        setWeek(week);
    }

    setCurrentTime(time: number) {
        this.currentTime = time;
        this.setTime = Date.now();
    }

    getCurrentTime() {
        return Date.now() - this.setTime + this.currentTime;
    }
    
    public async sendUngotDividend() {
        await sendUngotDividend();
    }
}