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

public class attackScript : MonoBehaviour {
    public int whichPlayer=1;
    public Transform prefab;
    public float attackOffsetX = 3.0f;
    public float attackOffsetY = 1.4f;
    public Animator animator;
    public float attackCooldown = 5.0f;
    private bool canHit = true;
    public Transform deathSoundPrefab;
    public Transform deathDecoyPrefab;
    public Transform redDeath;
    AudioSource sndPlayer;
    // Use this for initialization
    void Start () {
        sndPlayer = GetComponent<AudioSource>();
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.GetButtonDown("Fire1_P"+whichPlayer) && canHit == true)
        {
            playSound();
            canHit = false;
            Invoke("attackTimer", attackCooldown);
            if (GetComponent<SpriteRenderer>().flipX == true)
            {
                attackOffsetX = 2.0f;
            }
            else
            {
                attackOffsetX = -2.0f;
            }
            var myAttack = Instantiate(prefab, new Vector3(transform.position.x+attackOffsetX, transform.position.y + attackOffsetY, 0), Quaternion.identity);
            myAttack.transform.parent = gameObject.transform;
            var hitResult = Physics2D.BoxCast(new Vector2(myAttack.transform.position.x, myAttack.transform.position.y), new Vector2(2.0f, 3.0f), 0, Vector2.zero, 0.0f, LayerMask.GetMask("Player"));
            var hitNPCResult = Physics2D.BoxCast(new Vector2(myAttack.transform.position.x, myAttack.transform.position.y), new Vector2(2.0f, 3.0f), 0, Vector2.zero, 0.0f, LayerMask.GetMask("NPC"));
            if (hitResult)
            {
                var hitAnimResult = hitResult.transform.GetComponent<Animator>();
                var hitMovementResult = hitResult.transform.GetComponent<PlayerMovement>();
                var hitAttackResult = hitResult.transform.GetComponent<attackScript>();
                if (hitAttackResult.enabled == true)
                {
                    hitAnimResult.SetTrigger("Death");
                    Instantiate(deathSoundPrefab, new Vector3(Random.Range(-28.0f, 28.0f), Random.Range(-16.0f, 16.0f), 0), Quaternion.identity);
                    Instantiate(redDeath, new Vector3(0, 0, 500), Quaternion.identity);
                    Camera.main.GetComponent<cameraShake>().shakeDuration = 0.5f;
                }
                hitMovementResult.enabled = false;
                hitAttackResult.enabled = false;
            }
            if (hitNPCResult)
            {
                var hitAnimResult = hitNPCResult.transform.GetComponent<Animator>();
                var hitMovementResult = hitNPCResult.transform.GetComponent<npcMovement>();
                var hitDeathResult = hitNPCResult.transform.GetComponent<isDead>();
                hitAnimResult.SetTrigger("Death");
                hitMovementResult.enabled = false;
                hitDeathResult.dead = true;
                Instantiate(deathDecoyPrefab, new Vector3(Random.Range(-28.0f, 28.0f), Random.Range(-16.0f, 16.0f), 0), Quaternion.identity);
            }
            animator.SetBool("isAttacking", true);
        }
        else
        {
            animator.SetBool("isAttacking", false);
        }
        if (Input.GetButtonDown("Fire2_P" + whichPlayer) && canHit == true)
        {
            canHit = false;
            Invoke("attackTimer", attackCooldown);
        }
    }

    void attackTimer()
    {
        canHit = true;
    }

    public void playSound()
    {
        sndPlayer.Play();
    }
}
