using System;
using UnityEngine;
using UnityEngine.Scripting;

namespace TyphoonUnitySDK
{
    [Preserve]
    public class HuaweiHarmonySdk : BaseSdk
    {
        #region 回调数据

        private class BaseResultData
        {
            public string state;
            public string err;
            public long code;

            public bool IsOk()
            {
                return state == "ok";
            }
        }

        private class InitSdkResultData : BaseResultData
        {
        }

        private class LoginResultData : BaseResultData
        {
        }

        private class ShowVideoResultData : BaseResultData
        {
        }

        #endregion

#if TUANJIE_1_6_OR_NEWER
        private OpenHarmonyJSObject  HarmonyBridge { get; set; }

        private HuaweiHarmonyCallback HuaweiHarmonyCallback { get; set; }

        public override void OnCreate()
        {
            SdkDebug.Log("huawei harmony on creating");
            HarmonyBridge = new OpenHarmonyJSObject ("HarmonyBridge");
            HuaweiHarmonyCallback = HuaweiHarmonyCallback.CreateInstance();
            HuaweiHarmonyCallback.Initialize(OnReceiveFromHarmony);
            SdkDebug.Log("huawei harmony on created");
        }

        public override void InitSdk(Action success, Action<string> fail)
        {
            SdkDebug.Log("init sdk...");
            var item = new SessionItem("initSdk", null, (res) =>
            {
                SdkDebug.Log($"init sdk result = {res}");
                var result = res.ToXObject<InitSdkResultData>();
                if (result.IsOk())
                {
                    success?.Invoke();
                }
                else
                {
                    fail?.Invoke($"init sdk failed , code = {result.code}");
                }
            });
            CallWithSession(item);
        }

        public override void Login(Action success, Action<string> fail)
        {
            SdkDebug.Log("login...");
            var item = new SessionItem("login", null, (res) =>
            {
                SdkDebug.Log($"login result = {res}");
                var result = res.ToXObject<LoginResultData>();
                if (result.IsOk())
                {
                    success?.Invoke();
                }
                else
                {
                    fail?.Invoke($"login failed , code = {result.code}");
                }
            });
            CallWithSession(item);
        }

        public override bool GetVideoFlag()
        {
            SdkDebug.Log("get video flag = true");
            return true;
        }

        public override void ShowVideo(string scene, Action success, Action<string> fail)
        {
            SdkDebug.Log($"show video... scene = {scene} invoke success");
            var item = new SessionItem("showVideo", new { scene }, (res) =>
            {
                SdkDebug.Log($"show video result = {res}");
                var result = res.ToXObject<ShowVideoResultData>();
                if (result.IsOk())
                {
                    success?.Invoke();
                }
                else
                {
                    fail?.Invoke($"show video failed , code = {result.code}");
                }
            });
            CallWithSession(item);
        }

        public override void ShowInters(string scene)
        {
            SdkDebug.Log($"show inters... scene = {scene}");
            CallFromUnity("showInters", new { scene });
        }

        public override void ShowBanner(string scene, BannerPosition position)
        {
            SdkDebug.Log($"show banner... scene = {scene} position = {position}");
            CallFromUnity("showBanner", new { scene, pos = (int)position });
        }

        public override void HideBanner(string scene = "")
        {
            SdkDebug.Log($"hide banner... scene = {scene}");
            CallFromUnity("hideBanner", new { scene });
        }

        private void OnReceiveFromHarmony(string message)
        {
            SdkDebug.Log($"OnReceiveFromHarmony message = {message}");
        }

        private void CallFromUnity(string method, object data)
        {
            var body = new
            {
                method,
                data
            };
            var json = body.ToXJson();
            HarmonyBridge.Call("callFromUnity", json);
        }

        private string CallFromUnityWithReturn(string method, object data)
        {
            var body = new
            {
                method,
                data
            };
            var json = body.ToXJson();
            return HarmonyBridge.Call<string>("callFromUnityWithReturn", json);
        }

        public void CallWithSession(SessionItem session)
        {
            SdkDebug.Log($"CallWithSession {session.MethodName} {session.SendData}");
            //避免为空
            var body = new
            {
                method = session.MethodName,
                sessionId = session.GetSession(),
                data = session.SendData,
            };
            HarmonyBridge.Call("callFromUnity", body.ToXJson());
        }


#else
        public override void OnCreate()
        {
            SdkDebug.Log("huawei harmony on created");
        }

        public override void InitSdk(Action success, Action<string> fail)
        {
            SdkDebug.Log("init... success");
            success?.Invoke();
        }

        public override void Login(Action success, Action<string> fail)
        {
            SdkDebug.Log("login... success");
            success?.Invoke();
        }
#endif
    }
}