/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.lobby.particle;

import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.special.easter.EggDifficulty;
import de.steamwar.lobby.special.easter.EggHunt;
import de.steamwar.sql.*;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public interface ParticleRequirement {

    String getRequirementName(Player player);
    boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig);

    ParticleRequirement SERVER_TEAM = new ParticleRequirement() {
        @Override
        public String getRequirementName(Player player) {
            return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_SERVER_TEAM", player);
        }

        @Override
        public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
            return user.hasPerm(UserPerm.TEAM);
        }
    };

    ParticleRequirement NO_REQUIRMENT = new ParticleRequirement() {
        @Override
        public String getRequirementName(Player player) {
            return null;
        }

        @Override
        public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
            return true;
        }
    };
    ParticleRequirement HAS_TEAM = new ParticleRequirement() {
        @Override
        public String getRequirementName(Player player) {
            return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_TEAM", player);
        }

        @Override
        public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
            return user.getTeam() != 0;
        }
    }.or(SERVER_TEAM);
    ParticleRequirement EVENT_PARTICIPATION = new ParticleRequirement() {
        @Override
        public String getRequirementName(Player player) {
            return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_EVENT", player);
        }

        @Override
        public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
            return !eventTeilname.isEmpty();
        }
    }.or(SERVER_TEAM);
    ParticleRequirement EGG_HUNT_EASY = _easterEggHuntDifficulty(EggDifficulty.EASY);
    ParticleRequirement EGG_HUNT_MEDIUM = _easterEggHuntDifficulty(EggDifficulty.MEDIUM);
    ParticleRequirement EGG_HUNT_HARD = _easterEggHuntDifficulty(EggDifficulty.HARD);
    ParticleRequirement EGG_HUNT_EXTREME = _easterEggHuntDifficulty(EggDifficulty.EXTREME);
    ParticleRequirement EGG_HUNT_ADVANCED = _easterEggHuntDifficulty(EggDifficulty.ADVANCED);
    ParticleRequirement EGG_HUNT_FOUND_HALF = new ParticleRequirement() {
        @Override
        public String getRequirementName(Player player) {
            return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_EASTER_EGG_HUNT_HALF", player);
        }

        @Override
        public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
            if (eggHuntConfig == null) return false;
            int count = 0;
            for (int i = 0; i < eggHuntConfig.length(); i++) {
                if (eggHuntConfig.charAt(i) == '1') {
                    count++;
                }
            }
            return count >= EggHunt.getEggList().size() / 2;
        }
    };

    static ParticleRequirement specificTeam(int teamId) {
        if (teamId == 0) return NO_REQUIRMENT;
        String teamKuerzel = Team.get(teamId).getTeamKuerzel();
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_SPECIFIC_TEAM", player, teamKuerzel);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return user.getTeam() == teamId;
            }
        }.or(SERVER_TEAM);
    }

    static ParticleRequirement eventParticipation(int eventId) {
        String eventName = Event.get(eventId).getEventName();
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_EVENT_PARTICIPATION", player, eventName);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return eventTeilname.contains(eventId);
            }
        }.or(SERVER_TEAM);
    }

    static ParticleRequirement eventPlacement(int eventId, int... placementTeams) {
        String eventName = Event.get(eventId).getEventName();
        Set<Integer> teams = new HashSet<>();
        for (int i : placementTeams) teams.add(i);
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_EVENT_PLACEMENT", player, eventName);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                if (!eventTeilname.contains(eventId)) return false;
                return teams.contains(user.getTeam());
            }
        }.or(SERVER_TEAM);
    }

    static ParticleRequirement _easterEggHuntDifficulty(EggDifficulty difficulty) {
        AtomicInteger count = new AtomicInteger();
        List<Integer> eggs = EggHunt.getEggList()
                .stream()
                .map(egg -> {
                    int id = count.getAndIncrement();
                    if (egg.getDifficulty() == difficulty) {
                        return id;
                    } else {
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_EASTER_EGG_HUNT_DIFFICULTY", player, LobbySystem.getMessage().parse(difficulty.getMessage(), player));
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                if (eggHuntConfig == null) return false;
                return eggs.stream().allMatch(id -> eggHuntConfig.length() > id && eggHuntConfig.charAt(id) == '1');
            }
        };
    }

    static ParticleRequirement specificPlayer(int userId) {
        String userName = SteamwarUser.get(userId).getUserName();
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_SPECIFIC_USER", player, userName);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return user.getId() == userId;
            }
        }.or(SERVER_TEAM);
    }

    static ParticleRequirement easterEventSpecificPlayer(int userId) {
        String userName = SteamwarUser.get(userId).getUserName();
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_SPECIFIC_USER_EASTER", player, userName);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return user.getId() == userId;
            }
        }.or(SERVER_TEAM);
    }

    static ParticleRequirement easterEventSpecificTeam(int teamId) {
        if (teamId == 0) return NO_REQUIRMENT;
        String teamKuerzel = Team.get(teamId).getTeamKuerzel();
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return LobbySystem.getMessage().parse("PARTICLE_UNLOCKED_BY_SPECIFIC_TEAM_EASTER", player, teamKuerzel);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return user.getTeam() == teamId;
            }
        }.or(SERVER_TEAM);
    }

    default ParticleRequirement or(ParticleRequirement particleRequirement) {
        return new ParticleRequirement() {
            @Override
            public String getRequirementName(Player player) {
                return ParticleRequirement.this.getRequirementName(player);
            }

            @Override
            public boolean test(SteamwarUser user, Set<Integer> eventTeilname, String eggHuntConfig) {
                return ParticleRequirement.this.test(user, eventTeilname, eggHuntConfig) || particleRequirement.test(user, eventTeilname, eggHuntConfig);
            }
        };
    }
}
