import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
/**
* Write a description of class MainGame here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class MainGame
{
    // instance variables - replace the example below with your own
    private Scanner input;
    private Random rand;
    private Character player, computer;
    private Path chosenPath, computerPath;
    private int spaces, computerSpaces, numTurns = 0;
    private int atkBonus, hpBonus, computerAtk, computerHp;
    private ArrayList<Integer> cardDeck, computerDeck;
    private SpecialCard specialCardDeck;
    private boolean playerTurn = true;
    private boolean lostTurn = false;
    private boolean compLostTurn = false;
    /**
     * Constructor for objects of class MainGame
     */
    public MainGame()
    {
        input = new Scanner(System.in);
        rand = new Random();
        showMenu();
        
    }
    
    /**
     * Display the instructions
     */
    public void showInstructions()
    {
        System.out.println("To begin playing, first choose a character.");
        System.out.println("Each character has different stats, so choose wisely");
        System.out.println("Once you've chosen a character, choose a path.");
        System.out.println("Each path contains a different amount of upgrades for your character.");
        System.out.println("These upgrades include attack, health and special abilities.");
        System.out.println("Which path you choose is up to you.\n");
        System.out.println("when the game starts, you will roll a dice.");
        System.out.println("The dice will show how many spaces you move.");
        System.out.println("If you land on an attack or health space, you will receive a bonus");
        System.out.println("If you land on a special ability space, a special ability card will \nbe added to your deck.");
        System.out.println("This will continue until you reach the arena. At that time, the \nrules for the arena will be displayed.");
        input.nextLine();
        showMenu();
    }
    
    /**
     * Show the menu
     */
    public void showMenu()
    {
        int choice = 0;
        
        System.out.println("1. New Game");
        System.out.println("2. Continue");
        System.out.println("3. Instructions");
        System.out.println("4. Quit\n");
        if (input.hasNextInt())
            choice = input.nextInt();
        input.nextLine();
        
        while (choice < 1 || choice > 4)
        {
            System.out.println("This choice is not valid. Please choose 1 through 4.\n");
            if (input.hasNextInt())
                choice = input.nextInt();
            input.nextLine();
        }
        
        if (choice == 1)
        {
            startGame();
        }
        else if (choice == 2)
        {
            continueGame();
        }
        else if (choice == 3)
        {
            showInstructions();
        }
        else if (choice == 4)
        {
            quit();
        }
    }
    
    public void startGame()
    {
        /****************
         * player setup *
         ****************/
        int choice = 0;
        System.out.println("Welcome! Before starting the game, please select your character:\n");
        System.out.println("1. Warrior:   20 Health  8-Sided Die");
        System.out.println("2. Soldier:   35 Health  6-Sided Die");
        System.out.println("3. Defender:  50 Health  4-Sided Die");
        System.out.println("4. Return to menu\n");
        if (input.hasNextInt())
                choice = input.nextInt();
        input.nextLine();
       
        while (choice < 1 || choice > 4)
        {
            System.out.println("This choice is not valid. Please choose 1 through 4.\n");
            if (input.hasNextInt())
                choice = input.nextInt();
            input.nextLine();
        }
        
        if (choice == 1)
        {
            player = new Character(20, 8, "Warrior");
        }
        else if (choice == 2)
        {
            player = new Character(35, 6, "Soldier");
        }
        else if (choice == 3)
        {
            player = new Character(50, 4, "Defender");
        }
        else if (choice == 4)
        {
            quit();
        }
        
        System.out.println("Now you must choose which path you will take:\n");;
        System.out.println("1. Balanced Path: Upgrades are even");
        System.out.println("2. Attack Path:   Mostly attack upgrades");
        System.out.println("3. Defense Path:  Mostly health upgrades");
        System.out.println("4. Special Path:  Less upgrades, more special abilities\n");
        if (input.hasNextInt())
                choice = input.nextInt();
        input.nextLine();
       
        while (choice < 1 || choice > 4)
        {
            System.out.println("This choice is not valid. Please choose 1 through 4.\n");
            if (input.hasNextInt())
                choice = input.nextInt();
            input.nextLine();
        }
        
        if (choice == 1)
        {
            chosenPath = new Path(.40, .40, .15, "Balance");
        }
        else if (choice == 2)
        {
            chosenPath = new Path(.60, .20, .15, "Attack");
        }
        else if (choice == 3)
        {
            chosenPath = new Path(.20, .60, .15, "Defense");
        }
        else if (choice == 4)
        {
            chosenPath = new Path(.35, .35, .24, "Special"); 
        }
        /******************
         * computer setup *
         ******************/
        int decision = rand.nextInt(3) + 1;
        if (decision == 1)
        {
            computer = new Character(20, 8, "Warrior");
        }
        else if (decision == 2)
        {
            computer = new Character(35, 6, "Soldier");
        }
        else if (decision == 3)
        {
            computer = new Character(50, 4, "Defender");
        }
        
        int decision2 = rand.nextInt(4) + 1;
        if (decision2 == 1)
        {
            computerPath = new Path(.40, .40, .15, "Balance");
        }
        else if (decision2 == 2)
        {
            computerPath = new Path(.60, .20, .15, "Attack");
        }
        else if (decision2 == 3)
        {
            computerPath = new Path(.20, .60, .15, "Defense");
        }
        else if (decision2 == 4)
        {
            computerPath = new Path(.35, .35, .24, "Special"); 
        }
        
        System.out.println("The computer chooses to be a " + computer.getCharType());
        System.out.println("and will travel the " + computerPath.getPathName() + " path\n");
        /******************
         * Start the game *
         ******************/
        spaces = chosenPath.getNumSpaces();
        computerSpaces = computerPath.getNumSpaces();
        System.out.println("Let the game begin!\n");
        input.nextLine();
        while (spaces > 0 || computerSpaces > 0)
            playGame(player, chosenPath, computer, computerPath);
    }
    
    public void continueGame()
    {
        System.out.println("Continuing game\n");
        input.nextLine();
        showMenu();
    }
    
    public void quit()
    {
        System.out.println("Ending game\n");
        input.nextLine();
        System.exit(0);
    }
    
    public void playGame(Character charChosen, Path pathChosen, Character computerChar, Path computerPath)
    {
        cardDeck = new ArrayList<Integer>();
        computerDeck = new ArrayList<Integer>();
        specialCardDeck = new SpecialCard();
        
        
        if (playerTurn == true)
        {
            if (spaces > 0)
            {
                if (lostTurn == false)
                {
                    playerTurn(player, chosenPath);
                    playerTurn = false;
                    System.out.println("************************\n");
                }
                else
                {
                    System.out.println("You lose this turn\n");
                    System.out.println("************************\n");
                    playerTurn = false;
                    lostTurn = false;
                }
            }
            else
            {
                System.out.println("You have reached the arena\n");
                System.out.println("************************\n");
                playerTurn = false;
            }
                
        }
        else
        {
            if (computerSpaces > 0)
            {
                if (compLostTurn == false)
                {
                    computerTurn(computer, computerPath);
                    playerTurn = true;
                    numTurns += 1;
                    System.out.println("************************\n");
                }
                else
                {
                    System.out.println("Computer loses this turn\n");
                    System.out.println("************************\n");
                    playerTurn = true;
                    compLostTurn = false;
                    numTurns += 1;
                }
            }
            else
            {
                System.out.println("The computer has reached the arena\n");
                System.out.println("************************\n");
                playerTurn = true;
                numTurns += 1;
            }
        }
        
    }
    
    
    private int rollMoveDice()
    {
        int roll = rand.nextInt(4) + 1;
        return roll;
    }
    
    /***************
     * player turn *
     ***************/
    public void playerTurn(Character charChosen, Path pathChosen)
    {
        String curSpace;
        System.out.println("Press enter to roll the die.");
        input.nextLine();
        int movement = rollMoveDice();
        int bonus = 0;
        System.out.println("You rolled a " + movement);
        input.nextLine();
        if (spaces - movement > 0)
        {
            spaces -= movement;
            curSpace = pathChosen.spaceValue();
            System.out.println("You landed on a " + curSpace + " space.");
            
            if (curSpace == "Attack")
            {
                bonus = rand.nextInt(3) + 1;
                atkBonus += bonus ;
                System.out.println("You gain " + bonus + " attack.\n");
            }
            else if (curSpace == "Defense")
            {
                bonus = rand.nextInt(3) + 4;
                hpBonus += bonus;
                System.out.println("You gain " + bonus + " health.\n");
            }
            else if (curSpace == "Special Ability")
            {
                drawSpecialCard(specialCardDeck, "You draw", cardDeck);
            }
            else if (curSpace == "Lose Turn")
            {
                lostTurn = true;
                System.out.println("You lose your next turn.\n");
            }
        }
        else
        {
            spaces = 0;
            System.out.println("You have reached the arena!\n");
            System.out.println("Total Attack bonus: " + atkBonus);
            System.out.println("Total Health bonus: " + (player.getInitHealth() + hpBonus));
            System.out.println("Special Cards in deck: ");
            for (int card = 0;card < cardDeck.size();card++)
            {
                System.out.println(cardDeck.get(card));
            }
        }
    }
    /*****************
     * Computer turn *
     *****************/
    public void computerTurn(Character charChosen, Path pathChosen)
    {
        String curSpace;
        System.out.println("Computer rolls the die.\n");
        int movement = rollMoveDice();
        int bonus = 0;
        System.out.println("Computer rolled a " + movement);
        if (computerSpaces - movement > 0)
        {
            computerSpaces -= movement;
            curSpace = pathChosen.spaceValue();
            System.out.println("The computer landed on a " + curSpace + " space.");
        
             if (curSpace == "Attack")
            {
                bonus = rand.nextInt(3) + 1;
                computerAtk += bonus ;
                System.out.println("Computer gains " + bonus + " attack.\n");
            }
            else if (curSpace == "Defense")
            {
                bonus = rand.nextInt(3) + 4;
                computerHp += bonus;
                System.out.println("Computer gains " + bonus + " health.\n");
            }
            else if (curSpace == "Special Ability")
            {
                drawSpecialCard(specialCardDeck, "The computer draws", computerDeck);
            }
            else if (curSpace == "Lose Turn")
            {
                compLostTurn = true;
                System.out.println("Computer loses next turn.\n");
            }
        }
        else
        {
            computerSpaces = 0;
            System.out.println("Computer has reached the arena!\n");
            System.out.println("Total Attack bonus: " + computerAtk);
            System.out.println("Total Health bonus: " + (computer.getInitHealth() + computerHp));
            System.out.println("Special Cards in deck: ");
            for (int card = 0;card < computerDeck.size();card++)
            {
                System.out.println(computerDeck.get(card));
            }
            
        }
    }
    
   
    
    public void drawSpecialCard(SpecialCard deck, String player, ArrayList<Integer> playerDeck)
    {
            deck.chooseCard();
            playerDeck.add(deck.getCard());
            System.out.println(player + " a special ability card:");
            System.out.println(deck.getCardDesc());
            System.out.println("Type: " + deck.getCardType() + "\n");
        
    } 
    
    public void resetSpaces()
    {
        spaces = 10;
        computerSpaces = 10;
    }
}