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

public class ChoiceSelector : MonoBehaviour {
    private float selectionAngle = 0;
    public int selectionsNumber = 4;
    public Image[] images;
    public Text[] texts;
    private RectTransform selectorTransform;
    private Vector2 input1;
    private Vector2 input2;
    [HideInInspector]
    public float alpha = 0.0f;
    [HideInInspector]
    public int selectedButton;
    private Emotion.types[] emotions;
    [HideInInspector]
    public bool canSelectSomething;
    private float time = 0.0f;
    private Entity mainEntity;

    // Use this for initialization
    void Awake() {
        alpha = 0.0f;
    }

    public void UpdateInput1(Vector2 i) {
        input1 = i;
    }
    public void UpdateInput2(Vector2 i) {
        input2 = i;
    }

    // Update is called once per frame
    void Update() {
        Vector2 curMouseDirection = new Vector2(input1.x + input2.x, (input1.y + input2.y) * -1);
        if (curMouseDirection.magnitude > 0.3 && alpha > 0.9f) {
            time += Time.deltaTime;
            if (time > 0.3) {
                if (curMouseDirection.y > 0) {
                    selectedButton++;
                }
                else {
                    selectedButton--;
                }
                if (selectedButton >= selectionsNumber) {
                    selectedButton = 0;
                }
                else if (selectedButton < 0) {
                    selectedButton = selectionsNumber - 1;
                }
                time = 0.0f;
            }
        }
        else {
            time = 999.0f;
        }

        // Color change

        UpdateColors();

        while (canSelectSomething && !CheckEmotion(selectedButton)) {
            if (curMouseDirection.y > 0 && curMouseDirection.magnitude > 0.2) {
                selectedButton++;
            }
            else {
                selectedButton--;
            }
            if (selectedButton >= selectionsNumber) {
                selectedButton = 0;
            }
            else if (selectedButton < 0) {
                selectedButton = selectionsNumber - 1;
            }
        }
    }

    public void SetEmotions(Emotion.types[] list) {
        emotions = list;
    }

    private void UpdateColors() {
        if (emotions == null) {
            emotions = new Emotion.types[4] { Emotion.types.None, Emotion.types.None, Emotion.types.None, Emotion.types.None };
        }
        texts[0].color = new Color(texts[0].color.r, texts[0].color.g, texts[0].color.b, CheckEmotion(0) ? alpha : alpha * 0.5f);
        texts[1].color = new Color(texts[1].color.r, texts[1].color.g, texts[1].color.b, CheckEmotion(1) ? alpha : alpha * 0.5f);
        texts[2].color = new Color(texts[2].color.r, texts[2].color.g, texts[2].color.b, CheckEmotion(2) ? alpha : alpha * 0.5f);
        texts[3].color = new Color(texts[3].color.r, texts[3].color.g, texts[3].color.b, CheckEmotion(3) ? alpha : alpha * 0.5f);
        Color selectedColor = new Color(1.0f, 1.0f, 1.0f, 0.8f * alpha);
        Color notSelectedColor = new Color(0.22f, 0.22f, 0.22f, 0.22f * alpha);
        Color disabledColor = new Color(0.15f, 0.15f, 0.15f, 0.0f * alpha);
        Color notVisible = new Color(0.22f, 0.22f, 0.22f, 0.0f * alpha);
        canSelectSomething = false;
        if (selectionsNumber > 0) {
            if (!CheckEmotion(0)) {
                images[0].color = Color.Lerp(images[0].color, disabledColor, Time.deltaTime * 7.0f);
            }
            else if (selectedButton == 0) {
                images[0].color = Color.Lerp(images[0].color, selectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
            else {
                images[0].color = Color.Lerp(images[0].color, notSelectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
        }
        else {
            images[0].color = Color.Lerp(images[0].color, notVisible, Time.deltaTime * 7.0f);
        }

        if (selectionsNumber > 1) {
            if (!CheckEmotion(1)) {
                images[1].color = Color.Lerp(images[1].color, disabledColor, Time.deltaTime * 7.0f);
            }
            else if (selectedButton == 1) {
                images[1].color = Color.Lerp(images[1].color, selectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
            else {
                images[1].color = Color.Lerp(images[1].color, notSelectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
        }
        else {
            images[1].color = Color.Lerp(images[1].color, notVisible, Time.deltaTime * 7.0f);
        }

        if (selectionsNumber > 2) {
            if (!CheckEmotion(2)) {
                images[2].color = Color.Lerp(images[2].color, disabledColor, Time.deltaTime * 7.0f);
            }
            else if (selectedButton == 2) {
                images[2].color = Color.Lerp(images[2].color, selectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
            else {
                images[2].color = Color.Lerp(images[2].color, notSelectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
        }
        else {
            images[2].color = Color.Lerp(images[2].color, notVisible, Time.deltaTime * 7.0f);
        }

        if (selectionsNumber > 3) {
            if (!CheckEmotion(3)) {
                images[3].color = Color.Lerp(images[3].color, disabledColor, Time.deltaTime * 7.0f);
            }
            else if (selectedButton == 3) {
                images[3].color = Color.Lerp(images[3].color, selectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
            else {
                images[3].color = Color.Lerp(images[3].color, notSelectedColor, Time.deltaTime * 7.0f);
                canSelectSomething = true;
            }
        }
        else {
            images[3].color = Color.Lerp(images[3].color, notVisible, Time.deltaTime * 7.0f);
        }
    }

    public void OnEnable() {
        UpdateColors();
    }

    public bool CheckEmotion(int index){
        if (mainEntity == null) {
            mainEntity = ThirdPersonController.main.entity;
        }
        if (emotions[index] == Emotion.types.None) {
            return true;
        }
        else {
            return mainEntity.emotions.Contains(emotions[index]);
        }
    }
}
