export default class UrlParam
{
    constructor()
    {
        this.url = document.location.toString();
        this.params = UrlParam.getParamsFromUrl(this.url);
    }

    get(name)
    {
        return this.params[name];
    }

    static getParamsFromUrl(url)
    {
        let urlParts = url.split('?');

        if (urlParts.length !== 2) {
            return [];
        }

        let params = urlParts[1].split('&');

        if (params.length === 0) {
            return [];
        }

        let pairs = [];

        params.forEach(
            (param) => {
                let parts = param.split('=');

                if (parts.length !== 2) {
                    return;
                }

                pairs[parts[0]] = parts[1];
            }
        );

        return pairs;
    }

    getInt(name)
    {
        const value = parseInt(this.get(name));

        return isNaN(value) ? 0 : value;
    }

    getFloat(name)
    {
        const value = parseFloat(this.get(name));

        return isNaN(value) ? 0.0 : value;
    }
}
