using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChoiceScreen : IInputReceivable
{
    public GameObject Choice01Obj, SelectionArrowObj;
    private const float MOVEMENT_THRESHOLD = 0.1f;

    private Color mHighlightColor = new Color(175.0f / 255.0f, 8.0f / 255.0f, 8.0f / 255.0f);

    private int mCurrentlySelectedChoiceIndex = -1;
    private MultipleChoices mMultipleChoice = null;
    private GameObject[] mChoiceObjs;
    private Dictionary<char, float> mCharactersLength;

    private void Awake()
    {
        RegisterButtonAction("NextButton", () => { }, () => { });
        RegisterButtonAction("OkButton", () => { }, () => { });
        RegisterButtonAction("FastForwardBtn", () => { }, () => { });
        InputRedirecter.Instance.InteractionPressed += HandleInteractionPressed;
        InputRedirecter.Instance.VerticalAxisKeyDown += HandleVerticalAxisKeyDown;
    }

    private void OnDestroy()
    {
        InputRedirecter.Instance.InteractionPressed -= HandleInteractionPressed;
        InputRedirecter.Instance.VerticalAxisKeyDown -= HandleVerticalAxisKeyDown;
    }

    private void HandleVerticalAxisKeyDown(object sender, InputRedirecter.AxisKeyEventArgs e)
    {
        if (e.Direction < 0)
        {
            IncIndex();
        }
        else
        {
            DecIndex();
        }
    }

    private void HandleInteractionPressed(object sender, System.EventArgs e)
    {
        ChoiceScreenCaller.Instance.Dismiss();
        DialogCaller.Instance.Call(mMultipleChoice.mChoices[mCurrentlySelectedChoiceIndex].FollowingDialogType, Enums.eDialogModes.NextOk);
    }

    public override void OnDataPackReceived(object dataPack)
    {
        Util.AssertTrue(dataPack as ChoiceScreenDatapack != null, "Datapacks sent to the choice screen must be choice screen datapacks");
        mMultipleChoice = ChoicesManager.Instance.GetMultipleChoice((dataPack as ChoiceScreenDatapack).MultipleChoiceType);
        mChoiceObjs = new GameObject[mMultipleChoice.mChoices.Length];
        mChoiceObjs[0] = Choice01Obj;
        for (int i = 1; i < mMultipleChoice.mChoices.Length; i++)
        {
            mChoiceObjs[i] = MonoBehaviour.Instantiate(Choice01Obj) as GameObject;
            mChoiceObjs[i].transform.parent = Choice01Obj.transform.parent;
            mChoiceObjs[i].transform.position = Choice01Obj.transform.position;
        }

        mCharactersLength = Util.CreateCharactersLengthDictionary(mChoiceObjs[0].GetComponent<tk2dTextMesh>());
        float accumulatedDistance = 0.0f;
        for (int i = 0; i < mMultipleChoice.mChoices.Length; i++)
        {
            mChoiceObjs[i].transform.position = new Vector3(mChoiceObjs[i].transform.position.x, mChoiceObjs[i].transform.position.y + accumulatedDistance, mChoiceObjs[i].transform.position.z);
            mChoiceObjs[i].GetComponent<tk2dTextMesh>().text = Util.FormatTk2dText(mChoiceObjs[i].GetComponent<tk2dTextMesh>(), 1.8f, mMultipleChoice.mChoices[i].Text, mCharactersLength);
            mChoiceObjs[i].GetComponent<tk2dTextMesh>().Commit();
            accumulatedDistance -= mChoiceObjs[i].GetSize(true, false).y;
        }
        mCurrentlySelectedChoiceIndex = 0;
        ConfigSelection();
    }

    private void IncIndex()
    {
        mCurrentlySelectedChoiceIndex++;
        if (mCurrentlySelectedChoiceIndex >= mChoiceObjs.Length)
        {
            mCurrentlySelectedChoiceIndex = 0;
        }
        ConfigSelection();
    }

    private void DecIndex()
    {
        mCurrentlySelectedChoiceIndex--;
        if (mCurrentlySelectedChoiceIndex < 0)
        {
            mCurrentlySelectedChoiceIndex = mChoiceObjs.Length - 1;
        }
        ConfigSelection();
    }

    private void ConfigSelection()
    {
        for (int i = 0; i < mChoiceObjs.Length; i++)
        {
            if (i == mCurrentlySelectedChoiceIndex)
            {
                mChoiceObjs[i].GetComponent<tk2dTextMesh>().color = mHighlightColor;
            }
            else
            {
                mChoiceObjs[i].GetComponent<tk2dTextMesh>().color = Color.black;
            }
            mChoiceObjs[i].GetComponent<tk2dTextMesh>().Commit();
        }

        SelectionArrowObj.transform.position = new Vector3
        (
            mChoiceObjs[mCurrentlySelectedChoiceIndex].transform.position.x - SelectionArrowObj.GetSize(true, false).x / 2 - 0.05f,
            mChoiceObjs[mCurrentlySelectedChoiceIndex].transform.position.y - mChoiceObjs[mCurrentlySelectedChoiceIndex].GetSize(true, false).y / 2 - 0.01f,
            SelectionArrowObj.transform.position.z
        );
    }
}