using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;

public class LDPlayer : LDGameActor
{
    public LDWeaponBeam mainWeapon;

    private TM_CombatComponent myCombatComponent;

    [HideInInspector] 
    public bool bIgnoreInputThisFrame;
    
    public Rewired.Player myRewiredPlayer;

    public int iEnergyCollected;

    public AudioClip clip_GainHealth;
    public AudioClip clip_GainEnergy;
    public AudioClip clip_TakeDamage;
    public AudioClip clip_DiedLol;


    protected override void Awake()
    {
        base.Awake();
        myRewiredPlayer = Rewired.ReInput.players.GetPlayer(0);
    }

    // Use this for initialization
	protected override void Start ()
	{
        base.Start();
   
	}

    public void PrepareForNewGame()
    {
        theGame.theAIManager.RegisterPlayer(this);

        mainWeapon.OwningCombatComponent = myCombatComponent;
        mainWeapon.myOwningActor = this;

        GetComponent<SpriteRenderer>().enabled = true;

        fCurrentHealth = fMaxHealth;
        bDeadAndDying = false;
        iEnergyCollected = 0;
    }

    public override Vector2 GetCameraFocusPosition()
    {
        //float a little
        fDistanceFromGround = Mathf.Lerp(1.2f, 2.0f, Mathf.PingPong(Time.time, 4.0f) / 4.0f);

        //stop bobbing if the game isn't active
        if (theGame.GetGameState() != EGameState.gameplay)
        {
            fDistanceFromGround = 1.5f;
        }

        Vector2 vReturnPos = vWorldPos;
        vReturnPos.y -= (fDistanceFromGround * 0.5f);
        return vReturnPos;
    }

    // Update is called once per frame
	protected override void Update ()
    {
        base.Update();
        if (theGame.GetGameState() != EGameState.gameplay)
        {
            return;
        }

        /*
        if (GameManager.GetGameState() != EGameState.gameplay &&
            GameManager.GetGameState() != EGameState.shopping &&
            GameManager.GetGameState() != EGameState.wave_intro &&
            GameManager.GetGameState() != EGameState.wave_outro)
        {
            return;
        }
        */

        /*
	    if (iHealth <= 0)
	    {
	        return;
	    }
        */

        //UpdateMovementAndTransformPosition();
        UpdateWeapon(mainWeapon, false);

        //Blink when invincible
	    /*
        if (fInvincibilityTimer > 0f)
	    {
	        SpriteRenderer mySpriteRenderer = GetComponent<SpriteRenderer>();
	        mySpriteRenderer.enabled = !mySpriteRenderer.enabled;

	        fInvincibilityTimer -= Time.deltaTime;
	        if (fInvincibilityTimer <= 0f)
	        {
	            mySpriteRenderer.enabled = true;
	        }
	    }
        */
	    bIgnoreInputThisFrame = false;

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            theGame.PlayMultifoom(vWorldPos);
        }
    }

    void UpdateWeapon( TM_Weapon thisWeapon, bool bUseAltFire)
    {
        TM_SpriteAnimator weaponAnim = thisWeapon.GetComponent<TM_SpriteAnimator>();



        Vector2 vRightStick =  myRewiredPlayer.GetAxis2D("AimHorizontal", "AimVertical");

        //if the stick is dead this frame, don't change facing or move
        if (vRightStick != Vector2.zero)
        {
            //face that direction
            Vector3 rotation = thisWeapon.transform.rotation.eulerAngles;
            rotation.z = Vector2.Angle(Vector2.up, vRightStick);
            if (vRightStick.x > 0)
                rotation.z *= -1.0f;

            thisWeapon.transform.localRotation = Quaternion.Euler(rotation);
            vRightStick.Normalize();
            thisWeapon.transform.localPosition = new Vector3(vRightStick.x, vRightStick.y);
        }

        //player shooting: 
        if (TM_InputManager.IsAnyFireKeyPressed(bUseAltFire) ||
            TM_InputManager.IsAnyFireTriggerPulled(bUseAltFire))
        {
            thisWeapon.StartBurst();
            if (weaponAnim != null)
            {
                weaponAnim.StartIfNotRunning("fire");
            }
        }
        else
        {
            //immediately stop anim if not shooting
            if (weaponAnim != null)
            {
                weaponAnim.SetAnimation("idle");
            }
        }
    }

    public override void TakeDamage(float fDamage, LDGameActor source)
    {
        TM_AudioManager.PlayClip(clip_TakeDamage);
        fDamage *= 2.0f;
        base.TakeDamage(fDamage, source);


        TM_SpriteAnimator anim = GetComponent<TM_SpriteAnimator>();
        if (anim != null)
        {
            anim.StartIfNotRunning("hurt");
            anim.behavior = EShepAnimBehavior.play_once_then_idle;
        }
    }

    public override void OnCollectPickup(LDPickupProjectile pickup)
    {
        if (pickup.EnergyAwarded > 0)
        {
            TM_AudioManager.PlayClip(clip_GainEnergy);
            iEnergyCollected += pickup.EnergyAwarded;
        }

        if (pickup.HealthAwarded > 0)
        {
            TM_AudioManager.PlayClip(clip_GainHealth);
            GiveHealth(pickup.HealthAwarded);
        }
        
    }

    public override Vector2 GetPickupCollectionPosition()
    {
        return new Vector2(vWorldPos.x, vWorldPos.y + fDistanceFromGround );
    }

    public override void OnKilled(LDGameActor source)
    {
        if (bDeadAndDying)
        {
            return;
        }

        bDeadAndDying = true;

        theGame.PlayMultifoom(vWorldPos);
        GetComponent<SpriteRenderer>().enabled = false;

        theGame.SetGameState(EGameState.gameover);

    }
}
