using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GGJ16;

namespace GGJ16
{
    public class ActionController : MonoBehaviour
    {

        Client client;
        public GameObject ChoosePlayerPanelAssasin;
        public GameObject ChoosePlayerPanelPreacher;

        int chosenPlayerID = 1;

        void Start()
        {
            client = Client.instance;
        }

        public void OnBuyWorshiopersClicked()
        {
            client.playerService.GetMoreFollowers(client.turnService.currentPlayerTurn);

            //Next turn (after all animations)
            client.turnService.NextTurn();
        }

        public void OnBuyPriestClicked()
        {
            bool notFinished = true;
            //Buy Priest
            while (notFinished)
            {
                try
                {
                    client.playerService.RecruitPriest(client.turnService.currentPlayerTurn);
                    notFinished = false;
               } 
                catch (InsufficientFundsException e)
                {
                    System.Console.WriteLine(e.StackTrace);
                    Instantiate(Resources.Load("InsufficientFunds"), transform.position, Quaternion.identity);
                }

            }

            //Next turn (after all animations)
            client.turnService.NextTurn();


        }

        public void OnBuyAssasinClicked()
        {
            Debug.Log("Assassin Clicked");
            //Choose Player
            ChoosePlayerPanelAssasin.SetActive(true);
            this.gameObject.GetComponent<CanvasGroup>().interactable = false;

        }

        public void DoAssassin(int enemyID)
        {
            Debug.Log("Enemy Chosen -" + enemyID);
            Debug.Log("My ID Chosen -" + client.turnService.currentPlayerTurn);

            //Check user didn't choose himself
            if (enemyID != client.turnService.currentPlayerTurn)
            {
                bool notFinished = true;
                while (notFinished)
                {
                    try
                    {
                        client.playerService.SendAssassin(enemyID, client.turnService.currentPlayerTurn, 5);
                        notFinished = false;
                    }
                    catch(InsufficientFundsException e)
                    {
                        System.Console.WriteLine(e.StackTrace);
                        Instantiate(Resources.Load("InsufficientFunds"),transform.position,Quaternion.identity);
                    }
                    
                }
                //Next turn (after all animations)
                client.turnService.NextTurn();

                ChoosePlayerPanelAssasin.SetActive(false);
                this.gameObject.GetComponent<CanvasGroup>().interactable = true;
            }
        }

        public void OnBuyPreacherClicked()
        {
            //ChoosePlayer
            ChoosePlayerPanelPreacher.SetActive(true);
            this.gameObject.GetComponent<CanvasGroup>().interactable = false;
        }

        public void DoPreacher(int enemyID)
        {

            //Check user didn't choose himself
            if (enemyID != client.turnService.currentPlayerTurn)
            {
                bool notFinished = true;
                while (notFinished)
                {
                    try
                    {
                        client.playerService.SendPreacher(client.turnService.currentPlayerTurn, enemyID);
                        notFinished = false;
                    }
                    catch (InsufficientFundsException e)
                    {
                        System.Console.WriteLine(e.StackTrace);
                        Instantiate(Resources.Load("InsufficientFunds"), transform.position, Quaternion.identity);
                    }

                }
                
                //Next turn (after all animations)
                client.turnService.NextTurn();

                ChoosePlayerPanelPreacher.SetActive(false);
                this.gameObject.GetComponent<CanvasGroup>().interactable = true;
            }

        }

        public void OnBackButtonClicked()
        {
            Application.Quit();
        }

    }
}
