package com.ygames.ysoccer.server;

import com.esotericsoftware.kryonet.Server;
import com.ygames.ysoccer.events.BallBounceEvent;
import com.ygames.ysoccer.events.BallCollisionEvent;
import com.ygames.ysoccer.events.BallKickEvent;
import com.ygames.ysoccer.events.CelebrationEvent;
import com.ygames.ysoccer.events.CrowdChantsEvent;
import com.ygames.ysoccer.events.FoulEvent;
import com.ygames.ysoccer.events.HomeGoalEvent;
import com.ygames.ysoccer.events.KeeperDeflectEvent;
import com.ygames.ysoccer.events.KeeperHoldEvent;
import com.ygames.ysoccer.events.MatchIntroEvent;
import com.ygames.ysoccer.events.MatchNewStateEvent;
import com.ygames.ysoccer.events.PeriodStopEvent;
import com.ygames.ysoccer.events.WhistleEvent;
import com.ygames.ysoccer.framework.EventManager;
import com.ygames.ysoccer.network.dto.MatchStatsUpdateDto;
import com.ygames.ysoccer.network.dto.events.BallBounceEventDto;
import com.ygames.ysoccer.network.dto.events.BallCollisionEventDto;
import com.ygames.ysoccer.network.dto.events.BallKickEventDto;
import com.ygames.ysoccer.network.dto.events.CelebrationEventDto;
import com.ygames.ysoccer.network.dto.events.CrowdChantsEventDto;
import com.ygames.ysoccer.network.dto.events.FoulEventDto;
import com.ygames.ysoccer.network.dto.events.HomeGoalEventDto;
import com.ygames.ysoccer.network.dto.events.KeeperDeflectEventDto;
import com.ygames.ysoccer.network.dto.events.KeeperHoldEventDto;
import com.ygames.ysoccer.network.dto.events.MatchIntroEventDto;
import com.ygames.ysoccer.network.dto.events.PeriodStopEventDto;
import com.ygames.ysoccer.network.dto.events.WhistleEventDto;
import com.ygames.ysoccer.network.mappers.FoulMapper;
import com.ygames.ysoccer.network.mappers.GoalMapper;

public class NetworkManager {

    public static void subscribe(Server server) {
        EventManager.subscribe(BallBounceEvent.class, ballBounceEvent -> server.sendToAllTCP(new BallBounceEventDto(ballBounceEvent.speed)));
        EventManager.subscribe(BallCollisionEvent.class, ballCollisionEvent -> server.sendToAllTCP(new BallCollisionEventDto(ballCollisionEvent.strength)));
        EventManager.subscribe(BallKickEvent.class, ballKickEvent -> server.sendToAllTCP(new BallKickEventDto(ballKickEvent.strength)));
        EventManager.subscribe(CelebrationEvent.class, celebrationEvent -> server.sendToAllTCP(new CelebrationEventDto()));
        EventManager.subscribe(CrowdChantsEvent.class, crowdChantsEvent -> server.sendToAllTCP(new CrowdChantsEventDto()));
        EventManager.subscribe(HomeGoalEvent.class, homeGoalEvent -> server.sendToAllTCP(new HomeGoalEventDto(GoalMapper.toDto(homeGoalEvent.goal))));
        EventManager.subscribe(KeeperDeflectEvent.class, keeperDeflectEvent -> server.sendToAllTCP(new KeeperDeflectEventDto()));
        EventManager.subscribe(KeeperHoldEvent.class, keeperHoldEvent -> server.sendToAllTCP(new KeeperHoldEventDto()));
        EventManager.subscribe(MatchIntroEvent.class, matchIntroEvent -> server.sendToAllTCP(new MatchIntroEventDto()));
        EventManager.subscribe(PeriodStopEvent.class, periodStopEvent -> server.sendToAllTCP(new PeriodStopEventDto()));
        EventManager.subscribe(WhistleEvent.class, whistleEvent -> server.sendToAllTCP(new WhistleEventDto()));
        EventManager.subscribe(FoulEvent.class, foulEvent -> server.sendToAllTCP(new FoulEventDto(FoulMapper.toDto(foulEvent.foul))));
        EventManager.subscribe(MatchNewStateEvent.class, event -> server.sendToAllTCP(new MatchStatsUpdateDto(event.match.stats)));
    }
}
