using UnityEngine;

namespace ScrewsMaster
{
    public class AndroidKit
    {

        public const string AndroidClassName = "com.unity3d.player.UnityPlayer";

        private static string className;
        private static AndroidJavaClass androidClass;
        private static AndroidJavaObject androidObject;

        public static void InitAndroidObject()
        {
            className = AndroidClassName;


            if (Application.platform == RuntimePlatform.Android)
            {
                androidClass = new AndroidJavaClass(className);
                string sdkClassName = "com.unity3d.player.UnityPlayer";
                if (className.Contains(sdkClassName))
                {
                    androidObject = androidClass.GetStatic<AndroidJavaObject>("currentActivity");
                }
                else
                {
                    androidObject = androidClass.GetStatic<AndroidJavaObject>("Instance");
                    if (androidObject != null)
                    {
                        androidObject.Call("init");
                    }
                }
            }
        }

        public static void CallVoidApi(string apiFunc, params object[] args)
        {
            try
            {
#if !UNITY_EDITOR && UNITY_ANDROID
                androidObject.Call(apiFunc, args);
#endif
            }
            catch (System.Exception)
            {
            }
        }

        public static T CallReturnApi<T>(string apiFunc, params object[] args)
        {
            try
            {
                return androidObject.Call<T>(apiFunc, args);
            }
            catch (System.Exception)
            {
                return default;
            }
        }

        public static void webViewInit(int left, int top, int right, int bottom)
        {
            CallVoidApi("InitWebView", left, top, right, bottom);
        }


        protected static bool m_FirstLoadUrl = true;


        public static void webViewLoadUrl(string url)
        {
            if (m_FirstLoadUrl)
            {
                webViewLoadUrl(int.MaxValue, url);
                m_FirstLoadUrl = false;
                return;
            }

            webViewRefreshUrl(int.MaxValue, url, true);
        }


        public static void webViewLoadUrl(int _index, string url)
        {
            CallVoidApi("LoadWebView", url);
        }


        public static void webViewRefreshUrl(int _index, string url, bool isTop)
        {
            CallVoidApi("LoadWebView", url);
        }


        public static void webViewSetVisibility(int visibility)
        {
            CallVoidApi("SetWebViewShow", visibility);
        }


        public static void webViewSetLayerTop(int _index)
        {
        }


        public static void webViewSetEvent(bool needEvent)
        {
            CallVoidApi("SetWebViewClickable", needEvent);
        }


        public static void webViewShieldEvent(int placementId, int width, int height, int left, int top)
        {
            CallVoidApi("SetWebViewShield", placementId, width, height, left, top);
        }

    }
}

