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

public class Necromancer : MobBase
{
    public override LevelObjectFaction ObjectFaction { get { return LevelObjectFaction.Zombie; } }
    public override LevelObjectType ObjectType { get { return LevelObjectType.Player; } }

    public float outerBounds = 17.0f;
    public int goo = 0;
    public Rigidbody2D rb;
    public bool knockBack;
    public float thrust = 250f;
    private bool knockBackRight;
    private SpriteRenderer necroSpriteRenderer;
    private int spriteFacing;
    private StructureController targetStructure;
    private Color originalNecroColor;

    Animator animator;

    //some flags to check when certain animations are playing
    bool isIdle = false;
    bool isWalking = false;

    //animation states - the values in the animator conditions
    const int STATE_IDLE = 0;
    const int STATE_WALK = 1;
    int _currentAnimationState = STATE_IDLE;

    // This function is called just one time by Unity the moment the component loads
    private void Awake()
	{
	    // get a reference to the SpriteRenderer component on this gameObject
	    necroSpriteRenderer = GetComponent<SpriteRenderer>();
        originalNecroColor = necroSpriteRenderer.material.color;
        rb = this.GetComponent<Rigidbody2D>();
	    spriteFacing = 1;
        animator = this.GetComponent<Animator>();
	}

    // Update is called once per frame
    void Update()
    {
        if (knockBack)
        {
            // Default to 1
            int thrustCoefficient = 1;

            // Determine if bounce is to the right or the left
            knockBack = !knockBack;
            if (!knockBackRight)
                thrustCoefficient = -1;
            rb.AddForce(transform.right * thrust * thrustCoefficient);
        }
        else
        {
            moveHandler();

            if (targetStructure != null && Input.GetKeyDown("space"))
            {
                targetStructure.PlayerAction(this);
            }
        }
        EventManager.TriggerPlayerMoved(transform.position.x);
    }

    void moveHandler()
    {
        float xAxis = Input.GetAxis("Horizontal");
        if (xAxis == 0)
        {
            changeState(STATE_IDLE);
            return;
        }

        changeState(STATE_WALK);
        if (xAxis < 0)
        {
            spriteFacing = -1;
        }
        else if (xAxis > 0)
        {
            spriteFacing = 1;
        }
        if (spriteFacing == -1 && necroSpriteRenderer != null)
        {
            necroSpriteRenderer.flipX = true;
        }
        else if (spriteFacing == 1 && necroSpriteRenderer != null)
        {
            necroSpriteRenderer.flipX = false;
        }

        float newX = transform.position.x + (xAxis * Speed * Time.deltaTime);
        if (newX > outerBounds || newX < (-1 * outerBounds))
        { //Prevent user from going out of bounds
            return;
        }

        transform.Translate(Vector3.right * xAxis * Speed * Time.deltaTime);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log($"{this.name}:{this.name} entered {collision.name} space.");
        targetStructure = collision.gameObject.GetComponent<StructureController>();
        if (targetStructure != null)
        {
            Debug.Log($"{this.name}:{targetStructure.name} structure found");

        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        Debug.Log($"{this.name}:{this.name} exited {collision.name} space.");
        targetStructure = null;
    }

    public void receiveDamage(bool knockRight)
    {
        knockBack = true;
        knockBackRight = knockRight;
        Debug.LogWarning("Damage Applied");
        StartCoroutine("Flasher"); //VERY IMPORTANT!  You 'must' start coroutines with this code.
    }

    IEnumerator Flasher()
    {
        for (int i = 0; i < 5; i++)
        {
            
            necroSpriteRenderer.material.color = Color.red;
            yield return new WaitForSeconds(.1f);
            necroSpriteRenderer.material.color = new Color(255,255,255,255);
            yield return new WaitForSeconds(.1f);
        }
        necroSpriteRenderer.material.color = originalNecroColor;
    }

    //--------------------------------------
    // Change the players animation state
    //--------------------------------------
    void changeState(int state)
    {

        if (_currentAnimationState == state)
            return;

        switch (state)
        {

            case STATE_WALK:
                animator.SetInteger("State", STATE_WALK);
                break;

            case STATE_IDLE:
                animator.SetInteger("State", STATE_IDLE);
                break;

        }

        _currentAnimationState = state;
    }
}
