using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Singleton;
using UnityEngine.EventSystems;

public class Cannon : Singleton<Cannon> {

    [SerializeField] private Animator animator;
    [SerializeField] private GameObject cannon;
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private Transform bulletStartPosition;
    [SerializeField] private EventSystem eventSystem;

    public void Enable() {
        cannon.SetActive(true);
    }

    public void Update() {
        var worldMousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.x, bulletStartPosition.transform.position.z));
        var direction = worldMousePosition - bulletStartPosition.transform.position;
        direction.Normalize();


        if (Input.GetButtonDown("Fire1") && GameManager.Instance.minigameOne) {
            if (!BlockPointer.Instance.Blocked) {
                Shoot(direction);
                AudioManager.Instance.Play(this, "Fire");
            }
        }
    }

    private void Shoot(Vector3 dir) {
       GameObject obj =  Instantiate(bulletPrefab, bulletStartPosition);

       obj.transform.parent = transform;
       dir = new Vector3(dir.x, dir.y, 0);
       obj.GetComponent<Bullet>().velocityDirection = dir;
    }

    public void Disable() {
        animator.SetTrigger("Disappear");
    }

    IEnumerator Wair() {
        yield return new WaitForSeconds(3f);
        cannon.SetActive(false);
    }
}
