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

public class FakeItemBoxScript : MonoBehaviourPunCallbacks, ISpawnable
{
    [Header("Settings")]
    [SerializeField] [Range(0, 1)] float m_timeToActivate = 0.5f;
    [SerializeField] bool m_mineActive = false;

    // READABLES //
    #region
    public RacerCore Owner { get; private set; }
    public bool CanTargetOwner { get; private set; }
    #endregion
    
    // DEFAULT METHODS //
    #region
    void Start()
    {
        m_mineActive = false;

        RaycastHit _hit;

        if (Physics.Raycast(transform.position, -transform.up, out _hit))
        {
            transform.position = _hit.point + Vector3.up * 1.5f;
        }

        transform.rotation = Quaternion.Euler(0, -35, -45);

		StartCoroutine (DelayActivation());
	}
	void Update()
    {
		transform.RotateAround (transform.position, Vector3.right, 30f * Time.deltaTime);
        transform.GetChild(0).LookAt(2 * transform.GetChild(0).position - Camera.main.transform.position, Vector3.down);
	}
	void OnTriggerEnter (Collider col)
    {
        // If the mine is not active yet, return
        if (!m_mineActive)
            return;

        // Get a racer core from the object
        RacerCore _rc = col.GetComponent<RacerCore>();

        // If we didn't one, return
        if (!_rc)
            return;

        // If this is a network instance, return
        if (!_rc.IsLocal)
            return;

        // If we have an owner, we hit our owner, and we are unable to target our owner, return
        if (Owner != null && _rc == Owner && !CanTargetOwner)
            return;

        // Hit the player
        _rc.Motor.Hit();

        Byebye();
    }
    #endregion

    // EXECUTABLE METHODS //
    #region
    public void SetOwnerID(int _id)
    {
        Owner = RacerCore.GetRacerByID(_id);

        if (PhotonNetwork.IsConnected && photonView && photonView.IsMine)
            photonView.RPC("SetOwnerIDRemote", RpcTarget.Others, _id);
    }
    public void SetCanTargetOwner(bool _can)
    {
        CanTargetOwner = _can;

        if (PhotonNetwork.IsConnected && photonView && photonView.IsMine)
            photonView.RPC("SetCanTargetOwnerRemote", RpcTarget.Others, _can);
    }
    void Byebye()
    {
        if (PhotonNetwork.IsConnected && photonView)
        {
            if (photonView.IsMine)
                PhotonNetwork.Destroy(gameObject);
            else
                photonView.RPC("ByebyeRemote", RpcTarget.Others);
        }
        else
            Destroy(gameObject);
    }
    #endregion

    // COROUTINES //
    #region
    IEnumerator DelayActivation()
    {
		yield return new WaitForSeconds (0.5f);
		m_mineActive = true;
	}
    #endregion

    // PHOTON / RPC //
    #region
    [PunRPC]
    void ByebyeRemote()
    {
        if (photonView.IsMine)
            PhotonNetwork.Destroy(gameObject);
    }
    [PunRPC]
    void SetOwnerIDRemote(int _id)
    {
        Owner = RacerCore.GetRacerByID(_id);
    }
    [PunRPC]
    void SetCanTargetOwnerRemote(bool _can)
    {
        CanTargetOwner = _can;
    }
    #endregion
}
