#if USING_NETCODE_GO


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections;
using Unity.Netcode;
using SimplifyXR;

public class ShareClientNetworkVariableBehaviour : NetworkVariableBehaviour
{
    public GameObject ObjectToShareNetworkVariable;

    /// <summary>
    /// If Network Variable will be bool, int, float, or FixedString128
    /// </summary>
    public SimplifyXREnums.NetworkVariableTypes NetworkVariableType;

    /// <summary>
    /// The bool to share across the network
    /// </summary>
#if UNITY_EDITOR
    [Tooltip("The bool to share across the network."), Conditional("NetworkVariableType",
        SimplifyXREnums.NetworkVariableTypes.Bool, ComparisonType.Equals),
        Required("A Bool must be selected")]
#endif
    public NetworkVariable<bool> _bool = new NetworkVariable<bool>
    (
        false,
        NetworkVariableReadPermission.Everyone,
        NetworkVariableWritePermission.Owner
    );

    /// <summary>
    /// The int to share across the network
    /// </summary>
#if UNITY_EDITOR
    [Tooltip("The int to share across the network."), Conditional("NetworkVariableType",
        SimplifyXREnums.NetworkVariableTypes.Int, ComparisonType.Equals),
        Required("An Int must be selected")]
#endif
    public NetworkVariable<Int32> _int32 = new NetworkVariable<Int32>
    (
        0,
        NetworkVariableReadPermission.Everyone,
        NetworkVariableWritePermission.Owner
    );

    /// <summary>
    /// The float to share across the network
    /// </summary>
#if UNITY_EDITOR
    [Tooltip("The float to share across the network."), Conditional("NetworkVariableType",
        SimplifyXREnums.NetworkVariableTypes.Float, ComparisonType.Equals),
        Required("A float must be selected")]
#endif
    public NetworkVariable<float> _float = new NetworkVariable<float>
    (
        0,
        NetworkVariableReadPermission.Everyone,
        NetworkVariableWritePermission.Owner
    );

    /// <summary>
    /// The FixedString128 to share across the network
    /// </summary>
#if UNITY_EDITOR
    [Tooltip("The FixedString128 to share across the network."), Conditional("NetworkVariableType",
        SimplifyXREnums.NetworkVariableTypes.FixedString128, ComparisonType.Equals),
        Required("An FixedString128 must be selected")]
#endif
    public NetworkVariable<FixedString128Bytes> _string = new NetworkVariable<FixedString128Bytes>
    (
        "",
        NetworkVariableReadPermission.Everyone,
        NetworkVariableWritePermission.Owner
    );

    public NetworkObject _networkObject;

    public override void OnNetworkSpawn()
    {
        _bool.OnValueChanged += (bool previousValue, bool newValue) =>
        {
            _bool.Value = newValue;
            Invoke("UpdateBoolTextClientRpc", 0.1f);
        };

        _int32.OnValueChanged += (Int32 previousValue, Int32 newValue) =>
        {
            _int32.Value = newValue;
            Invoke("UpdateIntTextClientRpc", 0.1f);
        };

        _float.OnValueChanged += (float previousValue, float newValue) =>
        {
            _float.Value = newValue;
            Invoke("UpdateFloatTextClientRpc", 0.1f);
        };

        _string.OnValueChanged += (FixedString128Bytes previousValue, FixedString128Bytes newValue) =>
        {
            _string.Value = newValue;
            Invoke("UpdateStringTextClientRpc", 0.1f);
        };

        _networkObject = this.GetComponent<NetworkObject>() != null ?
            this.GetComponent<NetworkObject>() : this.gameObject.AddComponent<NetworkObject>();

        base.OnNetworkSpawn();
    }

    private new void TextObjectFinder(String text)
    {
        if (_textField != null)
        {
            _textField.text = text;
        }
#if USING_TMP
        else if (_textField_TMPUGUI != null)
        {
            _textField_TMPUGUI.text = text;
        }
        else if (_textField_TMP != null)
        {
            _textField_TMP.text = text;
        }
#endif
    }

    public override void UpdateBoolValue()
    {
        var Bool = _bool.Value;
        Debug.Log($"1 UpdateBoolValue : Bool = {Bool}");

        Bool = !Bool;
        Debug.Log($"2 UpdateBoolValue : Bool = {Bool}");

        _bool.Value = Bool;

        //RequestBoolValueServerRpc(_networkObject, Bool);
    }

    public override void UpdateBoolValue(bool Bool)
    {
        Debug.Log($"UpdateBoolValue : Bool = {Bool}");

        _bool.Value = Bool;

        //RequestBoolValueServerRpc(_networkObject, Bool);
    }

    public override void UpdateIntValue(int Int)
    {
        Debug.Log($"UpdateIntValue : Int = {Int}");

        _int32.Value = Int;

        //RequestIntValueServerRpc(_networkObject, Int);
    }

    public override void UpdateIntValue(int Int, bool Bool)
    {
        Debug.Log($"UpdateIntValue : Int = {Int}, Bool = {Bool}");

        if (Bool)
            _int32.Value += Int;
        else
            _int32.Value -= Int;

        //RequestIntValueServerRpc(_networkObject, Int, Bool);
    }

    public override void UpdateFloatValue(float Float)
    {
        Debug.Log($"UpdateFloatValue : Float = {Float}");

        _float.Value = Float;

        //RequestFloatValueServerRpc(_networkObject, Float);
    }

    public override void UpdateFloatValue(float Float, bool Bool)
    {
        Debug.Log($"UpdateFloatValue : Float = {Float}, Bool = {Bool}");

        if (Bool)
            _float.Value += Float;
        else
            _float.Value -= Float;

        //RequestFloatValueServerRpc(_networkObject, Float, Bool);
    }

    public override void UpdateFixedString128Value(FixedString128Bytes FixedString128)
    {
        Debug.Log($"UpdateFixedString128Value : FixedString128 = {FixedString128}");

        _string.Value = FixedString128;

        //RequestFixedString128ValueServerRpc(_networkObject, FixedString128);
    }

    [ClientRpc]
    public override void UpdateBoolTextClientRpc()
    {
        var text = $"{_bool.Value} : {OwnerClientId}";
        TextObjectFinder(text);
        ShareNetworkBehaviourMessageServerRpc($"Bool: {text}");
    }

    [ClientRpc]
    public override void UpdateIntTextClientRpc()
    {
        var text = $"{_int32.Value} : {OwnerClientId}";
        TextObjectFinder(text);
        ShareNetworkBehaviourMessageServerRpc($"Int: {text}");
    }

    [ClientRpc]
    public override void UpdateFloatTextClientRpc()
    {
        var text = $"{_float.Value} : {OwnerClientId}";
        TextObjectFinder(text);
        ShareNetworkBehaviourMessageServerRpc($"Float: {text}");
    }

    [ClientRpc]
    public override void UpdateStringTextClientRpc()
    {
        var text = $"{_string.Value} : {OwnerClientId}";
        TextObjectFinder(text);
        ShareNetworkBehaviourMessageServerRpc($"String: {text}");
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestBoolValueServerRpc(NetworkObjectReference networkObjectReference, bool Bool)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestIntValueServerRpc : networkObject = {networkObject}, Int = {Bool}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            networkVariableBehaviour._bool.Value = Bool;

            Debug.Log($"RequestBoolValueServerRpc networkVariableBehaviour._bool.Value: {networkVariableBehaviour._bool.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestIntValueServerRpc(NetworkObjectReference networkObjectReference, Int32 Int)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestIntValueServerRpc : networkObject = {networkObject}, Int = {Int}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            networkVariableBehaviour._int32.Value = Int;

            Debug.Log($"RequestIntValueServerRpc networkVariableBehaviour._int32.Value: {networkVariableBehaviour._int32.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestIntValueServerRpc(NetworkObjectReference networkObjectReference, Int32 Int, bool Bool)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestIntValueServerRpc : networkObject = {networkObject}, Int = {Int}, Bool = {Bool}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            if (Bool)
                networkVariableBehaviour._int32.Value += Int;
            else
                networkVariableBehaviour._int32.Value -= Int;

            Debug.Log($"RequestIntValueServerRpc networkVariableBehaviour._int32.Value: {networkVariableBehaviour._int32.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestFloatValueServerRpc(NetworkObjectReference networkObjectReference, float Float)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestFloatValueServerRpc : networkObject = {networkObject}, Float = {Float}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            networkVariableBehaviour._float.Value = Float;

            Debug.Log($"RequestFloatValueServerRpc networkVariableBehaviour._float.Value: {networkVariableBehaviour._float.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestFloatValueServerRpc(NetworkObjectReference networkObjectReference, float Float, bool Bool)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestFloatValueServerRpc : networkObject = {networkObject}, Int = {Float}, Bool = {Bool}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            if (Bool)
                networkVariableBehaviour._float.Value += Float;
            else
                networkVariableBehaviour._float.Value -= Float;

            Debug.Log($"RequestFloatValueServerRpc networkVariableBehaviour._float.Value: {networkVariableBehaviour._float.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void RequestFixedString128ValueServerRpc(NetworkObjectReference networkObjectReference, FixedString128Bytes FixedString)
    {
        /*if (networkObjectReference.TryGet(out NetworkObject networkObject))
        {
            Debug.Log($"RequestFixedString128ValueServerRpc : networkObject = {networkObject}, Float = {FixedString}");
            var networkVariableBehaviour = networkObject.gameObject.GetComponent<ShareServerNetworkVariableBehaviour>();

            networkVariableBehaviour._string.Value = FixedString;

            Debug.Log($"RequestFixedString128ValueServerRpc networkVariableBehaviour._string.Value: {networkVariableBehaviour._string.Value}");
        }*/
    }

    [ServerRpc(RequireOwnership = false)]
    public override void ShareNetworkBehaviourMessageServerRpc(string message)
    {
        Debug.Log($"ShareNetworkBehaviourMessageServerRpc {OwnerClientId}" + "; " + message);
    }
}


#endif