﻿using UnityEngine;
using UnityEngine.UI;
using Stove.StoveSDK;
using MiniJSON;
using System.Collections.Generic;

public class ViewSample : MonoBehaviour
{
    void Start()
    {
    }

    void Update()
    {
    }

    public void FetchPopup()
    {
        Debug.Log("------- [Stove-Sample] View.FetchPopup -------");

        View.FetchPopup((Result result, List<ViewRequest> popupDataList) =>
        {
            Debug.Log("------- [Stove-Sample] View.FetchPopup : " + result + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("View.FetchPopup Success", SSTools.Position.bottom, SSTools.Time.oneSecond);

                foreach (ViewRequest popupData in popupDataList)
                {
                    Debug.Log("popupData : " + popupData);
                }
            }
            else
            {
                SSTools.ShowMessage("View.FetchPopup Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void FetchPopupManual()
    {
        PopupLocationDialog.Show(location =>
        {
            if (location != -1)
            {
                Debug.Log("------- [Stove-Sample] View.FetchPopupWithLocation -------");

                View.FetchPopup(location, (Result result, List<ViewRequest> popupDataList) =>
                {
                    Debug.Log("------- [Stove-Sample] View.FetchPopupWithLocation : " + result + " -------");

                    if (result.IsSuccessful)
                    {
                        SSTools.ShowMessage("View.FetchPopup Success", SSTools.Position.bottom, SSTools.Time.oneSecond);

                        foreach (ViewRequest popupData in popupDataList)
                        {
                            Debug.Log("popupData : " + popupData);
                        }
                    }
                    else
                    {
                        SSTools.ShowMessage("View.FetchPopup Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
            else
            {
                // cancel
            }
        });
    }

    public void CustomerSupport()
    {
        Debug.Log("------- [Stove-Sample] ViewUI.CustomerSupport -------");

        ViewUI.CustomerSupport((Result result, Dictionary<string, string> dictionary) =>
        {
            Debug.Log("------- [Stove-Sample] ViewUI.CustomerSupport : " + result + " -------");
            Debug.Log("------- [Stove-Sample] ViewUI.CustomerSupport : " + dictionary + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("ViewUI.CustomerSupport Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("ViewUI.CustomerSupport Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void Coupon()
    {
        Debug.Log("------- [Stove-Sample] ViewUI.Coupon -------");

        ViewUI.Coupon((Result result, Dictionary<string, string> dictionary) =>
        {
            Debug.Log("------- [Stove-Sample] ViewUI.Coupon : " + result + " -------");
            Debug.Log("------- [Stove-Sample] ViewUI.Coupon : " + dictionary + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("ViewUI.Coupon Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("ViewUI.Coupon Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void Community()
    {
        Debug.Log("------- [Stove-Sample] ViewUI.Community -------");

        ViewUI.Community((Result result, Dictionary<string, string> dictionary) =>
        {
            Debug.Log("------- [Stove-Sample] ViewUI.Community : " + result + " -------");
            Debug.Log("------- [Stove-Sample] ViewUI.Community : " + dictionary + " -------");

            if (result.IsSuccessful)
            {
                if (dictionary != null && dictionary.TryGetValue("received_data", out string receivedData))
                {
                    if (!string.IsNullOrEmpty(receivedData))
                    {
                        Dictionary<string, object> data = Json.Deserialize(receivedData) as Dictionary<string, object>;

                        if (data != null)
                        {
                            if (data.TryGetValue("code", out object codeObj) && codeObj is long errorCode ) 
                            {
                                if (errorCode == 40104) {
                                    Debug.Log("------- [Stove-Sample] ViewUI.Community userInfo expired token");                                
                                } else {
                                    Debug.Log("------- [Stove-Sample] ViewUI.Community userInfo code : " + errorCode + " -------");                                
                                }
                            }
                             
                            if (data.TryGetValue("message", out object messageObj) && messageObj is string message)
                            {
                                Debug.Log("------- [Stove-Sample] ViewUI.Community userInfo message : " + message + " -------");
                            }
                        }
                    }
                }
                        
                SSTools.ShowMessage("ViewUI.Community Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("ViewUI.Community Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void CommunityWithURL()
    {
        TextFieldOneDialog.Show((string url) =>
        {
            if (url == null)
            {
                // cancel
            }
            else
            {
                Debug.Log("------- [Stove-Sample] View.CommunityWithURL -------");

                ViewUI.Community(url, (Result result, Dictionary<string, string> dictionary) =>
                {
                    Debug.Log("------- [Stove-Sample] ViewUI.CommunityWithURL : " + result + " -------");
                    Debug.Log("------- [Stove-Sample] ViewUI.CommunityWithURL : " + dictionary + " -------");

                    if (result.IsSuccessful)
                    {
                        SSTools.ShowMessage("ViewUI.CommunityWithURL Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
                     }
                    else
                    {
                        SSTools.ShowMessage("ViewUI.CommunityWithURL Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
        });
    }

    public void Popup()
    {
        Debug.Log("------- [Stove-Sample] ViewUI.Popup -------");

        ViewUI.Popup((Result result, Dictionary<string, string> dictionary) =>
        {
            Debug.Log("------- [Stove-Sample] ViewUI.Popup : " + result + " -------");
            Debug.Log("------- [Stove-Sample] ViewUI.Popup : " + dictionary + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("ViewUI.Popup Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("ViewUI.Popup Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void PopupManual()
    {
        PopupLocationDialog.Show(location =>
        {
            if (location != -1)
            {
                Debug.Log("------- [Stove-Sample] ViewUI.PopupWithLocation -------");

                ViewUI.Popup(location, (Result result, Dictionary<string, string> dictionary) =>
                {
                    Debug.Log("------- [Stove-Sample] ViewUI.PopupWithLocation : " + result + " -------");
                    Debug.Log("------- [Stove-Sample] ViewUI.PopupWithLocation : " + dictionary + " -------");

                    if (result.IsSuccessful)
                    {
                        SSTools.ShowMessage("ViewUI.PopupWithLocation Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                    else
                    {
                        SSTools.ShowMessage("ViewUI.PopupWithLocation Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
            else
            {
                // cancel
            }
        });
    }

    public void Load()
    {
        LoadViewDialog.Show((url, viewConfigDic) =>
        {
            if (url == null && viewConfigDic == null)
            {
                // cancel
            }
            else
            {
                Debug.Log("------- [Stove-Sample] ViewUI.Load -------");

                ViewConfiguration viewConfig = new ViewConfiguration(viewConfigDic);

                ViewRequest viewRequest = new ViewRequest(url);
                viewRequest.ViewConfiguration = viewConfig;
                ViewUI.Load(viewRequest, (Result result, Dictionary<string, string> dictionary) =>
                {
                    Debug.Log("------- [Stove-Sample] ViewUI.Load : " + result + " -------");
                    Debug.Log("------- [Stove-Sample] ViewUI.Load : " + dictionary + " -------");

                    if (result.IsSuccessful)
                    {
                        if (result.UserInfo != null && result.UserInfo.TryGetValue("userAction", out string userAction))
                        {
                            //게임 탈퇴
                            if (!string.IsNullOrEmpty(userAction) && userAction.Equals("withdraw_complete"))
                            {
                                /** ex) 게임 탈퇴 완료 / 게임 시나리오에 맞게 적용 **/
                            }
                        }

                        SSTools.ShowMessage("ViewUI.Load Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                    else
                    {
                        SSTools.ShowMessage("ViewUI.Load Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
        });        
    }

    public void UseCoupon()
    {
        TextFieldOneDialog.Show((string code) =>
        {
            if (code == null)
            {
                // cancel
            }
            else
            {
                Debug.Log("------- [Stove-Sample] View.UseCoupon -------");

                View.UseCoupon(code, (Result result) =>
                {
                    Debug.Log("------- [Stove-Sample] View.UseCoupon : " + result + " -------");

                    if (result.IsSuccessful)
                    {
                        SSTools.ShowMessage("View.UseCoupon Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                    else
                    {
                        SSTools.ShowMessage("View.UseCoupon Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
        });
    }

    public void News()
    {
        Debug.Log("------- [Stove-Sample] ViewUI.News -------");

        ViewUI.News((Result result, Dictionary<string, string> dictionary) =>
        {
            Debug.Log("------- [Stove-Sample] ViewUI.News : " + result + " -------");
            Debug.Log("------- [Stove-Sample] ViewUI.News : " + dictionary + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("ViewUI.News Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("ViewUI.News Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void FetchNews()
    {
        Debug.Log("------- [Stove-Sample] View.FetchNews -------");

        View.FetchNews((Result result, List<ViewRequest> popupDataList) =>
        {
            Debug.Log("------- [Stove-Sample] View.FetchNews : " + result + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("View.FetchNews Success", SSTools.Position.bottom, SSTools.Time.oneSecond);

                foreach (ViewRequest popupData in popupDataList)
                {
                    Debug.Log("popupData : " + popupData);
                }
            }
            else
            {
                SSTools.ShowMessage("View.FetchNews Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void Exit()
    {
    
    }

    public void OpenExternalUrl()
    {
        TextFieldOneDialog.Show((string url) =>
        {
            if (url == null)
            {
                // cancel
            }
            else
            {
                Debug.Log("------- [Stove-Sample] View.OpenExternalUrl -------");
                View.OpenExternalUrl(url, (Result result) =>
                {
                    Debug.Log("------- [Stove-Sample] ViewUI.OpenExternalUrl : " + result + " -------");
                    if (result.IsSuccessful)
                    {
                        SSTools.ShowMessage("ViewUI.OpenExternalUrl Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
                        }
                    else
                    {
                        SSTools.ShowMessage("ViewUI.OpenExternalUrl Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
                    }
                });
            }
        });
    }

    public void Version()
    {
        SSTools.ShowMessage("View Version : " + View.GetVersion(), SSTools.Position.bottom, SSTools.Time.oneSecond);
    }
}
