using UnityEngine;
using System;
using System.Reflection;

namespace MRStore.Util
{
    /// <summary>
    /// 通用型接口
    /// </summary>
    public interface JavaInterface
    {
        /// <summary>
        /// 无参函数
        /// </summary>
        /// <param name="method"></param>
        void Call(string method);
        /// <summary>
        /// 有参函数
        /// </summary>
        /// <param name="method"></param>
        /// <param name="args"></param>
        void Call(string method, string args);
    }
    /// <summary>
    /// 和安卓Java进行交互的方式
    /// Java端调用C#代码的方案
    /// Java声明接口，由Unity实现相关的代码
    /// 重点是，如何设计一个通用的类来实现相关的功能？
    /// 
    /// </summary>
    public abstract class JavaProxy<T> : AndroidJavaProxy, JavaInterface where T : JavaProxy<T>
    {
        /// <summary>
        /// com.shadowcreator.mediasouptool.JavaInterface
        /// </summary>
        /// <param name="packName"></param>
        public JavaProxy(string packName) : base(packName) { }

        public void Call(string method)
        {
            MethodInfo methodInfo = typeof(T).GetMethod(method);
            if (methodInfo == null) throw new Exception(typeof(T) + method + "method is null");
            methodInfo.Invoke(this, null);
        }

        public void Call(string method, string args)
        {
            MethodInfo methodInfo = typeof(T).GetMethod(method);
            if (methodInfo == null) throw new Exception(typeof(T) + method + "method is null");
            methodInfo.Invoke(this, new System.Object[] { args });
        }
    }
}