﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AnimatedPointPool : MonoBehaviour {
    #pragma warning disable 0649
    [SerializeField] Color GainColor;
    [SerializeField] Color LossColor;
    [SerializeField] float midBarPulsesPerSecond = 4;
    [SerializeField] float midBarPulseMedianValue = 0.75f;
    [SerializeField] float midBarPulseVariance = 0.5f;
    [SerializeField] int TransitionSpeed = 10;
    [SerializeField] Color DefaultTextColor;
    [SerializeField] Color CriticalTextColor;
    [SerializeField] Color EmergencyTextColor;
    [SerializeField] float CriticalThreshold = 0.25f;
    [SerializeField] float EmergencyThreshold = 0.1f;
    #pragma warning restore 0649

    public int Value {
        get { return _Value; }
        set {
            float percent = (100.0f * value) / MaxValue;

            if (value > _Value) {
                MidImage.color = GainColor;
                var pos = MidTrans.anchoredPosition;
                pos.x = percent;
                MidTrans.anchoredPosition = pos;
                state = State.GAINING;
            }
            else if (value < _Value) {
                MidImage.color = LossColor;
                var midpos = MidTrans.anchoredPosition;
                var frontpos = FrontTrans.anchoredPosition;
                midpos.x = frontpos.x;
                MidTrans.anchoredPosition = midpos;
                frontpos.x = percent;
                FrontTrans.anchoredPosition = frontpos;
                state = State.LOSING;
            }

            _Value = value;
        }
    }

    public void Initialize(int value, int maxValue) {
        _Value = value;
        MaxValue = maxValue;

        ValueText.text = Value + "/" + MaxValue;
        float percent = (Value * 100.0f) / MaxValue;
        state = State.RESTING;

        Vector2 pos;

        pos = FrontTrans.anchoredPosition;
        pos.x = percent;
        FrontTrans.anchoredPosition = pos;

        pos = MidTrans.anchoredPosition;
        pos.x = percent;
        MidTrans.anchoredPosition = pos;

        RefreshText();
    }

    RectTransform FrontTrans;
    RectTransform MidTrans;
    Text ValueText;
    Image MidImage;
    float midAlpha;
    int MaxValue;
    int _Value;

    enum State {
        RESTING,
        GAINING,
        LOSING
    }

    State state = State.RESTING;

    void RefreshText() {
        float mobileValue = (Value * 100.0f) / MaxValue;
        if (state == State.GAINING) { mobileValue = FrontTrans.anchoredPosition.x; }
        else if (state == State.LOSING) { mobileValue = MidTrans.anchoredPosition.x; }

        float normalized = mobileValue / 100;
        ValueText.text = Mathf.FloorToInt(normalized * MaxValue) + "/" + MaxValue;

        if (normalized > CriticalThreshold) {
            ValueText.color = DefaultTextColor;
        }
        else if (normalized > EmergencyThreshold) {
            ValueText.color = CriticalTextColor;
        }
        else {
            ValueText.color = EmergencyTextColor;
        }

    }

    void Start() {
        ValueText = transform.Find("Value Text BG").GetComponentInChildren<Text>();
        FrontTrans = transform.Find("Front/Horizontal Mask/Diagonal Mask/Grip/Color Bar") as RectTransform;
        MidTrans = transform.Find("Mid/Horizontal Mask/Diagonal Mask/Grip/Color Bar") as RectTransform;
        MidImage = MidTrans.GetComponent<Image>();
    }

    void Update() {
        float thetaCoefficient = Mathf.PI * midBarPulsesPerSecond;
        float sineScale = 0.5f * midBarPulseVariance;

        midAlpha = (Mathf.Sin(Time.fixedTime * thetaCoefficient) * sineScale) + midBarPulseMedianValue;

        if (state != State.RESTING) {
            var frontPos = FrontTrans.anchoredPosition;
            var midPos = MidTrans.anchoredPosition;

            if (state == State.GAINING) {
                frontPos.x += TransitionSpeed * Time.deltaTime;
                if (frontPos.x > midPos.x) {
                    frontPos.x = midPos.x;
                    state = State.RESTING;
                }
                FrontTrans.anchoredPosition = frontPos;
            }
            else if(state == State.LOSING) {
                midPos.x -= TransitionSpeed * Time.deltaTime;
                if (midPos.x < frontPos.x) {
                    midPos.x = frontPos.x;
                    state = State.RESTING;
                }
                MidTrans.anchoredPosition = midPos;
            }

            var col = MidImage.color;
            col.a = midAlpha;
            MidImage.color = col;

            RefreshText();
        }
    }
}
