using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using InControl;

public enum PriestSide
{
    Left,
    Right
}

public class GameManager : MonoBehaviour
{
    public bool GlobalGameEnd = false;

    AudioSource audio;
    float m_soundFXTimer = 0.0f;
    float m_soundFXMaxTimer = 2.0f;
    bool m_playSound = false;

    int m_numPlayer;
    public List<Player> m_listPlayer = new List<Player>();

    public int m_maxPeonOnBoard = 20;
    public List<Peon> m_listPeon = new List<Peon>();

    public bool m_firstTime = true;

    public AudioClip m_money1;
    public AudioClip m_money2;
    public AudioClip m_money3;
    public AudioClip m_money4;
    public AudioClip m_money5;
    public AudioClip m_money6;

    public Target m_targetP1;
    public Target m_targetP2;
    public Target m_targetP3;
    public Target m_targetP4;


    public Jauge m_jaugep1;
    public Jauge m_jaugep2;
    public Jauge m_jaugep3;
    public Jauge m_jaugep4;


    public AudioClip m_hit;

    public ScoreManager m_scoreManager;

    public GameObject m_victoryPanel;

    // Use this for initialization
    void Start()
    {
        audio = GetComponent<AudioSource>();

        m_soundFXTimer = m_soundFXMaxTimer;
        //Creer les joueurs en fonctione du nombre de personne
        m_numPlayer = 4;

        for (int i = 0; i < m_numPlayer; i++)
        {
            GameObject p = (GameObject)Instantiate(Resources.Load("Prefabs/Player"));
            Player player = p.GetComponent<Player>();
            player.SetManager(this);
            player.m_playerID = i;
            m_listPlayer.Add(player);
        }

        for (int i = 0; i < m_maxPeonOnBoard; i++)
        {
            GameObject p = (GameObject)Instantiate(Resources.Load("Prefabs/Peon"));
            Peon peon = p.GetComponent<Peon>();
            peon.m_manager = this;
            m_listPeon.Add(peon); 
        }

        GeneratePeonPosition();
        
        Debug.Log("End");
    }

    private void UpdatePlayer()
    {
        SpriteRenderer spriteRendererTemp;
        Sprite newSprite = Resources.Load<Sprite>("Sprite/Pretre_Bleu");

        spriteRendererTemp = m_listPlayer[0].m_priestLeft.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        spriteRendererTemp = m_listPlayer[0].m_priestRight.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        m_listPlayer[0].m_priestLeft.transform.position = new Vector2(-3, 2);
        m_listPlayer[0].m_priestRight.transform.position = new Vector2(-2, 2);
        //////////////////////////////////////////////
        newSprite = Resources.Load<Sprite>("Sprite/Pretre_Jaune");

        spriteRendererTemp = m_listPlayer[1].m_priestLeft.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        spriteRendererTemp = m_listPlayer[1].m_priestRight.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        m_listPlayer[1].m_priestLeft.transform.position = new Vector2(3, 2);
        m_listPlayer[1].m_priestRight.transform.position = new Vector2(2, 2);
        //////////////////////////////////////////////
        newSprite = Resources.Load<Sprite>("Sprite/Pretre_Vert");

        spriteRendererTemp = m_listPlayer[2].m_priestLeft.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        spriteRendererTemp = m_listPlayer[2].m_priestRight.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        m_listPlayer[2].m_priestLeft.transform.position = new Vector2(-3, -2);
        m_listPlayer[2].m_priestRight.transform.position = new Vector2(-2, -2);
        //////////////////////////////////////////////
        newSprite = Resources.Load<Sprite>("Sprite/Pretre_Rouge");

        spriteRendererTemp = m_listPlayer[3].m_priestLeft.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        spriteRendererTemp = m_listPlayer[3].m_priestRight.GetComponent<SpriteRenderer>();
        spriteRendererTemp.sprite = newSprite;

        m_listPlayer[3].m_priestLeft.transform.position = new Vector2(3, -2);
        m_listPlayer[3].m_priestRight.transform.position = new Vector2(2, -2);

    }


    private void GeneratePeonPosition()
    {
        List<Vector2> positionCheckList = new List<Vector2>();
        
        bool isPosValid = true;

        foreach (Peon p in m_listPeon)
        {
            //do
            //{
                float hRdmValue = Random.Range(-3.8f, 3.8f);
                float vRdmValue = Random.Range(-2.8f, 2.8f);

                Vector2 testedPosition = new Vector2(hRdmValue, vRdmValue);
            /*foreach (Vector2 vec in positionCheckList)
            {
                if (Vector3.Distance(testedPosition, vec) < 0.1f)
                {
                    isPosValid = false;
                }
            }*/

            //if(isPosValid)
            //{
            //positionCheckList.Add(testedPosition);
            p.transform.position = testedPosition;
            //}

            //}
            //while (!isPosValid);
        }

    }

    private void GeneratePeonPosition(Peon targetPeon)
    {
        bool isPosValid = true;

        List<Vector2> positionCheckList = new List<Vector2>();

        foreach(Peon peon in m_listPeon)
        {
            positionCheckList.Add(peon.transform.position);
        }

        //do
        //{
            float hRdmValue = Random.Range(-3.8f, 3.8f);
            float vRdmValue = Random.Range(-2.8f, 2.8f);

            Vector2 testedPosition = new Vector2(hRdmValue, vRdmValue);
        /*foreach (Vector2 vec in positionCheckList)
        {
            if (Vector3.Distance(testedPosition, vec) < 0.1f)
            {
                isPosValid = false;
            }
        }*/

        //if (isPosValid)
        //{
        targetPeon.transform.position = testedPosition;
        targetPeon.m_replace = false;
        //}

        //}
        //while (!isPosValid);
    }

    // Update is called once per frame
    void Update()
    {
        if(m_playSound)
        {
            m_soundFXTimer -= Time.deltaTime;

            if(m_soundFXTimer <= 0.0f)
            {
                m_playSound = false;
                m_soundFXTimer = m_soundFXMaxTimer;
            }
        }

        if(m_firstTime)
        {
            UpdatePlayer();
            m_firstTime = false;
        }

        foreach (Peon peon in m_listPeon)
        {
            if(peon.m_replace)
            {
                GeneratePeonPosition(peon);
            }
        }
    }

    public void Wololo(int idPlayer, PriestSide side)
    {
        Player player = m_listPlayer[idPlayer];
        Priest priest = player.GetPriest(side);

        Vector2 priestPosition = priest.transform.position;

        foreach(Peon peon in m_listPeon)
        {
            Vector2 peonPosition = peon.transform.position;

            float distance = Vector3.Distance(priestPosition, peonPosition);

            if (distance < 0.60f)
            {
                peon.Convert(player, priest);
            }
        }

        bool hit = false;
        foreach (Player playerToTest in m_listPlayer)
        {
            if(playerToTest.m_playerID != idPlayer)
            {
                Vector2 advPriestLPosition = playerToTest.m_priestLeft.transform.position;
                Vector2 advPriestRPosition = playerToTest.m_priestRight.transform.position;


                float distance1 = Vector3.Distance(priestPosition, advPriestLPosition);
                float distance2 = Vector3.Distance(priestPosition, advPriestRPosition);
                
                if (distance1 < 0.60f)
                {
                    hit = true;
                    playerToTest.m_priestLeft.m_isStun = true;
                }

                if (distance2 < 0.60f)
                {
                    hit = true;
                    playerToTest.m_priestRight.m_isStun = true;
                }

            }
        }

        if (hit)
            audio.PlayOneShot(m_hit, 1.0F);
    }

    public void IncrementScore(int playerID)
    {
        /*
        Earn_Money_07
        Earn_Money_08
        Earn_Money_09
        Earn_Money_10
        Earn_Money_11
        Earn_Money_12
        */

        if(!m_playSound)
        {
            m_playSound = true;

            int rdm = Random.Range(1, 6);

            switch(rdm)
            {
                case 1:
                    audio.PlayOneShot(m_money1, 0.7F);
                    break;
                case 2:
                    audio.PlayOneShot(m_money2, 0.7F);
                    break;
                case 3:
                    audio.PlayOneShot(m_money3, 0.7F);
                    break;
                case 4:
                    audio.PlayOneShot(m_money4, 0.7F);
                    break;
                case 5:
                    audio.PlayOneShot(m_money5, 0.7F);
                    break;
                case 6:
                    audio.PlayOneShot(m_money6, 0.7F);
                    break;
            }
            
        }

        m_listPlayer[playerID].ScoreInc();

        switch(playerID)
        {
            case 0:
                m_jaugep1.IncrementJauge();
                break;
            case 1:
                m_jaugep2.IncrementJauge();
                break;
            case 2:
                m_jaugep3.IncrementJauge();
                break;
            case 3:
                m_jaugep4.IncrementJauge();
                break;
        }
    }

    public int AskForPlayerScore(int playerID)
    {
        return m_listPlayer[playerID].m_score;
    }

    public void ShowVictoryPanel(int winnerID)
    {
        SpriteRenderer renderer = m_victoryPanel.GetComponent<SpriteRenderer>();
        Sprite newSprite;

        switch (winnerID)
        {
            case 0:
                newSprite = Resources.Load<Sprite>("Sprite/P1Vic");
                renderer.sprite = newSprite;
                break;
            case 1:
                newSprite = Resources.Load<Sprite>("Sprite/P2Vic");
                renderer.sprite = newSprite;
                break;
            case 2:
                newSprite = Resources.Load<Sprite>("Sprite/P3Vic");
                renderer.sprite = newSprite;
                break;
            case 3:
                newSprite = Resources.Load<Sprite>("Sprite/P4Vic");
                renderer.sprite = newSprite;
                break;
        }
    }
}
