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

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

    void Update()
    {
    }

    public void AddEvent()
    {
        LogDialog.Show((string name, string category1, string category2, string simpleValue, string parameters) =>
        {
            if (name == null &&
                 category1 == null &&
                 category2 == null &&
                 simpleValue == null &&
                 parameters == null)
            {
                // cancel
            }
            else
            {

                LogEvent logEvent = new LogEvent(name);

                if (!string.IsNullOrEmpty(category1))
                {
                    logEvent.Category1 = category1;
                }

                if (!string.IsNullOrEmpty(category2))
                {
                    logEvent.Category2 = category2;
                }

                if (!string.IsNullOrEmpty(simpleValue))
                {
                    logEvent.SimpleValue = float.Parse(simpleValue);
                }

                if (!string.IsNullOrEmpty(parameters))
                {
                    logEvent.Parameters = Json.Deserialize(parameters) as Dictionary<string, string>;
                }

                Debug.Log("------- [Stove-Sample] Log.Add -------");

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

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

    public void Flush()
    {
        Log.Flush();

        Debug.Log("------- [Stove-Sample] Log.Flush -------");
    }

    public void AddEvent50()
    {
        for (int i = 0; i < 50; i++)
        {
            LogEvent logEvent = new LogEvent("AddEvent50 : " + i);

            Debug.Log("------- [Stove-Sample] Log.AddEvent50 start : " + i + " -------");

            Log.Add(logEvent, (Result result) =>
            {
                Debug.Log("------- [Stove-Sample] Log.AddEvent50 end : " + i + " -------");

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

    public void AddEventCustom()
    {
        TextFieldOneDialog.Show((string count) =>
        {
            if (count != null)
            {
                int convertCount = Convert.ToInt32(count);
                for (int i = 0; i < convertCount; i++)
                {
                    LogEvent logEvent = new LogEvent("AddEvent" + convertCount + " : " + i);

                    Debug.Log("------- [Stove-Sample] Log.AddEvent" + convertCount + "start : " + i + " -------");

                    Log.Add(logEvent, (Result result) =>
                    {
                        Debug.Log("------- [Stove-Sample] Log.AddEvent" + convertCount + "end : " + i + " -------");

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

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

    public void Versions()
    {
        SSTools.ShowMessage("Log Versions : " + Log.GetSDKVersions(), SSTools.Position.bottom, SSTools.Time.oneSecond);
    }

    public void SetExternalId()
    {
        TextFieldOneDialog.Show((string externalId) =>
        {
            if (externalId != null)
            {
                Debug.Log("------- [Stove-Sample] Log.SetExternalId : " + externalId + " -------");
                
                Log.SetExternalId(externalId);
                
                SSTools.ShowMessage("Log.SetExternalId Success: " + externalId, SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }

    public void AddGameLog() {
        GameLogDialog.Show((string parameters, string additionalParameters) =>
        {
            if (parameters == null && additionalParameters == null) {}
            else {

                GameLogEvent logEvent = new GameLogEvent();

                if (!string.IsNullOrEmpty(parameters))
                {
                    var parsedParams = Json.Deserialize(parameters) as Dictionary<string, object>;
                    if (parsedParams == null)
                    {
                        Debug.Log("------- [Stove-Sample] GameLog.Add failed: parameters is not valid JSON format -------");
                        return;
                    }
                    logEvent.Parameters = parsedParams;
                }

                if (!string.IsNullOrEmpty(additionalParameters))
                {
                    var parsedAdditionalParams = Json.Deserialize(additionalParameters) as Dictionary<string, object>;
                    if (parsedAdditionalParams == null)
                    {
                        Debug.Log("------- [Stove-Sample] GameLog.Add failed: additionalParameters is not valid JSON format -------");
                        return;
                    }
                    logEvent.AdditionalParameters = parsedAdditionalParams;
                }

                Debug.Log("------- [Stove-Sample] GameLog.Add -------");

                GameLog.Add(logEvent, (Result result) =>
                {
                    Debug.Log("------- [Stove-Sample] GameLog.Add : " + result + " -------");

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

    public void FlushGameLog()
    {
        GameLog.Flush();

        Debug.Log("------- [Stove-Sample] GameLog.Flush -------");
    }

    public void AddGameLogNCount() {
        // 1. TextFieldOneDialog로 전송할 횟수를 입력받음
        TextFieldOneDialog.Show((string count) =>
        {
            // 2. cancel이면 그냥 종료
            if (count == null)
            {
                return;
            }

            int convertCount = Convert.ToInt32(count);

            // 3. OK이면 GameLogDialog를 띄워서 내용 입력
            GameLogDialog.Show((string parameters, string additionalParameters) =>
            {
                // GameLogDialog 에서 cancel 이면 종료
                if (parameters == null && additionalParameters == null)
                {
                    return;
                }

                // 입력 값으로 GameLogEvent 구성
                GameLogEvent logEvent = new GameLogEvent();

                if (!string.IsNullOrEmpty(parameters))
                {
                    var parsedParams = Json.Deserialize(parameters) as Dictionary<string, object>;
                    if (parsedParams == null)
                    {
                        Debug.Log("------- [Stove-Sample] GameLog.AddNCount failed: parameters is not valid JSON format -------");
                        return;
                    }
                    logEvent.Parameters = parsedParams;
                }

                if (!string.IsNullOrEmpty(additionalParameters))
                {
                    var parsedAdditionalParams = Json.Deserialize(additionalParameters) as Dictionary<string, object>;
                    if (parsedAdditionalParams == null)
                    {
                        Debug.Log("------- [Stove-Sample] GameLog.AddNCount failed: additionalParameters is not valid JSON format -------");
                        return;
                    }
                    logEvent.AdditionalParameters = parsedAdditionalParams;
                }

                // 4. GameLogDialog 에서 OK를 누르면 지정된 횟수만큼 GameLog.Add 실행
                for (int i = 0; i < convertCount; i++)
                {
                    int currentIndex = i;
                    Debug.Log("------- [Stove-Sample] GameLog.AddNCount start : " + currentIndex + " / " + convertCount + " -------");

                    GameLog.Add(logEvent, (Result result) =>
                    {
                        Debug.Log("------- [Stove-Sample] GameLog.AddNCount end : " + currentIndex + " / " + convertCount + " -------");

                        if (result.IsSuccessful)
                        {
                            SSTools.ShowMessage("GameLog.Add Success (" + (currentIndex + 1) + "/" + convertCount + ")", SSTools.Position.bottom, SSTools.Time.oneSecond);
                        }
                        else
                        {
                            SSTools.ShowMessage("GameLog.Add Fail (" + (currentIndex + 1) + "/" + convertCount + ")", SSTools.Position.bottom, SSTools.Time.oneSecond);
                        }
                    });
                }

                Debug.Log("------- [Stove-Sample] GameLog.AddNCount completed: " + convertCount + " events -------");
            });
        });
    }
}
