using UnityEngine;
using System.Collections;
using InControl;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    public int m_playerID;
    public InputDevice m_device;

    public Priest m_priestRight;
    public Priest m_priestLeft;

    public float m_speed;

    public GameManager m_manager;


    public int m_score = 0;

    public Jauge m_playerJauge;

    // Use this for initialization
    void Start()
    {
        GameObject tempRight = transform.Find("Priest_R").gameObject;
        GameObject tempLeft = transform.Find("Priest_L").gameObject;
        
        m_priestRight = tempRight.GetComponent<Priest>();
        m_priestLeft = tempLeft.GetComponent<Priest>();

        m_priestRight.m_masterID = m_playerID;
        m_priestLeft.m_masterID = m_playerID;

        switch(m_playerID)
        {
            case 0:
                m_playerJauge.transform.position = new Vector2(-6.0f,0.0f);
                break;
            case 1:
                m_playerJauge.transform.position = new Vector2(-5.0f, 0.0f);
                break;
            case 2:
                m_playerJauge.transform.position = new Vector2(5.0f, 0.0f);
                break;
            case 3:
                m_playerJauge.transform.position = new Vector2(6.0f, 0.0f);
                break;
        }

        Debug.Log("Player Creer");
    }

    // Update is called once per frame
    void Update()
    {
        if (!m_manager.GlobalGameEnd)
        {
            if (m_playerID == 0)
            {
                #region player_1
                //Deplacement
                #region deplacement
                float firstPriestH = Input.GetAxis("J1_Ax1_H");
                float firstPriestV = Input.GetAxis("J1_Ax1_V");

                float secondPriestH = Input.GetAxis("J1_Ax2_H");
                float secondPriestV = Input.GetAxis("J1_Ax2_V");

                if (!m_priestLeft.m_isStun)
                {
                    Vector3 rPriestMove = Vector3.right * firstPriestH * Time.deltaTime * m_speed + Vector3.up * -firstPriestV * Time.deltaTime * m_speed;
                    m_priestLeft.transform.Translate(rPriestMove);
                }

                if (!m_priestRight.m_isStun)
                {
                    Vector3 lPriestMove = Vector3.right * secondPriestH * Time.deltaTime * m_speed + Vector3.up * -secondPriestV * Time.deltaTime * m_speed;
                    m_priestRight.transform.Translate(lPriestMove);
                }
                #endregion

                //Action
                float actionLeftPriest = Input.GetAxis("J1_LTrigger");
                float actionRightPriest = Input.GetAxis("J1_RTrigger");

                if (actionLeftPriest != 0 && !m_priestLeft.m_isStun)
                {
                    if (!m_priestLeft.m_isWololoing)
                    {
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Left);

                        m_priestLeft.Wololo();
                    }

                }

                if (actionRightPriest != 0 && !m_priestRight.m_isStun)
                {
                    if (!m_priestRight.m_isWololoing)
                    {
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Right);

                        m_priestRight.Wololo();
                    }
                }
                #endregion
            }

            if (m_playerID == 1)
            {
                #region player_2
                //Deplacement
                #region deplacement
                float firstPriestH_2 = Input.GetAxis("J2_Ax1_H");
                float firstPriestV_2 = Input.GetAxis("J2_Ax1_V");

                float secondPriestH_2 = Input.GetAxis("J2_Ax2_H");
                float secondPriestV_2 = Input.GetAxis("J2_Ax2_V");

                if (!m_priestRight.m_isStun)
                {
                    Vector3 rPriestMove_2 = Vector3.right * firstPriestH_2 * Time.deltaTime * m_speed + Vector3.up * -firstPriestV_2 * Time.deltaTime * m_speed;
                    m_priestRight.transform.Translate(rPriestMove_2);
                }

                if (!m_priestLeft.m_isStun)
                {
                    Vector3 lPriestMove_2 = Vector3.right * secondPriestH_2 * Time.deltaTime * m_speed + Vector3.up * -secondPriestV_2 * Time.deltaTime * m_speed;
                    m_priestLeft.transform.Translate(lPriestMove_2);
                }
                #endregion

                //Action
                float actionLeftPriest_2 = Input.GetAxis("J2_LTrigger");
                float actionRightPriest_2 = Input.GetAxis("J2_RTrigger");

                if (actionRightPriest_2 != 0 && !m_priestLeft.m_isStun)
                {
                    if (!m_priestLeft.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Left);

                        m_priestLeft.Wololo();
                    }

                }

                if (actionLeftPriest_2 != 0 && !m_priestRight.m_isStun)
                {
                    if (!m_priestRight.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Right);

                        m_priestRight.Wololo();
                    }
                }
                #endregion
            }


            if (m_playerID == 2)
            {
                #region player_3
                //Deplacement
                #region deplacement
                float firstPriestH_3 = Input.GetAxis("J3_Ax1_H");
                float firstPriestV_3 = Input.GetAxis("J3_Ax1_V");

                float secondPriestH_3 = Input.GetAxis("J3_Ax2_H");
                float secondPriestV_3 = Input.GetAxis("J3_Ax2_V");

                if (!m_priestRight.m_isStun)
                {
                    Vector3 rPriestMove_3 = Vector3.right * firstPriestH_3 * Time.deltaTime * m_speed + Vector3.up * -firstPriestV_3 * Time.deltaTime * m_speed;
                    m_priestRight.transform.Translate(rPriestMove_3);
                }

                if (!m_priestLeft.m_isStun)
                {
                    Vector3 lPriestMove_3 = Vector3.right * secondPriestH_3 * Time.deltaTime * m_speed + Vector3.up * -secondPriestV_3 * Time.deltaTime * m_speed;
                    m_priestLeft.transform.Translate(lPriestMove_3);
                }
                #endregion

                //Action
                float actionLeftPriest_3 = Input.GetAxis("J3_LTrigger");
                float actionRightPriest_3 = Input.GetAxis("J3_RTrigger");

                if (actionLeftPriest_3 != 0 && !m_priestLeft.m_isStun)
                {
                    if (!m_priestLeft.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Left);

                        m_priestLeft.Wololo();
                    }

                }

                if (actionRightPriest_3 != 0 && !m_priestRight.m_isStun)
                {
                    if (!m_priestRight.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Right);

                        m_priestRight.Wololo();
                    }
                }
                #endregion
            }


            if (m_playerID == 3)
            {
                #region player_4
                //Deplacement
                #region deplacement
                float firstPriestH_4 = Input.GetAxis("J4_Ax1_H");
                float firstPriestV_4 = Input.GetAxis("J4_Ax1_V");

                float secondPriestH_4 = Input.GetAxis("J4_Ax2_H");
                float secondPriestV_4 = Input.GetAxis("J4_Ax2_V");

                if (!m_priestRight.m_isStun)
                {
                    Vector3 rPriestMove_4 = Vector3.right * firstPriestH_4 * Time.deltaTime * m_speed + Vector3.up * -firstPriestV_4 * Time.deltaTime * m_speed;
                    m_priestRight.transform.Translate(rPriestMove_4);
                }

                if (!m_priestLeft.m_isStun)
                {
                    Vector3 lPriestMove_4 = Vector3.right * secondPriestH_4 * Time.deltaTime * m_speed + Vector3.up * -secondPriestV_4 * Time.deltaTime * m_speed;
                    m_priestLeft.transform.Translate(lPriestMove_4);
                }
                #endregion

                //Action
                float actionLeftPriest_4 = Input.GetAxis("J4_LTrigger");
                float actionRightPriest_4 = Input.GetAxis("J4_RTrigger");

                if (actionLeftPriest_4 != 0 && !m_priestLeft.m_isStun)
                {
                    if (!m_priestLeft.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Left);

                        m_priestLeft.Wololo();
                    }

                }

                if (actionRightPriest_4 != 0 && !m_priestRight.m_isStun)
                {
                    if (!m_priestRight.m_isWololoing)
                    {
                        Debug.Log("Wololo");
                        AudioSource _audio = this.GetComponent<AudioSource>();
                        _audio.Play();

                        m_manager.Wololo(m_playerID, PriestSide.Right);

                        m_priestRight.Wololo();
                    }
                }
                #endregion
            }
        }
    }

    public Priest GetPriest(PriestSide side)
    {
        if (side == PriestSide.Left)
            return m_priestLeft;
        else
            return m_priestRight;
    }

    public void SetManager(GameManager manager)
    {
        m_manager = manager;
    }

    public void ScoreInc()
    {
        m_score++;
    }
}
