using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Gamelogic.Grids2;

public class MouseInput : MonoBehaviour {
    public Transform SplatShotPrefab;
    public Transform SplatHitPrefab;

    private GridPoint2 queuedTriggerLocation;
    private bool triggered = false;

    private GridPoint2 queuedSlamLocation;
    private bool slamTriggered = false;

    void Update()
    {
        if (!GameController.Instance.isGameRunning())
            return;

        if (Input.GetMouseButtonDown(0))
        {
            QueueMove();
        }
        else if (Input.GetMouseButtonDown(1))
        {
            QueueSlam();
        }
    }

    private void QueueSlam()
    {
        GridPoint2 p = GameMap.GetGridPointUnderMouse();

        if (GameMap.Contains(p))
        {
            queuedSlamLocation = p;
            slamTriggered = true;
            TurnBasedGameLoop.instance.ApplyPlayerInput();
        }
        else
            RenderSounds.PlaySound("ErrorBeep");
    }

    private void QueueMove()
    {
        GridPoint2 p = GameMap.GetGridPointUnderMouse();

        if (GameMap.Contains(p))
        {
            queuedTriggerLocation = p;
            triggered = true;
            TurnBasedGameLoop.instance.ApplyPlayerInput();
        }
        else
            RenderSounds.PlaySound("ErrorBeep");
    }

    void PlanTurn(List<TurnAction> actions)
    {
        if (slamTriggered)
            DoRoll(actions);
        else if (triggered)
            DoMove(actions);
    }
    
    private void DoMove(List<TurnAction> actions)
    {
        triggered = false;
        slamTriggered = false;

        if (!GameMap.Cell[queuedTriggerLocation].HighLight)
        {
            RenderSounds.PlaySound("ErrorBeep");
            return;
        }

        actions.Add(BuildMoveAction());
    }

    private TurnAction BuildMoveAction()
    {
        float dist = (float)PointyHexPoint.HexNorm(CombatLib.Player.Location - queuedTriggerLocation);
        if (dist > 1)
            dist *= 0.7f;
        TurnAction move = new ActionMove(CombatLib.Player, CombatLib.Player.Location, queuedTriggerLocation, dist);
        TurnAction splat = new ActionAttackSplat(queuedTriggerLocation, SplatShotPrefab, SplatHitPrefab);
        move.FollowupAction = splat;
        return move;
    }

    private void DoSlam(List<TurnAction> actions)
    {
        triggered = false;
        slamTriggered = false;

        if (GameMap.AreOnHexLine(queuedSlamLocation, CombatLib.Player.Location))
        {
            GridPoint2 dir = GameMap.GetBestHexDirection(CombatLib.Player.Location, queuedSlamLocation);
            actions.Add(new ActionSlam(dir));
        }
        else
            RenderSounds.PlaySound("ErrorBeep");
    }

    private void DoRoll(List<TurnAction> actions)
    {
        triggered = false;
        slamTriggered = false;

        if (IsQueuedRollValid())
        {
            GridPoint2 dir = GameMap.GetBestHexDirection(CombatLib.Player.Location, queuedSlamLocation);
            ActionRoll roll = new ActionRoll(CombatLib.Player, dir);
            TurnAction splat = new ActionAttackSplat(roll.GetLanding(), SplatShotPrefab, SplatHitPrefab);
            roll.FollowupAction = splat;
            actions.Add(roll);
        }
        else
            RenderSounds.PlaySound("ErrorBeep");

    }

    private bool IsQueuedRollValid()
    {
        if (!GameMap.AreOnHexLine(queuedSlamLocation, CombatLib.Player.Location))
            return false;

        GridPoint2 dir = GameMap.GetBestHexDirection(CombatLib.Player.Location, queuedSlamLocation);
        return GameMap.Available(CombatLib.Player.Location + dir);
    }

}
