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

public class BulletBehaviour : MonoBehaviour
{
    private const string PLAYER = "Player";
    private const string ENEMY = "Enemy";
    [SerializeField] private float bulletSpeed = 10f;
    [SerializeField] private float maxDistance = 50f;

    public Ownership owner;
    private Vector3 startingPosition;
    private HealthDelta lethality;

    void Awake() {
        this.owner = Ownership.NONE;
        this.startingPosition = this.transform.position;
        this.lethality = GetComponent<HealthDelta>();
    }
    void FixedUpdate() {
        float bulletTravel = Time.deltaTime * bulletSpeed;
        this.transform.position = Vector3.MoveTowards(this.transform.position, this.transform.position + this.transform.forward, bulletTravel);
    }

    void Update() {
        if(this.owner == Ownership.PLAYER)
            this.lethality.affects = LayerMask.GetMask(ENEMY);
        else if(this.owner == Ownership.ENEMY)
            this.lethality.affects = LayerMask.GetMask(PLAYER);
        if(Vector3.Distance(this.transform.position, this.startingPosition) > maxDistance) {
            Expire();
        }
    }

    public void Expire() {
        //Do Something
        Destroy(this.gameObject);
    }

    void OnCollisionEnter(Collision other) {
        //If the bullet collides with a shield...
        // if(LayerMask.NameToLayer("Shield") == other.gameObject.layer) {
        //     //Take shield plate reference...
        //     ShielderPlate plate = other.gameObject.GetComponent<ShielderPlate>();
        //     //If the owner of shield plate is on different side with owner of this bullet...
        //     if(this.owner != plate.GetOwnership()) {
        //         this.owner = plate.GetOwnership();//Change allegiance of this bullet...
        //         this.transform.forward = Vector3.Reflect(this.transform.forward, other.GetContact(0).normal);//And deflect bullet.
        //     }
        //     else
        //         Physics.IgnoreCollision(other.collider, this.GetComponent<Collider>());//Otherwise, ignore this shield.
        // }
        // else {//Otherwise...
            //If owner of this bullet is allied with ENEMY...
            if(this.owner == Ownership.ENEMY) {
                if(LayerMask.NameToLayer("Player") == other.gameObject.layer) {// and it hits the player...
                    //Damage Player
                    Expire();//Destroy this bullet.
                }
                else if(other.gameObject.layer == LayerMask.NameToLayer("Environment")) {//If it hits neither PLAYER, ENEMY, nor shield...
                    Expire();//Destroy this bullet.
                }
                else//Otherwise...
                    Physics.IgnoreCollision(other.collider, this.GetComponent<Collider>());
            }
            else if(this.owner == Ownership.PLAYER) {//If the owner of this bullet is PLAYER
                if(LayerMask.NameToLayer("Enemy") == other.gameObject.layer) {// and it hits an ENEMY...
                    //Damage Enemy
                    Expire();//Destroy this bullet.
                }
                else if(other.gameObject.layer == LayerMask.NameToLayer("Environment")) {//If it hits neither PLAYER, ENEMY, nor shield...
                    Expire();//Destroy this bullet.
                }
                else//Otherwise...
                    Physics.IgnoreCollision(other.collider, this.GetComponent<Collider>());
            }
        // }
    }
}
