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

public class Soccerman : MonoBehaviour
{
    public int playerIndex;
    InputDevice myDevice;

    public Rigidbody myBody;

    public float moveSpeed;
    float dashSpeed;
    public float turnSpeed;
    public float kickStrength;
    public float blockSize;
    public float dashMax;
    public float pullRange;
    public float kickDelay;

    Vector3 myVelo = Vector3.zero;
    Vector3 aimPoint = Vector3.zero;
    Vector3 dirToAim;
    float aimDot;

    Vector3 myAng = Vector3.zero;

    float xIn, yIn;
    Vector3 inputVector = Vector3.zero;

    public bool hasBall = false;
    public Vector3 ballPos;

    Soccermagnet magnet;
    bool isChargingDash = false;
    bool isEndingDash = false;
    bool isDashing = false;
    bool dashButtonPressed = false;
    float dashTimer;

    public float turboRemaining;
    bool isTurbo = false;

    int currentFrameIndex;
    int maxFrames;
    public List<Sprite> normSprites;
    public List<Sprite> glowSprites;
    SpriteRenderer sr;

    float animTimer;
    float timePerSprite;
    bool shouldAlternateSprites = false;

    public int teamIndex;
    public bool isExploding = false;

	// Use this for initialization
	void Start ()
    {
        if (InputManager.Devices.Count > playerIndex)
            myDevice = InputManager.Devices[playerIndex];

        myBody = GetComponent<Rigidbody>();
        magnet = GetComponentInChildren<Soccermagnet>();

        dashSpeed = moveSpeed * 4.0f;

        sr = transform.Find("New Sprite").GetComponent<SpriteRenderer>();
        maxFrames = normSprites.Count;
        timePerSprite = dashMax / (maxFrames - 1);
    }
	
	// Update is called once per frame
	void Update ()
    {
        if (myDevice == null) return;
        if (isExploding) return;

        xIn = myDevice.LeftStick.X;
        yIn = myDevice.LeftStick.Y;
        inputVector.x = xIn;
        inputVector.z = yIn;
        
        aimPoint = transform.position + (Vector3.right * myBody.velocity.normalized.x) + (Vector3.forward * myBody.velocity.normalized.z);
        //Debug.Log("x: " + xIn + " y: " + yIn);

        ballPos = transform.position + transform.forward * 0.5f;
        ballPos.y = 0.8f;

        if(!isEndingDash && !isDashing)
        {
            if(myDevice.Action1.WasPressed)
            {
                ChargeDash();
                dashButtonPressed = true;
            }
            if (myDevice.Action1.WasReleased && dashButtonPressed)
                Dash();
        }

        if (dashButtonPressed && !myDevice.Action1.IsPressed)
            dashButtonPressed = false;

        if (isChargingDash)
        {
            dashTimer += Time.deltaTime;
            dashTimer = Mathf.Clamp(dashTimer, 0.0f, dashMax);

            animTimer += Time.deltaTime;

            if (currentFrameIndex > 10)
            {
                if(currentFrameIndex == 11)
                {
                    if(animTimer >= timePerSprite * 4)
                    {
                        currentFrameIndex = 12;
                        animTimer = 0;
                    }
                }
                else
                {
                    if (animTimer >= timePerSprite)
                    {
                        currentFrameIndex = 11;
                        animTimer = 0;
                    }
                }
            }
            else
            {
                currentFrameIndex = Mathf.FloorToInt((animTimer / timePerSprite)) + 1;
                if (currentFrameIndex == 11)
                    animTimer = 0;
            }

            sr.sprite = glowSprites[currentFrameIndex];
        }
        else if(isEndingDash)
        {
            animTimer -= Time.deltaTime;
            if(animTimer <= 0)
            {
                if(currentFrameIndex > 0)
                {
                    currentFrameIndex--;
                    animTimer = timePerSprite * 1.5f;
                    sr.sprite = normSprites[currentFrameIndex];
                }
                else
                {
                    isEndingDash = false;
                }
            }
        }
        else if (isDashing)
        {
            dashTimer -= Time.deltaTime;
            if (dashTimer <= 0)
            {
                isDashing = false;
                isEndingDash = true;
                myBody.drag = 3;
            }
        }
    }

    void FixedUpdate()
    {
        if (isExploding) return;

        if (isChargingDash)
        {
            myBody.velocity = Vector3.zero;
            myBody.angularVelocity = Vector3.zero;

            if(inputVector.magnitude > 0.15f)
            {

                aimPoint = transform.position + inputVector;
                dirToAim = (aimPoint - transform.position).normalized;
                aimDot = Vector3.Dot(transform.forward, dirToAim);

                if (aimDot <= 0.975f)
                {
                    if (transform.InverseTransformPoint(aimPoint).x < 0)
                        myAng.y = -turnSpeed * 0.00025f;
                    else
                        myAng.y = turnSpeed * 0.00025f;
                }
                else
                    myAng.y = 0;

                myBody.angularVelocity = myAng;
            }

            return;
        }
        if (isDashing)
        {
            //myVelo = transform.forward * dashSpeed;
            //myBody.velocity = myVelo;
            return;
        }


        myVelo.x = xIn * moveSpeed;
        myVelo.z = yIn * moveSpeed;

        dirToAim = (aimPoint - transform.position).normalized;
        //Debug.Log(Vector3.Dot(transform.forward, dirToAim));
        aimDot = Vector3.Dot(transform.forward, dirToAim);

        float turnSpeedMultiplier = 1.0f;
        if (isChargingDash)
            turnSpeedMultiplier = 0.4f;

        //Debug.Log(aimDot);
        if (myBody.velocity.magnitude > 0.25f)
        {
            if (aimDot <= 0.975f)
            {
                if (transform.InverseTransformPoint(aimPoint).x < 0)
                    myAng.y = -turnSpeed * turnSpeedMultiplier;
                else
                    myAng.y = turnSpeed * turnSpeedMultiplier;
            }
            else
                myAng.y = 0;
        }
        else
            myAng.y = 0;

        myBody.angularVelocity = myAng;
        

        //myBody.velocity = Vector3.zero;
        

        //if (isTurbo)
        //    myVelo *= 2.0f;

        //if (aimDot >= 0.0f)
        //    myBody.velocity = myVelo;
        //else
        //    myBody.velocity = Vector3.zero;
        //myBody.velocity = myVelo;

        if(!isChargingDash)
        {
            Vector3 movementForce = myVelo.normalized * 2000;
            myBody.AddForce(movementForce);
            myBody.velocity = Vector3.ClampMagnitude(myBody.velocity, moveSpeed);
        }

        //Debug.Log("velo: " + myBody.velocity + " force: " + movementForce);
    }

    void ChargeDash()
    {
        isChargingDash = true;
        myBody.angularVelocity = Vector3.zero;

        animTimer = 0;
    }
    
    void Dash()
    {
        isChargingDash = false;
        isDashing = true;

        //currentFrameIndex = 11;
        if (currentFrameIndex > 10)
            currentFrameIndex = 11;
        sr.sprite = glowSprites[currentFrameIndex];
        animTimer = timePerSprite * 2.5f;

        myVelo = transform.forward * dashSpeed;
        myBody.velocity = myVelo;
        myBody.drag = 0;
    }

    void ReenableMagnet() { magnet.SetMagnetEnabled(true); }
}
