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

public class Player : MonoBehaviour
{
    public int mCount;
    public AudioManager mAudioManager;
    public Bar mBarScore;
    public GameManager mGameManager;
    bool mKeyPressed, mError = false;
    char pressed;
    public Score mScore;
    public Audience[] mAudience;
    public GameObject[] mLights;
    public Transform[] mBand;
    private AnimationMusician[] mMusicians;
    public Transform mPointOfTrade;
    private Vector3[] position;
    int trade = 0, mNumberOfChange = 3;

    public event System.Action resetEvent;
    public event System.Action hitEvent;
    Coroutine comparasion;

    public delegate void LightsOffDelegate();
    public LightsOffDelegate lightsOffEvent;

    void Start()
    {
        mGameManager.Event_TimerIsZero += mBarScore.Restart;
        mGameManager.Event_TimerIsZero += mScore.ResetMultiplication;
        mGameManager.Event_TimerIsZero += TocarErro;
        mMusicians = new AnimationMusician[4];
        position = new Vector3[4];
        for (int i = 0; i < 4; i++)
        {
            position[i] = mBand[i].position;
            mMusicians[i] = mBand[i].GetComponentInChildren<AnimationMusician>();
        }
        mGameManager.InputEnabled = true;
        mGameManager.NewSequence();
    }

    void Update()
    {
        ReceivingKeywords();
    }

    public void ReceivingKeywords()
    {
        if (mKeyPressed = Input.GetKeyDown(KeyCode.Q))
        {
            pressed = 'Q';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.W))
        {
            pressed = 'W';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.E))
        {
            pressed = 'E';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.R))
        {
            pressed = 'R';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.A))
        {
            pressed = 'A';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.S))
        {
            pressed = 'S';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.D))
        {
            pressed = 'D';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.F))
        {
            pressed = 'F';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.P))
        {
            pressed = 'P';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.O))
        {
            pressed = 'O';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.I))
        {
            pressed = 'I';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.U))
        {
            pressed = 'U';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.L))
        {
            pressed = 'L';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.K))
        {
            pressed = 'K';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.J))
        {
            pressed = 'J';
        }
        else if (mKeyPressed = Input.GetKeyDown(KeyCode.H))
        {
            pressed = 'H';
        }
    }

    public void ReceivingSequence(char[] sequence)
    {
        if (comparasion != null)
        {
            StopCoroutine(comparasion);
        }
        comparasion = StartCoroutine(Comparaison(sequence));
    }

    public void ChangePosition()
    {
        StartCoroutine(Change());
    }

    IEnumerator Comparaison(char[] sequence)
    {
        mCount = 0;
        while (mCount < sequence.Length && !mError)
        {
            int active = (int)(mCount / 3);
            if (!mLights[mGameManager.mSetOrder[active]].activeSelf)
            {
                mLights[mGameManager.mSetOrder[active]].SetActive(true);
            }
            if (mKeyPressed)
            {
                if (pressed == sequence[mCount])
                {
                    if (hitEvent != null)
                    {
                        hitEvent();
                    }
                    mAudioManager.PlaySFX(mBand[mGameManager.mSetOrder[active]].name);
                    mAudience[mCount].Change();
                    mCount++;
                    if (mScore.mMultiplication != 8)
                    {
                        mBarScore.TakeHit();
                    }
                    mScore.CalculatingScore(mCount);
                    mMusicians[mGameManager.mSetOrder[active]].Fried();
                }
                else
                {
                    mError = true;
                    mBarScore.Restart();
                    mScore.ResetMultiplication();
                    TocarErro();
                }
            }
            yield return null;
        }
        if (mCount == 12)
        {
            trade++;
            if (mScore.mMultiplication != 8)
            {
                mBarScore.Restart();
            }
            if(Random.Range(0f, 1f) < 0.25f)
            {
                //mGameManager.ShuffleKeyOrder();
            }
        }
        mError = false;
        if (trade != mNumberOfChange)
        {
            mGameManager.ResetTimer();
            mGameManager.NewSequence();
        }
        if (trade == mNumberOfChange)
        {
            mGameManager.InputEnabled = false;
            mGameManager.ShuffleSetOrder();
            ChangePosition();
            trade = 0;
        }
        for(int i=0; i < 4; i++)
        {
            mMusicians[i].StopFried();
        }      
        lightsOffEvent();
        //resetEvent();
        yield return null;
    }

    IEnumerator Change()
    {
        bool ok = false;
        mGameManager.HideSequenceText();
        mGameManager.ResetTimer();
        while (!ok)
        {
            for(int i=0; i<4; i++)
            {
                mBand[i].position = Vector3.Lerp(mBand[i].position, mPointOfTrade.position, Time.deltaTime * 4);
            }
            if(mBand[0].position == mBand[1].position && mBand[3].position == mPointOfTrade.position)
            {
                ok = true;
            }
            yield return null;
        }
        ok = false;
        while (!ok)
        {
            for (int i = 0; i < 4; i++)
            {
                mBand[mGameManager.mSetOrder[i]].position = Vector3.Lerp(mBand[mGameManager.mSetOrder[i]].position, position[i], Time.deltaTime * 6);
            }
            if (mBand[mGameManager.mSetOrder[0]].position == position[0] && mBand[mGameManager.mSetOrder[1]].position == position[1] && mBand[mGameManager.mSetOrder[2]].position == position[2] && mBand[mGameManager.mSetOrder[3]].position == position[3])
            {
                mGameManager.InputEnabled = true;
                mGameManager.ResetTimer();
                mGameManager.NewSequence();
                mGameManager.ShowSequenceText();
                ok = true;
            }
            yield return null;
        }
        yield return null;
    }

    public void TocarErro()
    {
        mAudioManager.PlaySFX("erro");
        mAudioManager.PlaySFX("boo");
    }
}
