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

public class ScoreAccumulator : MonoBehaviour {

    public int DisplayedScore
    {
        get
        {
            return DisplayedShapeCount * ComboCount * ShiftMultiplier;
        }
    }
    public int Score
    {
        get
        {
            return ShapeCount * ComboCount * ShiftMultiplier;
        }
    }

    public int ShapeCount = 0;
    public int DisplayedShapeCount = 0;

    public int ComboCount = 0;
    public int ShiftMultiplier = 1;

    public void ResetAccumulator()
    {
        ShapeCount = 0;
        DisplayedShapeCount = 0;

        ComboCount = 0;
        ShiftMultiplier = 1;

        PointsLockedIn = false;
    }

    public int DisplayedTotalScore = 0;
    public int TotalScore = 0;
    public int ScoreFerry = 0;

    public Text textScore = null;
    public Text textComboCount = null;
    public Text textShapeShiftMultiplier = null;
    public Text textScoreFerry = null;
    public Text textTotalScore = null;

    // Points are not yet locked when we reset, and then they get locked in after "Locked In" gets called.
    private bool PointsLockedIn = false; 
     

    public void AddShapes(int n)
    {
        if (PointsLockedIn)
            print("Error - adding shapes when points are locked in");

        ShapeCount += n;
    }

    public void AddCombo(int n)
    {
        ComboCount += n;
        textComboCount.GetComponent<PulseText>().Pulse();
        GameManager.Instance.ComboCelebrator.PlayComboSound(ComboCount);
        GameManager.Instance.Annoucer.PlaySoundForCombo(ComboCount);
        Camera.main.GetComponent<CameraShake>().DoShake(0.07f + ComboCount * 0.03f);
            
    }

    public void SetShiftMultiplier(int n)
    {
        ShiftMultiplier = n;
    }

	// Use this for initialization
	void Start () {
        ResetAccumulator();
	}

    // Update is called once per frame
    void Update()
    {
        textScore.text = DisplayedScore.ToString();
        textComboCount.text = "x" + ComboCount;
        textShapeShiftMultiplier.text = "x" + ShiftMultiplier;

        bool show = (Score > 0);
        textScore.gameObject.SetActive(show);
        textComboCount.gameObject.SetActive(show);
        textShapeShiftMultiplier.gameObject.SetActive(show);


        // Update the total score
        textTotalScore.text = DisplayedTotalScore.ToString();
        textScoreFerry.text = "+" + ScoreFerry.ToString();
        textScoreFerry.gameObject.SetActive(DisplayedTotalScore < TotalScore);
    }

    public void LockInPoints()
    {
        if (!PointsLockedIn)
        { 
            PointsLockedIn = true;

            ScoreFerry = Score; // Show how many points we just earned
            TotalScore += Score; // Change our total score to be our new real score

            if (ComboCount > 3)
            {
                GameObject.Find("Cheering").GetComponent<AudioSource>().Play();
            }
        }
    }

    float NumericCounterTimeAccumulator = 0.0f;
    float NumericCounterTimePeriod = 0.05f;

    int ConsecutiveBeeps = 0;
    void FixedUpdate()
    {
        NumericCounterTimeAccumulator += Time.fixedDeltaTime;

        if (NumericCounterTimeAccumulator > NumericCounterTimePeriod)
        {
            NumericCounterTimeAccumulator = 0.0f;

            if (DisplayedShapeCount < ShapeCount)
            {
                int delta = ShapeCount - DisplayedShapeCount;
                int increment = 1 + delta / 5;
                DisplayedShapeCount += increment;

                textScore.GetComponent<PulseText>().Pulse();
            }

            if (DisplayedTotalScore < TotalScore)
            {
                int delta = TotalScore - DisplayedTotalScore;
                int increment = 1 + delta / 3;
                DisplayedTotalScore += increment;


                textTotalScore.GetComponent<AudioSource>().pitch = Mathf.Min(0.4f + 0.10f * ConsecutiveBeeps, 1.6f);
                textTotalScore.GetComponent<AudioSource>().Play();
                ConsecutiveBeeps++;
               
            }
            else
            {
                ConsecutiveBeeps = 0;
            }
        }
    }
}
