using System;
using System.Collections;
using System.Collections.Generic;
using PRequest = Playtomic.PRequest;

public class PGameVars
{
    private const string SECTION = "gamevars";
    private const string LOAD = "load";
    private const string LOADSINGLE = "single";

    public void Load(Action<Dictionary<string, GameVar>, PResponse> callback)
    {
        Load<GameVar>(callback);
    }

    public void Load(string name, Action<GameVar, PResponse> callback)
    {
        Load<GameVar>(name, callback);
    }

    public void Load<T>(Action<Dictionary<string, T>, PResponse> callback) where T : GameVar, new()
    {
        Playtomic.Api.StartCoroutine(SendRequest(SECTION, LOAD, callback));
    }

    public void Load<T>(string name, Action<T, PResponse> callback) where T : GameVar, new()
    {
        var postdata = new Dictionary<string, object>
        {
            {"name", name}
        };

        Playtomic.Api.StartCoroutine(SendRequest(name, SECTION, LOADSINGLE, callback, postdata));
    }

    internal IEnumerator SendRequest<T>(string section, string action,
        Action<Dictionary<string, T>, PResponse> callback)
        where T : GameVar, new()
    {
        var www = PRequest.Prepare(section, action, null);

        yield return www;

        var response = PRequest.Process(www);

        var data = response.success ? response.json : null;

        var gameVars = new Dictionary<string, T>();

        if (data != null)
        {
            if (data is IDictionary)
            {
                foreach (var key in data.Keys)
                {
                    if (data[key] is IDictionary)
                    {
                        gameVars.Add(key, (T) Activator.CreateInstance(typeof(T), data[key]));
                    }
                }
            }
        }

        callback(gameVars, response);
    }

    internal IEnumerator SendRequest<T>(string name, string section, string action, Action<T, PResponse> callback,
        Dictionary<string, object> postdata = null) where T : GameVar, new()
    {
        var www = PRequest.Prepare(section, action, postdata);

        yield return www;

        var response = PRequest.Process(www);

        var data = response.success ? response.json : null;

        var gameVar = new T();

        if (data != null)
        {
            if (data is IDictionary)
            {
                if (data.ContainsKey(name))
                {
                    gameVar = (T) Activator.CreateInstance(typeof(T), data[name]);
                }
            }
        }

        callback(gameVar, response);
    }
}