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

public class Necromancer : MonoBehaviour
{
	public float speed = 3.0f;
    public float outerBounds = 10.0f;
    private SpriteRenderer necroSpriteRenderer;
    private int spriteFacing;

	// 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>();
	    spriteFacing = 1;
	}

    // Update is called once per frame
    void Update()
    {
    	float xAxis = Input.GetAxis ("Horizontal");
        if (xAxis == 0) {
            return;
        }

    	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);
        EventManager.TriggerPlayerMoved(transform.position.x);
    }

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


}
