using Scenes.scripts.utils;
using UnityEngine;

//接收方
public class TextureHelper
{
    private static string UXR_TEXTURE_Reviever = "Receiver.";
    private static AndroidJavaObject textureHolder;

    public static void Create()
    {
        Debug.Log("TextureHelper Create");

        //创建接收端
        new AndroidJavaObject("com.rokid.mrcplugin.codingtransmission.textureholder.TextureHolder");
    }

    public static void Start(string ip,int unityTextureId, int width, int height, int frameRate = 60, int bitRate = 1024)
    {
        Debug.Log($"TextureHelper.Start IP - {ip} TexID - {unityTextureId} Width - {width} Height - {height} FrameRate - {frameRate} BitRate - {bitRate}");

        CallBridge.callAndroid(Request.Build()
            .Name(UXR_TEXTURE_Reviever + "start")
            .Param("ip", ip)
            .Param("unityTextureId", unityTextureId)
            .Param("width", width)
            .Param("height", height)
            .Param("frameRate", frameRate)
            .Param("bitRate", bitRate * 1024));

        textureHolder = CallBridge.callAndroid(Request.Build()
            .Name(UXR_TEXTURE_Reviever + "getInstance"));
    }

    public static void _updateTexture()
    {
        //Debug.Log("TextureHelper _updateTexture");
        textureHolder.Call("updateTexture");
    }

    public static bool _isFrameUpdated()
    {
        if(textureHolder == null)
        {
            return false;
        }

        return textureHolder.Call<bool>("isUpdateFrame");
    }

    public static void Release()
    {
        Debug.Log("TextureHelper.Release");

        CallBridge.callAndroid(Request.Build()
           .Name(UXR_TEXTURE_Reviever + "release"));

        //textureHolder.Call("release");
    }

    //public static void initTCP()
    //{
    //    Debug.Log("TextureHelper initTCP");
    //    CallBridge.callAndroid(Request.Build()
    //           .Name(UXR_TEXTURE_Reviever + "initTCP"));
    //}

    public static void _setTextureModeListener(string callObj, string method)
    {
        CallBridge.callAndroid(Request.Build()
            .Name(UXR_TEXTURE_Reviever + "setTextureModeListener")
            .AndroidCallback(CallBridge.createCallback(callObj, method)));
    }
}