package org.project;

import org.project.entity.enemies.Enemy;
import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton;
import org.project.entity.enemies.Vampire;
import org.project.entity.players.Assassin;
import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.entity.players.Wizard;
import org.project.item.weapons.Sword;
import org.project.item.weapons.Weapon;
import org.project.location.Location;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Weapon wp = new Sword();
        ArrayList<Player> players = new ArrayList<>();
        players.add(new Assassin());
        players.add(new Knight());
        players.add(new Wizard());
        ArrayList<Enemy> enemies = new ArrayList<>();
        enemies.add(new Goblin());
        enemies.add(new Skeleton());
        enemies.add(new Vampire());
        ArrayList<Location> locations = new ArrayList<>();
        locations.add(new Location("Jungle", enemies));
        locations.add(new Location("China", new ArrayList<>(enemies.subList(0, 2))));
        locations.add(new Location("Iran", new ArrayList<>(enemies.subList(1, 3))));
        Player player;
        Enemy enemy;
        Location location;
        Scanner sc = new Scanner(System.in);

        while (true) {
            player = choosePlayer(players,sc);
            while (true) {
                location = chooseLocation(locations,sc);
                enemy = chooseEnemy(location);
                System.out.println("wanna fight?\n1.Yes\n2.No");
                int choice = sc.nextInt();
                if (choice == 1) {
                    combat(player,enemy,sc);
                    System.out.println("player AHP: " + player.getHP());
                    break;
                }
                else {
                    continue;
                }
            }

        }



    }
/////////////////////////////////////////////////////////////////////////////////////////
    public static void displayLocations(List<Location> locations) {
        for (Location location : locations) {
            System.out.println(location);
        }
    }

    public static Player choosePlayer(ArrayList<Player> players, Scanner sc) {
        System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
        int choice = sc.nextInt();
        Player player;
        switch (choice) {
            case 1 -> player = players.get(0);
            case 2 -> player = players.get(1);
            default -> player = players.get(2);
        }
        System.out.println("You chose " + player.getName() + "(" + player.getClass().getSimpleName() + ")");
        return player;
    }

    public static Location chooseLocation(ArrayList<Location> locations, Scanner sc) {

        displayLocations(locations);
        System.out.print("1.Jungle\n2.China\n3.Iran\nChoose Location: ");
        int choice = sc.nextInt();
        Location location = null;

        switch (choice) {
            case 1 -> location = locations.get(0);
            case 2 -> location = locations.get(1);
            case 3 -> location = locations.get(2);
        }
        System.out.println("You chose " + location.getName() );
        return location;
    }

    public static Enemy chooseEnemy(Location location) {
        int random = new Random().nextInt(location.getEnemies().size());
        Enemy enemy = location.getEnemies().get(random);
        System.out.println("The Enemy is: " + enemy.getClass().getSimpleName());
        return enemy;
    }

    public static void combat(Player player, Enemy enemy, Scanner sc) {
        int turn = 0;
        int choice;
        while (player.isAlive() && enemy.isAlive()) {
            if (turn == 0) {
                turn = 1;
                System.out.println("choose your action:\n1.light attack 2.heavy attack 3.defend 4.heal 5.special ability");
                choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        player.lightAttack(enemy);
                        break;
                    case 2:
                        player.heavyAttack(enemy);
                        if (player.isSuccessfulAction()) break;
                        else {turn = 0; continue;}
                    case 3:
                        player.defend();
                        if (player.isSuccessfulAction()) break;
                        else {turn = 0; continue;}
                    case 4:
                        player.heal(10);
                        if (player.isSuccessfulAction()) break;
                        else {turn = 0; continue;}

                    case 5:
                        player.specialAbility(enemy);
                        if (player.isSuccessfulAction()) break;
                        else {turn = 0; continue;}
                    default:
                        System.out.println("invalid choice");
                }
                System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
                + " HP | " + player.getMP() + "/" + player.getMaxMP() + " Mana" + "]");
                System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
                        + " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");

            }
            if (turn == 1) {
                System.out.println("-".repeat(30));
                if (player instanceof Assassin) {
                    if (player.getUsingSpecialAbility()){
                     player.setUsingSpecialAbility(false);
                     turn = 0;
                        System.out.println(enemy.getClass().getSimpleName() + " skipped it's turn!");
                     continue;
                    }
                }
                System.out.println(enemy.getClass().getSimpleName() + "'s turn: ");
                turn = 0;
                choice = new Random().nextInt(3);
                switch (choice) {
                    case 0:
                        enemy.attack(player);
                        if (enemy.isSuccessfulAction()) break;
                        else {turn = 1; continue;}
                    case 1:
                        enemy.defend();
                        if (enemy.isSuccessfulAction()) break;
                        else {turn = 1; continue;}
                    case 2:
                        enemy.heal(10);
                        if (enemy.isSuccessfulAction()) break;
                        else {turn = 1; continue;}
                }
            }
            System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
                    + " HP | " + player.getMP() + "/" + player.getMaxMP() +" Mana" + "]");
            System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
                    + " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");

            }
        if (!player.isAlive()) {
            System.out.println("You lost!");
        }
        else {
            System.out.println("You Won!");
            if (enemy.dropKey()) {
                System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!");
                player.achieveKey(enemy);
            }
        }
        }
    }


