using System.Collections;
using UnityEngine;

[CreateAssetMenu(fileName = "Ammo", menuName = "Ammo/Projectile: Laser", order = 10)]
public class ProjectileAmmoLaser : ProjectileAmmo
{
    public GameObject ImpactCharacterEffect;
    public GameObject ImpactEffect;
    public LayerMask CollidesWith;
    public LineRenderer Trail;
    public float Range = 15.0f;

    public float ExplosionForce = 20.0f;
    public float ExplosionRadius = 100.0f;
    public int DestructionPixelRadius = 5;

    public override void OnStart(WeaponHolder owner, GameObject projectile)
    {
        var trail = (LineRenderer) Instantiate(Trail, owner.ShootTransform.position, owner.ShootTransform.rotation);
        trail.SetPosition(0, trail.transform.position);

        Pix.RaycastHit hit;
        if (Pix.Physics2D.Raycast(new Ray(trail.transform.position, owner.ShootTransform.forward), out hit, Range, CollidesWith))
        {
            if (hit.Collider.sharedMaterial && hit.Collider.sharedMaterial.name == "Player")
            {
                PlayerController killer = owner.GetComponent<PlayerController>();
                hit.Collider.GetComponent<PlayerHealth>().TakeDamage(10.0f, killer);
                Instantiate(ImpactCharacterEffect, hit.Point, Quaternion.identity);

                int sound = Random.Range(1,4);
                switch (sound) {
                case 1:
					AudioManager.Play(AudioManager.Sounds.HUMAN_HIT_1);
					break;
				case 2:
					AudioManager.Play(AudioManager.Sounds.HUMAN_HIT_2);
					break;
				case 3:
					AudioManager.Play(AudioManager.Sounds.HUMAN_HIT_3);
					break;
                }



            }
            else
            {
                Instantiate(ImpactEffect, hit.Point, Quaternion.identity);

				int sound = Random.Range(1,4);
                switch (sound) {
                case 1:
					AudioManager.Play(AudioManager.Sounds.IMPACT_DAMAGE_1);
					break;
				case 2:
					AudioManager.Play(AudioManager.Sounds.IMPACT_DAMAGE_2);
					break;
				case 3:
					AudioManager.Play(AudioManager.Sounds.IMPACT_DAMAGE_3);
					break;
                }
            }

            trail.SetPosition(1, hit.Point);
        }
        else
        {
            trail.SetPosition(1, trail.transform.position + Range * owner.ShootTransform.forward);

			int sound = Random.Range(1,4);
                switch (sound) {
                case 1:
				AudioManager.Play(AudioManager.Sounds.LASER_TYPE_1);
					break;
				case 2:
				AudioManager.Play(AudioManager.Sounds.LASER_TYPE_2);
					break;
				case 3:
				AudioManager.Play(AudioManager.Sounds.LASER_TYPE_3);
					break;
                }
        }
    }
}
