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

    /**
     * TermsOfService provided by the Stove.
     * @since 2.0.0
     */
    public class TermsOfService
    {
        /**
         * Fetch terms and conditions for agree.
         *
         * If the user agrees to all the terms, it returns an empty map.
         *
         * @param action Fetch Result.
         * @since 2.0.0
         */
        public static void Fetch(Action<Result, List<TermsOfServiceData>> action)
        {
            Debug.Log("TermsOfService.Fetch");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                List<TermsOfServiceData> list = new List<TermsOfServiceData>();
                List<object> termsOfServiceDataList = payload["termsOfServiceDataList"] as List<object>;
                foreach (object obj in termsOfServiceDataList)
                {
                    TermsOfServiceData termsOfServiceData = new TermsOfServiceData(obj as Dictionary<string, object>);
                    list.Add(termsOfServiceData);
                }
                Debug.Log("TermsOfService.Fetch Result(" + result + "), list(" + list + ")");
                action.Invoke(result, list);
            });
            AuthInterface.Fetch(identifier);
        }

        /**
         * Fetch terms and conditions for join.
         *
         * @param action Fetch Result.
         * @since 2.0.0
         */

        public static void FetchForRegister(Action<Result, List<TermsOfServiceData>> action)
        {
            Debug.Log("TermsOfService.FetchForRegister");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                List<TermsOfServiceData> list = new List<TermsOfServiceData>();
                List<object> termsOfServiceDataList = payload["termsOfServiceDataList"] as List<object>;
                foreach (object obj in termsOfServiceDataList)
                {
                    TermsOfServiceData termsOfServiceData = new TermsOfServiceData(obj as Dictionary<string, object>);
                    list.Add(termsOfServiceData);
                }
                Debug.Log("TermsOfService.FetchForRegister Result(" + result + "), list(" + list + ")");
                action.Invoke(result, list);
            });
            AuthInterface.FetchForRegister(identifier);
        }

        /**
         * Save terms and conditions of consent.
         *
         *
         * @param list List of terms and conditions confirming consent.
         * @param action Save Result.
         * @since 2.0.0
         */
        public static void Save(List<TermsOfServiceData> list, Action<Result> action)
        {
            Debug.Log("TermsOfService.Save");
            string identifier = StoveSDK.Instance.AddAction(payload =>
            {
                Result result = new Result(payload["result"] as Dictionary<string, object>);
                Debug.Log("TermsOfService.Save Result(" + result + ")");
                action.Invoke(result);
            });
            AuthInterface.Save(identifier, GetTermsOfServiceDataListString(list));
        }

        private static string GetTermsOfServiceDataListString(List<TermsOfServiceData> termsOfServiceDataList)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (TermsOfServiceData termsOfServiceData in termsOfServiceDataList)
            {
                list.Add(termsOfServiceData.ToDictionary());
            }
            return Json.Serialize(list);
        }
    }
}
