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

public class Gun : MonoBehaviour {

    [SerializeField]
    AbilityType type;

    [SerializeField]
    GameObject firePoint;

    [SerializeField]
    float fireRate = 1;

    [SerializeField]
    float timeToFire = 0;

    [SerializeField]
    GameObject electricPrefab;

    [SerializeField]
    GameObject soundPrefab;

    [SerializeField]
    GameObject lightPrefab;

    public Sprite CurrentSprite { get; set; }

    MusicManager mm;

    public AbilityType Type {
        get {
            return type;
        }

        set {
            type = value;
        }
    }

    SpriteRenderer sr;

    void Awake() {
        mm = MusicManager.Instance;
        sr = GetComponent<SpriteRenderer>();
    }

    void Update() {
        if(fireRate == 0) {
            if(Input.GetMouseButton(0)) {
                Shoot();
            }
        } else {
            if(Input.GetMouseButton(0) && Time.time > timeToFire) {
                timeToFire = Time.time + 1.0f / fireRate;
                Shoot();
            }
        }
    }

    public void ChangeSprite() {
        sr.sprite = CurrentSprite;
    }

    void Shoot() {
        // Bullet bullet = new Bullet(type);

        //   Debug.Log("shoot");
        var shot = Sounds.LightShot;
        var pref = lightPrefab;

        if(type.Equals(AbilityType.Electricity)) {
            pref = electricPrefab;
            shot = Sounds.ElectricityShot;
        } else if(type.Equals(AbilityType.Sound)) {
            pref = soundPrefab;
            shot = Sounds.SoundGun;
        }

        mm.PlayOneShot(mm.MapAudio(shot));
        GameObject bul =  Instantiate( pref, firePoint.transform.position, Quaternion.identity);
        Bullet b = bul.AddComponent<Bullet>();
        b.Type = type;
        bul.layer = 10;

    }



}
