package Server;

import Server.Cards.Event;
import Server.Cards.EventCard;
import Server.Cards.Trigger;
import Server.Utils.EventsManagerException;

import java.util.ArrayList;
import java.util.List;

public class EventsSolver {

    public static boolean solveEvents(List<EventCard> events, List<Player> players){

        for(EventCard event: events){
            switch (event.getEvent()){
                case Event.SUSTAINMENT:
                    EventsSolver.sustainment(event, players);
                    break;
                case Event.HUNT:
                    EventsSolver.hunt(event, players);
                    break;
                case Event.SHAMANIC_RITUAL:
                    EventsSolver.shamanicRitual(event, players);
                    break;
                case Event.CAVE_PAINTINGS:
                    EventsSolver.cavePaintings(event, players);
                    break;
                default:
                    throw new EventsManagerException("Unknown event type");
            }

        }
        return true;
    }

    public static List<Integer> sustainment(EventCard event, List<Player> players){

        if(event.getEvent() != Event.SUSTAINMENT){throw new EventsManagerException("Not a sustainment card");}

        List<Integer> result = new ArrayList<>();
        for(Player p: players){
            int subpoints = p.getPlayerTribe().getCharacters().size();
            int discount = p.getPlayerTribe().gathererDiscount() + BuildingManager.sustainDiscount(p);

            if(subpoints <= discount){subpoints = 0;}else{
                subpoints -= discount;
            }


            if(p.getFoodTokens() >= subpoints){
                p.removeFood(subpoints);
            }else{
                subpoints -= p.getFoodTokens();
                p.removeFood(p.getFoodTokens());
                p.removePrestigePoints(subpoints*event.getFirstValue());
            }

            result.add(p.getFoodTokens());
            result.add(p.getPrestigePoints());

        }

        return result;
    }

    public static List<Integer> hunt(EventCard event, List<Player> players){
        if(event.getEvent() != Event.HUNT){throw new EventsManagerException("Not a hunt card");}

        List<Integer> result = new ArrayList<>();

        for(Player p: players){
            p.addFood(p.getPlayerTribe().huntersNumber());
            p.addPrestigePoints(p.getPlayerTribe().huntersNumber()*event.getFirstValue());

            if(BuildingManager.hunterBonus(p)){
                p.addFood(p.getPlayerTribe().huntersNumber());
                p.addPrestigePoints(p.getPlayerTribe().huntersNumber());
            }

            result.add(p.getFoodTokens());
            result.add(p.getPrestigePoints());
        }

        return result;
    }

    public static List<Integer> shamanicRitual(EventCard event, List<Player> players){
        if(event.getEvent() != Event.SHAMANIC_RITUAL){throw new EventsManagerException("Not a shamanic ritual card");}
        List<Integer> result = new ArrayList<>();
        int maxSymbols = 0;
        int minSymbols = 999;

        for(Player p: players){
            int symbols = p.getPlayerTribe().shamansIcons() + BuildingManager.shamanBonus(p);
            if (symbols > maxSymbols){maxSymbols = p.getPlayerTribe().shamansIcons();}
            if (symbols < minSymbols){minSymbols = p.getPlayerTribe().shamansIcons();}
        }

        for(Player p: players){
            if(p.getPlayerTribe().shamansIcons() == maxSymbols){
                p.addPrestigePoints(event.getFirstValue());

                //activating building shamanDoublePoints card effect
                if(BuildingManager.shamanDoublePoints(p))
                    p.addPrestigePoints(event.getFirstValue());
            }
            if(p.getPlayerTribe().shamansIcons() == minSymbols){

                //activating building shamanNoLoss card effect
                if(!BuildingManager.shamanNoLoss(p))
                    p.removePrestigePoints(event.getSecondValue());
            }
            result.add(p.getPrestigePoints());
        }

        return result;
    }

    public static List<Integer> cavePaintings(EventCard event, List<Player> players){

        if(event.getEvent() != Event.CAVE_PAINTINGS){throw new EventsManagerException("Not a cave painting card");}
        List<Integer> result = new ArrayList<>();
        for(Player p: players){
            BuildingManager.paintingFoodBonus(p);
            if(p.getPlayerTribe().artistsNumber() >= event.getFirstValue()){
                p.addPrestigePoints(p.getPlayerTribe().artistsNumber()*event.getSecondValue());
            }else{
                p.removePrestigePoints(event.getSecondValue());
            }

            result.add(p.getPrestigePoints());
        }
        return result;
    }
}
