Engine.SafeRead = class
{
    static doubleRangeNoTag(dict /*Dictionary<string,object>*/, name /*string*/, defaultValue /*double*/)
    {
        if (defaultValue === undefined) defaultValue = 0;

        try
        {
            if (dict[name] !== undefined && typeof dict[name] == "string")
            {
                var rangeParts = dict[name].split(',');
                if (rangeParts.length == 2)
                {
                    return {"first": parseFloat(rangeParts[0]), "second": parseFloat(rangeParts[1])};
                }
            }
            else if (dict[name] !== undefined)
            {
                var val = parseFloat(dict[name]);
                return {"first": val, "second": val};
            }
        }
        catch(e) {}

        return { "first": defaultValue, "second": defaultValue };
    }

    static doubleRange(dict /**/, prefix /*string*/, defaultValue /*double*/)
    {
        if (defaultValue === undefined) defaultValue = 0;

        try
        {
            if (typeof dict[prefix + "_range"] == "string")
            {
                var rangeParts = dict[prefix + "_range"].split(',');
                if (rangeParts.length == 2)
                {
                    return {"first": parseFloat(rangeParts[0]), "second": parseFloat(rangeParts[1])};
                }
            }
            else if (dict[prefix] !== undefined)
            {
                var val = parseFloat(dict[prefix]);
                return {"first": val, "second": val};
            }
        }
        catch(e)
        {
            
        }
        return { "first":defaultValue, "second":defaultValue };
    }

    static intRangeNoTag(dict /**/, name /*string*/, defaultValue /*double*/)
    {
        if (defaultValue === undefined) defaultValue = 0;

        try
        {
            if (dict[name] !== undefined && typeof dict[name] == "string")
            {
                var rangeParts = dict[name].split(',');
                if (rangeParts.length == 2)
                {
                    return {"first": parseInt(rangeParts[0]), "second": parseInt(rangeParts[1])};
                }
            }
            else if (dict[name] !== undefined)
            {
                var val = parseInt(dict[name]);
                return {"first": val, "second": val};
            }
        }
        catch(e)
        {

        }
        return {"first": defaultValue, "second": defaultValue};
    }

    // wrappers for parity
    static getString(dict, name)
    {
        if (dict.containsKey(name))
        {
            return "" + dict[name];
        }
        return "";
    }

    static double(dict, name)
    {
        if (dict.containsKey(name))
        {
            var value = parseFloat(dict[name]);
            if (isNaN(value)) value = 0;
            return value;
        }
        return 0;
    }

    static long(dict, name)
    {
        if (dict.containsKey(name))
        {
            var value = parseInt(dict[name]);
            if (isNaN(value)) value = 0;
            return value;
        }
        return 0;
    }

    static int(dict, name)
    {
        if (dict.containsKey(name))
        {
            var value = parseInt(dict[name]);
            if (isNaN(value)) value = 0;
            return value;
        }
        return 0;
    }

    static bool(dict, name)
    {
        if (dict.containsKey(name))
        {
            return Boolean(dict[name]);
        }
        return false;
    }

    static dictStrObj(dict, name)
    {
        if (dict.containsKey(name) && typeof(dict[name]) == "object")
        {
            return dict[name];
        }
        return {};
    }

    static dictStrDouble(dict, name)
    {
        if (dict.containsKey(name))
        {
            for (var key of Object.keys(dict[name]))
            {
                dict[name][key] = parseFloat(dict[name][key]);
            }
            return dict[name];
        }
        return {};
    }

    static dictStrInt(dict, name)
    {
        if (dict.containsKey(name))
        {
            for (var key of Object.keys(dict[name]))
            {
                dict[name][key] = parseInt(dict[name][key]);
            }
            return dict[name];
        }
        return {};
    }

    static dictIntObj(dict, name)
    {
        if (dict.containsKey(name))
        {
            return dict[name];
        }
        return {};
    }

    static dictIntInt(dict, name)
    {
        if (dict.containsKey(name))
        {
            for (var key of Object.keys(dict[name]))
            {
                dict[name][key] = parseInt(dict[name][key]);
            }
            return dict[name];
        }
        return {};
    }

    static listDictStrObj(dict, name)
    {
        if (dict.containsKey(name))
        {
            return dict[name];
        }
        return [];
    }

    static listString(dict, name)
    {
        if (dict.containsKey(name))
        {
            return dict[name];
        }
        return [];
    }

    static listDouble(dict, name)
    {
        if (dict.containsKey(name))
        {
            for (var i = 0; i < dict[name].length; i++)
            {
                dict[name][i] = parseFloat(dict[name][i]);
            }
            return dict[name];
        }
        return [];
    }

    static listInt(dict, name)
    {
        if (dict.containsKey(name))
        {
            for (var i = 0; i < dict[name].length; i++)
            {
                dict[name][i] = parseInt(dict[name][i]);
            }
            return dict[name];
        }
        return [];
    }

    static getRect(dict, key)
    {
        var rectData = dict[key];

        var x = rectData["x"] !== undefined ? rectData.x : 0;
        var y = rectData["y"] !== undefined  ? rectData.y : 0;
        var width = rectData["width"] !== undefined  ? rectData.width : 0;
        var height = rectData["height"] !== undefined  ? rectData.height : 0;
        var r = new PIXI.Rectangle(x, y, width, height);

        return r;
    }
}