﻿namespace Stove.StoveSDK
{
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using MiniJSON;

    /**
     * View UI provided by the Stove.
     * @since 2.0.0
     */
    public class ViewUI
    {
        /**
        * Show game news.
        * @param action Show result.
        * @see Result
        * @since 2.0.0
        */
        public static void News(Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.News");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.News Result : " + result);
                Debug.Log("ViewUI.News UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.News(identifier);
        }

        /**
        * Show customer support UI.
        * @param action Show result.
        * @see Result
        * @since 2.0.0
        */
        public static void CustomerSupport(Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.CustomerSupport");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.CustomerSupport Result : " + result);
                Debug.Log("ViewUI.CustomerSupport UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.CustomerSupport(identifier);
        }

        /**
         * Show coupon UI.
         * @param action Show result.
         * @see Result
         * @since 2.0.0
         */
        public static void Coupon(Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.Coupon");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.Coupon Result : " + result);
                Debug.Log("ViewUI.Coupon UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Coupon(identifier);
        }

        /**
         * Show popup UI.
         * @param action Show result.
         * @see Result
         * @since 2.0.0
         */
        public static void Popup(Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.Popup");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.Popup Result : " + result);
                Debug.Log("ViewUI.Popup UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Popup(identifier);
        }

        /**
         * Show popup UI.
         * @param location Popup's location. If location isn't set, an automatic popup is shown.
         * @param action Show result.
         * @see Result
         * @since 2.0.0
         */
        public static void Popup(int location, Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.Popup with location");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.Popup with location Result : " + result);
                Debug.Log("ViewUI.Popup with location UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Popup(location, identifier);
        }

        /**
         * Show community UI.
         * @param action Show result.
         * @since 2.0.0
         * @see Result
         */
        public static void Community(Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.Community");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.Community Result : " + result);
                Debug.Log("ViewUI.Community UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Community(identifier);
        }

        /**
         * Show community UI With URL.
         * @param url Url of community.
         * @param action Show result.
         * @since 2.2.3
         * @see Result
         */
        public static void Community(string url, Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.CommunityWithURL");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.CommunityWithURL Result : " + result);
                Debug.Log("ViewUI.CommunityWithURL UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Community(url, identifier);
        }

        /**
         * Show custom UI.
         * @param viewRequest View request information.
         * @param action Show result.
         * @see ViewRequest
         * @see Result
         * @since 2.0.0
         */

        public static void Load(ViewRequest viewRequest, Action<Result, Dictionary<string, string>> action)
        {
            Debug.Log("ViewUI.Load");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Dictionary<string, string> userInfo = MakeUserInfo(payload);
                Debug.Log("ViewUI.Load Result : " + result);
                Debug.Log("ViewUI.Load UserInfo : " + SerializeUserInfo(userInfo));

                action.Invoke(result, userInfo);
            });
            ViewInterface.Load(viewRequest.ToJSONString(), identifier);
        }

        private static Dictionary<string, string> MakeUserInfo(Dictionary<string, object> payload)
        {
            Dictionary<string, string> userInfo = new Dictionary<string, string>();
            if (payload.ContainsKey("userInfo"))
            {
                Dictionary<string, object> dictionary = payload["userInfo"] as Dictionary<string, object>;
                foreach (KeyValuePair<string, object> keyValuePair in dictionary)
                {
                    userInfo.Add(keyValuePair.Key, keyValuePair.Value as string);
                }
            }
            return userInfo;
        }

        private static string SerializeUserInfo(Dictionary<string, string> userInfo)
        {
            string userInfoString = "";
            if (userInfo != null)
            {
                userInfoString = Json.Serialize(userInfo);
            }
            return userInfoString;
        }
    }
}
