﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yodo1.FanCraft;

public class FanCraftDemo : MonoBehaviour
{
    public Text initStautsText;
    public Text uidText;

    public InputField setTagsInputField;
    public InputField showLocationInputField;

    // Start is called before the first frame update
    void Start()
    {
#if UNITY_EDITOR
        InitFanCraft(string.Empty);
#else
        RegisterForRemoteNotifications();
#endif
    }

    // Update is called once per frame
    void Update()
    {
    }

    void RegisterForRemoteNotifications()
    {
        /// You must enable Push Notifications for FanCraft in the Editor if you use this method to get a device token. Otherwise, call the initialization method directly 
        FanCraftSDK.RegisterForRemoteNotifications((string deviceToken, FanCraftError error) =>
        {
            InitFanCraft(deviceToken);
        });
    }


    void InitFanCraft(string deviceToken)
    {
        PrivacySettings privacySettings = new PrivacySettings();
        privacySettings.CoppaAgeRestrictedUser = false;
        privacySettings.CcpaDoNotSell = false;
        privacySettings.GdprConsent = true;

        GameUserInfo gameUserInfo = new GameUserInfo();
        gameUserInfo.UserId = "your_game_user_id";
        gameUserInfo.NickName = "your_game_user_name";
        gameUserInfo.UserGender = GameUserInfo.Gender.Male;
        gameUserInfo.Age = 10;

        GameUserSettings gameUserSettings = new GameUserSettings();
        gameUserSettings.Enable = true;
        gameUserSettings.UserInfo = gameUserInfo;

        PushNotificationSettings pushNotificationSettings = new PushNotificationSettings();
        if (!string.IsNullOrEmpty(deviceToken))
        {
            pushNotificationSettings.Enable = true;
            pushNotificationSettings.DeviceToken = deviceToken;
        }
        else
        {
            pushNotificationSettings.Enable = false;
        }

        SDKConfiguration sdkConfiguration = SDKConfiguration.defaultConfig();
        sdkConfiguration.InterNotificationTheme.OverlayColor = new Color(22 / 255f, 37 / 255f, 67 / 255f, 205 / 255f);

        sdkConfiguration.InterNotificationTheme.PopupBackgroundColor = new Color(22f / 255f, 37f / 255f, 67f / 255f, 230f / 255f);

        sdkConfiguration.InterNotificationTheme.TextColor = Color.white;

        //sdkConfiguration.InterNotificationTheme.TitleColor = Color.red;
        //sdkConfiguration.InterNotificationTheme.TitleFontSize = 20;

        //sdkConfiguration.InterNotificationTheme.DescriptionFontSize = 16;

        sdkConfiguration.InterNotificationTheme.CtaBackgroundColor = Color.white;
        sdkConfiguration.InterNotificationTheme.CtaTextColor = new Color(38f / 255f, 64f / 255f, 114f / 255f, 255f / 255f);
        //sdkConfiguration.InterNotificationTheme.CtaFontSize = 18;

        sdkConfiguration.InterNotificationTheme.PollOptionBackgroundColor = new Color(96f / 255f, 112f / 255f, 142f / 255f, 255f / 255f);
        //sdkConfiguration.InterNotificationTheme.PollOptionTextColor = Color.gray;

        FanCraftSDK.Init("123", gameUserSettings, privacySettings, pushNotificationSettings, sdkConfiguration,
            (SDKInitSuccessInfo info, FanCraftError error) =>
            {
                Debug.Log(FanCraftSDK.TAG + "Init delegate.");
                if (initStautsText != null)
                {
                    initStautsText.text = string.Format("Init Status: {0}", (error == null) ? "Success" : "Failure");
                }

                if (error != null)
                {
                    // Initialization failed
                }
                else
                {
                    // Initialization success
                    FanCraftUserInfo userInfo = info.GetUserInfo();
                    if (uidText != null && userInfo != null)
                    {
                        uidText.text = "FanCraft UID: " + userInfo.Uid;
                    }
                    Debug.Log(FanCraftSDK.TAG + string.Format("UserInfo - Uid:{0}, IsAnonymous:{1}, DeviceTokenSet:{2}, GameUserId:{3}",
                                  userInfo.Uid, userInfo.IsAnonymous, userInfo.DeviceTokenSet, userInfo.GameUserId));
                }
            });
    }

    public void SetTags()
    {
        if (setTagsInputField == null || string.IsNullOrEmpty(setTagsInputField.text))
        {
            return;
        }

        string tagString = setTagsInputField.text;
        if (tagString.Contains(","))
        {
            //Multiple tags
            string[] tagsArray = tagString.Split(',');
            FanCraftSDK.SetTags(tagsArray, (bool status, FanCraftError error) =>
            {
                Debug.Log(FanCraftSDK.TAG + string.Format("SetTags - status:{0}", status));
                if (error != null)
                {
                    Debug.Log(FanCraftSDK.TAG + string.Format("SetTags - error({0}, {1})", error.Code, error.Message));
                }
            });
        }
        else
        {
            FanCraftSDK.SetTag(tagString, (bool status, FanCraftError error) =>
            {
                Debug.Log(FanCraftSDK.TAG + string.Format("SetTags - status:{0}", status));
                if (error != null)
                {
                    Debug.Log(FanCraftSDK.TAG + string.Format("SetTags - error({0}, {1})", error.Code, error.Message));
                }
            });
        }
    }

    public void ShowInterstitialNotification()
    {
        FanCraftSDK.ShowInterstitialNotification(HandleInterstitialNotification);
    }

    public void ShowInterstitialNotificationByLocation()
    {
        if (showLocationInputField == null || string.IsNullOrEmpty(showLocationInputField.text))
        {
            return;
        }
        string locationString = showLocationInputField.text;
        FanCraftSDK.ShowInterstitialNotificationByLocation(locationString, HandleInterstitialNotification);
    }

    public void HandleInterstitialNotification(string notificationId, FanCraftError error, InterstialNotificationHandler handler)
    {
        Debug.Log(FanCraftSDK.TAG + "ShowInterstitialNotification delegate.");
        if (error != null)
        {
            // Failed to display the interstitial notification
            return;
        }
        else
        {
            // The interstitial notification will be displayed

            FanCraftSDK.InterstialNotificationEvent interstialNotificationEvent = handler.GetNotificationEvent();

            Debug.Log(FanCraftSDK.TAG + "ShowInterstitialNotification InterstialNotificationEvent: " +
                      interstialNotificationEvent.ToString());

            switch (interstialNotificationEvent)
            {
                case FanCraftSDK.InterstialNotificationEvent.InterstitialNotificationWillAppear:
                    // game pause
                    break;
                case FanCraftSDK.InterstialNotificationEvent.InterstitialNotificationDidCancel:
                    // game resume
                    break;
                case FanCraftSDK.InterstialNotificationEvent.InterstitialNotificationDidEngagen:
                    {
                        // game resume
                        FanCraftSDK.NotificationActionType actionType = handler.GetAction();
                        if (actionType == FanCraftSDK.NotificationActionType.NotificationActionTypeInApp)
                        {
                            // The game need to jump this page
                        }

                        // Give rewards to players
                        List<NotificationReward> rewards = handler.GetRewards();
                        Debug.Log(FanCraftSDK.TAG + "ShowInterstitialNotification rewards size: " +
                                  rewards.Count);
                        if (rewards != null && rewards.Count > 0)
                        {
                            for (int i = 0; i < rewards.Count; i++)
                            {
                                NotificationReward reward = rewards[i];
                                Debug.Log(FanCraftSDK.TAG + " ShowInterstitialNotification reward id: " +
                                          reward.RewardId);
                            }
                        }
                    }
                    break;
            }
        }
    }

}