const storage_price = [.0656,.0878,.1093,.1231];

const prices = {
    nc: {
        storage: storage_price,
        users: [0.66,1.16,1.76,2.16],
        options: {
            lxc: {
                basePrice: 31,
                factor: 1.217
            },
            dedicated: {
                basePrice: 64,
                factor: 1.753
            },
            onprem: {
                basePrice: 111,
                factor: 1.498
            },
            vol: [75,90,105,120]
        }
    },
    wh: {
        storage: storage_price,
        sites: [3.6,4.7,6.9,9.1],
        options: {
            preinstall: {
                basePrice: 3.773,
                factor: 1.168
            }
        }
    },
    mail: {
        storage: storage_price,
        users: [2.1,4.2,7.3,11.4],
        options: {
            catchall: {
                basePrice: 3.787,
                factor: 1.196
            }
        }
    },
    vps: {
        storage: storage_price.map(x => x * 1.1297),
        cpu: [6.146, 7.687, 9.218, 10.749],
        ram: [1.646, 2.057, 2.468, 2.879]
    }
};


$('.ui.accordion')
    .accordion({
        exclusive: true,
        collapsible: false
    })
;

$('.ui.sticky')
    .sticky()
;
$('.shape').shape();

$('.ui.toggle.checkbox').checkbox({
    onChange: function () {
        updatePrice();
    }
});

$('#nc_opt_vol_val').on('change', function() {
    updatePrice();
});

$('.ui.toggle.checkbox.nc_opt_lxc').checkbox({
    onChange: function() {
        updatePrice();
        $('#ncusers').shape('flip up');
        $('#nextcloud_users_source').toggleClass('disabled');
    },
    onChecked: function() {
        $('.ui.toggle.checkbox.nc_opt_decicated').checkbox('set enabled');
    },
    onUnchecked: function() {
        $('.ui.toggle.checkbox.nc_opt_decicated').checkbox('uncheck').checkbox('set disabled');
        $('.ui.toggle.checkbox.nc_opt_onprem').checkbox('uncheck').checkbox('set disabled');
    }
});
$('.ui.toggle.checkbox.nc_opt_decicated').checkbox({
    onChange: function() {
        updatePrice();
    },
    onChecked: function() {
        $('.ui.toggle.checkbox.nc_opt_onprem').checkbox('set enabled');
    },
    onUnchecked: function() {
        $('.ui.toggle.checkbox.nc_opt_onprem').checkbox('uncheck').checkbox('set disabled');
    }
}).checkbox('set disabled');
$('.ui.toggle.checkbox.nc_opt_onprem').checkbox({
    onChange: function() {
        updatePrice();
    }
}).checkbox('set disabled');

$('#nextcloud_stockage_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 50, 100, 200, 300, 400, 500],
        min: 0,
        max: 500,
        step: 5,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#nextcloud_stockage_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#nextcloud_users_source')
    .slider({
            showThumbTooltip: true,
            restrictedLabels: [0, 5, 10, 15, 20, 30, 40, 50],
            min: 0,
            max: 50,
            // step: 10,
            autoAdjustLabels: false,
            onMove: function(range, firstVal, secondVal) {
                $('#nextcloud_users_display').html(firstVal);
                updatePrice();
            }
    })
;

$('#wh_stockage_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 5, 10, 25, 50, 75, 100],
        min: 0,
        max: 100,
        step: 1,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#wh_stockage_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#wh_sites_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 5, 10, 15, 20],
        min: 0,
        max: 20,
        // step: 10,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#wh_sites_display').html(firstVal);
            updatePrice();
        }
    })
;


$('#mail_stockage_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 5, 10, 25, 50, 75, 100],
        min: 0,
        max: 100,
        step: 1,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#mail_stockage_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#mail_users_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 5, 10, 15, 20],
        min: 0,
        max: 20,
        // step: 10,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#mail_users_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#vps_stockage_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 25, 50, 75, 100, 200, 300, 400, 500],
        min: 0,
        max: 500,
        step: 25,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#vps_stockage_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#vps_cpu_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 1, 2, 4, 8, 16, 32, 48, 64],
        min: 0,
        max: 64,
        step: 1,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#vps_cpu_display').html(firstVal);
            updatePrice();
        }
    })
;

$('#vps_ram_source')
    .slider({
        showThumbTooltip: true,
        restrictedLabels: [0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128],
        min: 0,
        max: 128,
        step: 1,
        autoAdjustLabels: false,
        onMove: function(range, firstVal, secondVal) {
            $('#vps_ram_display').html(firstVal);
            updatePrice();
        }
    })
;



function updatePrice() {
    let ncPriceStorage = $('#nextcloud_stockage_source').slider('get value');
    const ncPriceUsers = $('#nextcloud_users_source').slider('get value');
    const whPriceStorage =  $('#wh_stockage_source').slider('get value');
    const whPriceSites =  $('#wh_sites_source').slider('get value');
    const mailPriceStorage = $('#mail_stockage_source').slider('get value');
    const mailPriceUsers = $('#mail_users_source').slider('get value');

    const vpsPriceStorage = $('#vps_stockage_source').slider('get value');
    const vpsPriceCpu = $('#vps_cpu_source').slider('get value');
    const vpsPriceRam = $('#vps_ram_source').slider('get value');

    const cbNcLxc = $('.ui.toggle.checkbox.nc_opt_lxc').checkbox('is checked');
    const cbNcDedicated = $('.ui.toggle.checkbox.nc_opt_decicated').checkbox('is checked');
    const cbNcOnprem = $('.ui.toggle.checkbox.nc_opt_onprem').checkbox('is checked');
    const cbWhPreinstall = $('.ui.toggle.checkbox.wh_opt_preinstall').checkbox('is checked');
    const cbMailCatchall = $('.ui.toggle.checkbox.mail_opt_catchall').checkbox('is checked');

    let opt = { basePrice: 0, factor: 1 };

    if (cbNcLxc) {
        opt = prices.nc.options.lxc;
    }
    if (cbNcLxc && cbNcDedicated) {
        opt = prices.nc.options.dedicated;
    }
    if (cbNcLxc && cbNcDedicated && cbNcOnprem) {
        opt = prices.nc.options.onprem;
    }

    const ncOptVol = $('.ui.checkbox.nc_opt_vol').checkbox('is checked')
        ? parseInt($('#nc_opt_vol_val').val()) || 1
        : 0;

    for (let i=0; i<4; i++) {

        // base nc price calculation
        let ncPrice = ncPriceStorage * prices.nc.storage[i];
        if (!cbNcLxc) ncPrice += ncPriceUsers * prices.nc.users[i];

        // add options pricing if any
        ncPrice = (ncPrice + opt.basePrice) * opt.factor;

        if (ncOptVol>0) ncPrice += Math.max(100, Math.pow(ncOptVol, 0.7) * prices.nc.options.vol[i]);

        if (ncOptVol>0) {
            if (i == 0) {
                $('#teraeurolow').html((ncPrice / ncOptVol).toPrecision(3));
            }

            if (i == 3) {
                $('#teraeurohigh').html((ncPrice / ncOptVol).toPrecision(3));
            }
        } else {
            $('#teraeurolow').html('-');
            $('#teraeurohigh').html('-');
        }

        // base wh price calculation
        let whPrice = whPriceStorage * prices.wh.storage[i]
             + whPriceSites * prices.wh.sites[i];
        if (cbWhPreinstall) whPrice = (whPrice + prices.wh.options.preinstall.basePrice) * prices.wh.options.preinstall.factor;

        // base mail price calculation
        let mailPrice = mailPriceStorage * prices.mail.storage[i]
            + mailPriceUsers * prices.mail.users[i];
        if (cbMailCatchall) mailPrice = (mailPrice + prices.mail.options.catchall.basePrice) * prices.mail.options.catchall.factor;

        // base VPS price calculation
        let vpsPrice = vpsPriceStorage * prices.vps.storage[i]
            + vpsPriceCpu * prices.vps.cpu[i]
            + vpsPriceRam * prices.vps.ram[i];

        let totalPrice = ncPrice + whPrice + mailPrice + vpsPrice;

        totalPrice = Math.ceil(totalPrice);
        $('#price_'+i).html(totalPrice+'€');
    }

}

$(document)
    .ready(function() {
        updatePrice();
    });
