using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class Projectile : MonoBehaviour {

    float damage;
    int OwnerViewID;
    int projectileID;

    void Start() {

    }

    void OnTriggerEnter(Collider collider) {
        GameObject obj = collider.gameObject;

        // figure out if this this projectile belongs to me. If it do then continue
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < players.Length; i++) {
            PhotonView photonView = players[i].GetComponent<PhotonView>();
            if (!photonView || !photonView.IsMine)
                continue;

            if (photonView.ViewID != OwnerViewID) {
                return;
            }
            break;
        }

        if (obj.tag == "Player") {
            PhotonView view = obj.GetComponent<PhotonView>();
            if (!view) {
                return;
            }
            if (view.ViewID == OwnerViewID) {
                return;
            }
            //if (view && !view.IsMine) {
            //    return;
            //}

            PlayerBase playerBase = obj.GetComponent<PlayerBase>();
            PhotonDebugger.IncrementNumberOfMessages("projectile hit");
            playerBase.photonView.RPC("ProjectileHit", RpcTarget.OthersBuffered, projectileID, view.ViewID, OwnerViewID, damage);
            playerBase.ProjectileHit(projectileID, view.ViewID, OwnerViewID, damage);
        }
        else if(obj.tag == "Building") {
            GameObject buildMan = GameObject.FindGameObjectWithTag("Build Manager");
            if(!buildMan) {
                Debug.LogError("No Build Manager Found");
                return;
            }
            Build buildScript = buildMan.GetComponent<Build>();

            // the block health script isn't exactly on this colliders object it's a grandparent so we have to go up the chain to grab it
            // because why would you put the script with the components? that makes no sense so you wouldn't.
            BlockHealth blockHealthScript = obj.transform.parent.parent.gameObject.GetComponent<BlockHealth>();

            buildScript.AddHealthUpdate(blockHealthScript.gridNode.index, damage);
            blockHealthScript.SetHealth(damage);
            Destroy(gameObject);
        }
        else if (obj.tag == "Building") {

        }
        else if (obj.tag == "Projectile") {

        }
        else if (obj.tag == "Wand") {

        }
        else if (obj.name == "Safe Zone") {

        }
        else if (obj.layer == LayerMask.NameToLayer("Ignore Raycast")) {

        }
        else if (obj.layer == LayerMask.NameToLayer("CircleVoid")) {

        }
        else {
            //Debug.Log(obj.name);
			// This needs to be gone for now
            //Destroy(gameObject);
        }
    }

    public void SetDamage(float _damage) {
        damage = _damage;
    }

    public float GetDamage() {
        return damage;
    }

    public void SetProjectileID(int _projectileID) {
        projectileID = _projectileID;
        gameObject.name = projectileID.ToString();
    }

    public void SetOwnerViewID(int _ownerViewID) {
        OwnerViewID = _ownerViewID;
    }

    public static Projectile GetProjectile(int projectileID) {
        GameObject[] projectiles = GameObject.FindGameObjectsWithTag("Projectile");
        for (int i = 0; i < projectiles.Length; i++) {
            if(projectiles[i].name == projectileID.ToString()) {
                return projectiles[i].GetComponent<Projectile>();
            }
        }
        return null;
    }

    public static int GetNextAvailableProjectileID() {
        int actNum = PhotonNetwork.LocalPlayer.ActorNumber;
        if (!PhotonNetwork.IsConnected)
            return 1;
        else {
            GameObject[] projectiles = GameObject.FindGameObjectsWithTag("Projectile");
            for(int i = (((actNum - 1) * 500) + 1); i < (((actNum - 1) * 500) + 500); i++) {
                bool found = false;
                for (int j = 0; j < projectiles.Length; j++) {
                    if(projectiles[j].name == i.ToString()) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    return i;
            }
            Debug.Log("Ran Out of Projectile IDs for this player");
            return -1;
        }
    }
}
