﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Yodo1.FanCraft
{
    public class FanCraftSDK_Android : FanCraftSDKImpi
    {
        private string TAG = "[FanCraftSDK_Android]";
        private const string CLASS_NAME = "com.yodo1.android.fancraft.unity.UnitySDK";
        private string METHOD_INIT = "init";
        private string METHOD_UPDATE_GAME_USER = "identifyGameUser";
        private string METHOD_SET_TAG_USER = "setTag";
        private string METHOD_INTER_NOTIFICATION_EVENT = "showNotification";
        private string METHOD_INTER_NOTIFICATION_BY_TAG_EVENT = "showNotificationByTag";
        private string METHOD_INTER_NOTIFICATION_BY_LOCATION_EVENT = "showNotificationByLocation";
        private string METHOD_REGISTER_REMOTE_NOTIFICATIONS = "getDeviceToken";

        private AndroidJavaClass _androidCallClass;

        private AndroidJavaClass androidCallClass
        {
            get
            {
                if (_androidCallClass == null)
                {
                    try
                    {
                        _androidCallClass = new AndroidJavaClass(CLASS_NAME);
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarningFormat("Not find : {0}\n{1}", CLASS_NAME, e);
                    }
                }

                return _androidCallClass;
            }
        }

        public override void Init(string appKey, string gameUserSettingsJsonString, string privacySettingsJsonString,
            string pushNotificationSettingsJsonString, string configurationJsonString, string gameObjectName,
            string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("appKey", appKey);
            dic.Add("userSetting", gameUserSettingsJsonString);
            dic.Add("privacySetting", privacySettingsJsonString);
            dic.Add("pushSetting", pushNotificationSettingsJsonString);
            dic.Add("configSetting", configurationJsonString);
            String configJson = JsonUtility.Serialize(dic);
            androidCallClass.CallStatic(METHOD_INIT, configJson, gameObjectName, callbackName);
        }

        public override void UpdateGameUser(string gameUserJsonString, string gameObjectName, string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            androidCallClass.CallStatic(METHOD_UPDATE_GAME_USER, gameUserJsonString, gameObjectName, callbackName);
        }

        public override void SetTags(List<string> tags, string gameObjectName, string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            string gameUserJsonString = JsonUtility.Serialize(tags);
            androidCallClass.CallStatic(METHOD_SET_TAG_USER, gameUserJsonString, gameObjectName, callbackName);
        }

        public override void ShowInterstitialNotification(string gameObjectName, string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            androidCallClass.CallStatic(METHOD_INTER_NOTIFICATION_EVENT, "", gameObjectName, callbackName);
        }

        public override void ShowInterstitialNotificationByLocation(string locationId, string gameObjectName,
            string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            androidCallClass.CallStatic(METHOD_INTER_NOTIFICATION_BY_LOCATION_EVENT, locationId, gameObjectName, callbackName);
        }

        public void ShowInterstitialNotificationByTag(string locationId, string gameObjectName, string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            androidCallClass.CallStatic(METHOD_INTER_NOTIFICATION_BY_TAG_EVENT, locationId, gameObjectName,
                callbackName);
        }

        public override void RegisterForRemoteNotifications(string gameObjectName, string callbackName)
        {
            if (androidCallClass == null)
            {
                Debug.LogWarningFormat("{0} androidCallClass is null", TAG);
                return;
            }

            androidCallClass.CallStatic(METHOD_REGISTER_REMOTE_NOTIFICATIONS, gameObjectName, callbackName);
        }
    }
}