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

public class PainKiller : MonoBehaviour
{
    UInt16 up, down, left, right;
    private Animator animator;
    float speed, MouseYAxis, MouseXAxis;
    public bool freezeonx, freezeony;
    public int hitCounter;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        transform.position = new Vector3(-6, 0, -0.01f);
        speed = 4f;
        freezeonx = false;
        freezeony = false;
        hitCounter = 0;
    }

    private void Update()
    {
       
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        
        up = Convert.ToUInt16(Input.GetKey(KeyCode.W));
        down = Convert.ToUInt16(Input.GetKey(KeyCode.S));
        left = Convert.ToUInt16(Input.GetKey(KeyCode.A));
        right = Convert.ToUInt16(Input.GetKey(KeyCode.D));

        if (up == 0 && down == 0 && right == 0 && left == 0)
        {
            animator.SetBool("isWalking", false);
        }
        if (up == 1 || down == 1 || right == 1 || left == 1)
        {
            animator.SetBool("isWalking", true);
            animator.SetFloat("velX", right - left);
            animator.SetFloat("velY", up - down);
        }

        transform.Translate(new Vector3(speed * Time.deltaTime * (right - left), speed * Time.deltaTime * (up - down), 0));




        if (Input.GetMouseButton(0))
        {
            MouseXAxis = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
            MouseYAxis = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
            if (MouseXAxis - transform.position.x < -4)
            {
                if (MouseYAxis - transform.position.y > 0 && MouseYAxis - transform.position.y < 1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", 0);
                }
                if (MouseYAxis - transform.position.y > 1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", 1);
                }


                if (MouseYAxis - transform.position.y < 0 && MouseYAxis - transform.position.y > -1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", 0);
                }
                if (MouseYAxis - transform.position.y < -1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", -1);
                }
            }
            if (MouseXAxis - transform.position.x < -2 && MouseXAxis - transform.position.x > -4)
            {
                if (MouseYAxis - transform.position.y > 1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", 1);
                }
                if (MouseYAxis - transform.position.y < -1.25f)
                {
                    animator.SetFloat("velX", -1);
                    animator.SetFloat("velY", -1);
                }
            }
            if (MouseXAxis - transform.position.x < 0 && MouseXAxis - transform.position.x > -2)
            {
                if (MouseYAxis - transform.position.y > 2.5f)
                {
                    animator.SetFloat("velX", 0);
                    animator.SetFloat("velY", 1);
                }
                if (MouseYAxis - transform.position.y < -2.5f)
                {
                    animator.SetFloat("velX", 0);
                    animator.SetFloat("velY", -1);
                }
            }
            if (MouseXAxis - transform.position.x > 0 && MouseXAxis - transform.position.x < 2)
            {
                if (MouseYAxis - transform.position.y > 2.5f)
                {
                    animator.SetFloat("velX", 0);
                    animator.SetFloat("velY", 1);
                }
                if (MouseYAxis - transform.position.y < -2.5f)
                {
                    animator.SetFloat("velX", 0);
                    animator.SetFloat("velY", -1);
                }
            }
            if (MouseXAxis - transform.position.x > 2 && MouseXAxis - transform.position.x < 4)
            {
                if (MouseYAxis - transform.position.y > 1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", 1);
                }
                if (MouseYAxis - transform.position.y < -1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", -1);
                }
            }
            if (MouseXAxis - transform.position.x > 4)
            {
                if (MouseYAxis - transform.position.y > 0 && MouseYAxis - transform.position.y < 1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", 0);
                }
                if (MouseYAxis - transform.position.y > 1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", 1);
                }

                if (MouseYAxis - transform.position.y < 0 && MouseYAxis - transform.position.y > -1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", 0);
                }
                if (MouseYAxis - transform.position.y < -1.25f)
                {
                    animator.SetFloat("velX", 1);
                    animator.SetFloat("velY", -1);
                }
            }
        }

    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "YAxis")
        {
            freezeony = true;
        }
        if (other.gameObject.tag == "XAxis")
        {
            freezeonx = true;
        }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "YAxis")
        {
            freezeony = false;
        }
        if (other.gameObject.tag == "XAxis")
        {
            freezeonx = false;
        }
    }

    public IEnumerator stand()
    {
        hitCounter++;
        yield return new WaitForSecondsRealtime(0.15f);
        gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
    }
}
