using UnityEngine;
using System.Collections;

public class PlayerTurn : BaseTurn {

    public PlayerTurn(PlayerObject obj) : base (obj.GetComponent<CharacterObject>() )
    {
        m_playerObj = obj;
        m_playerObj.Info.m_pendingXP = 0;
        m_playerObj.Info.m_pendingGold = 0;
    }

    public override void StartMoveLocPick()
    {
        m_playerObj.WaitForGridClick(true);
    }

    public override bool DoMoveChoice(out GridCoord destPos)
    {
        bool cancel; // ignored. Can't cancel moves
        if ( m_playerObj.CheckMousePick(out destPos, out cancel) )
        {
            if ( destPos == m_playerObj.GetComponent<GridObject>().GridPosition )
            {
                SFXManager.Play(SFXManager.Instance.m_error);
            }
            else
            {
                SFXManager.Play(SFXManager.Instance.m_playerMove);
            }
            return true;
        }
        return false;
    }

    public override bool PlayerWantsActionMenu()
    {
        if ( m_playerObj.PlayerWasSelected() )
        {
            m_playerObj.ShowMenu(m_playerObj.GetComponent<GridObject>().GridPosition, "Action", PlayerObject.ACTION_MENU);

            return true;
        }
        return false;
    }

    public override void HasMoved()
    {
        m_playerObj.WaitForGridClick(true);
    }

    public override bool MadeMenuChoiceMove(out MenuAction choice)
    {
        return m_playerObj.CheckMenuResult(out choice);
    }

    public override void StartActionLocPick()
    {
        m_playerObj.WaitForGridClick(false);
    }

    public override bool DoActionLocationPick(out GridCoord targetPos, out bool cancel)
    {
        if ( m_playerObj.CheckMousePick( out targetPos, out cancel ))
        {
            if (!cancel)
            {
                m_playerObj.ShowMenu(targetPos, "Do Action?", PlayerObject.CONFIRM_MENU);
            }

            return true;
        }
        return false;
    }

    public override bool MadeMenuChoiceAction(out MenuAction choice)
    {
        return m_playerObj.CheckMenuResult(out choice);
    }

    public override void ShowMagicChoiceMenu()
    {
        m_playerObj.ScrollMenu.NewChoiceMenuCaption("Choose Magic To Use");

        CharacterObject charObj = m_playerObj.GetComponent<CharacterObject>();
        CharacterInfo info = charObj.Info;

        for (int i = 0; i < info.m_magic.Count; i++)
        {
            CharacterMagic magic = CharacterMagic.GetMagic(info.m_magic[i]);
            m_playerObj.ScrollMenu.Add(i, string.Format( "{0} ({1} MP)", magic.m_name, magic.m_mpCost ) );
        }

        m_playerObj.ShowScrollMenu();
    }

    public override void ShowItemChoiceMenu()
    {
        m_playerObj.ScrollMenu.NewChoiceMenuCaption("Choose Item To Use");

        CharacterObject charObj = m_playerObj.GetComponent<CharacterObject>();
        CharacterInfo info = charObj.Info;

        for (int i = 0; i < info.m_inventory.Count; i++)
        {
            CharacterItem item = CharacterItem.GetItem(info.m_inventory[i]);
            m_playerObj.ScrollMenu.Add(i, item.m_name);
        }

        m_playerObj.ShowScrollMenu();
    }

    public override bool MadeMagicChoice(out int choiceIndex)
    {
        if ( m_playerObj.CheckScrollMenuResult(out choiceIndex) )
        {
            CharacterObject charObj = m_playerObj.GetComponent<CharacterObject>();
            CharacterInfo info = charObj.Info;

            if ( choiceIndex >= 0 )
            {
                CharacterMagic magic = CharacterMagic.GetMagic(info.m_magic[choiceIndex]);
                if ( magic.m_mpCost > info.m_currentMp )
                {
                    GuiStatusText.DisplayText( magic.m_name + ": Not enough MP to use", GuiStatusText.DEFAULT_TIME );
                    choiceIndex = -1;
                }
            }

            return true;
        }
        return false;
    }

    public override bool MadeItemChoice(out int choiceIndex)
    {
        if (m_playerObj.CheckScrollMenuResult(out choiceIndex))
        {
            CharacterObject charObj = m_playerObj.GetComponent<CharacterObject>();
            CharacterInfo info = charObj.Info;

            if (choiceIndex >= 0)
            {
                CharacterItem item = CharacterItem.GetItem(info.m_inventory[choiceIndex]);
                if ( item.m_usable == false )
                {
                    GuiStatusText.DisplayText(item.m_name + ": This item cannot be used", GuiStatusText.DEFAULT_TIME);
                    choiceIndex = -1;
                }
            }

            return true;
        }
        return false;
    }

    PlayerObject m_playerObj;
}
