function rampHasMod4v2() {
    var Mod4v2 = ['Module4v2N', 'Module4v2S', 'Module4v2W', 'Module4v2E'];
    for (var i = 0; i < modules.length; i++) {
        mod_name = modules[i].name();
        if (Mod4v2.indexOf(mod_name) !== -1) return true;
    }
    return false;
}

function rampHasMod6() {
    var Mod6 = ['Module6N', 'Module6S', 'Module6W', 'Module6E'];
    for (var i = 0; i < modules.length; i++) {
        if (Mod6.indexOf(modules[i].name()) !== -1) return true;
    }
    return false;
}

let saveQuoteForm    = document.getElementById('saveQuoteForm');
let imageFileField   = document.getElementById('image-file');
let moduleCountField = document.getElementById('mcount');
let actualSlopeField = document.getElementById('actual_slope');
let saveBtn          = document.getElementById('save');

saveBtn.addEventListener('click', submitSaveQuoteForm);

function getModuleCount() {
    layer.draw();

    let countM1 = 0;
    let countM2 = 0;
    let countM3 = 0;
    let countM4 = 0;
    let countM4v2 = 0;
    let countM5 = 0;
    let countM6 = 0;
    let countM7 = 0;
    let countM7B = 0;
    let countM8 = 0;
    let countM9 = 0;
    let countM10 = 0;

    for (var i = 0; i < modules.length; i++) {
        let mod_name = modules[i].name();

        let Mod1   = ['Module1N', 'Module1S', 'Module1W', 'Module1E'];
        let Mod2   = ['Module2N', 'Module2W'];
        let Mod3   = ['Module3N', 'Module3S', 'Module3W', 'Module3E'];
        let Mod4   = ['Module4N', 'Module4S', 'Module4W', 'Module4E'];
        let Mod4v2 = ['Module4v2N', 'Module4v2S', 'Module4v2W', 'Module4v2E'];
        let Mod5   = ['Module5N', 'Module5S', 'Module5W', 'Module5E'];
        let Mod6   = ['Module6N', 'Module6S', 'Module6W', 'Module6E'];
        let Mod7   = ['Module7N', 'Module7S', 'Module7W', 'Module7E'];
        let Mod7B  = ['Module7BN', 'Module7BS', 'Module7BW', 'Module7BE'];
        let Mod8   = ['Module8N', 'Module8S', 'Module8W', 'Module8E'];
        let Mod9   = ['Module9N', 'Module9S', 'Module9W', 'Module9E'];
        let Mod10  = ['Module10N', 'Module10S', 'Module10W', 'Module10E'];

        if (Mod1.indexOf(mod_name) > -1) countM1 += 1;
        if (Mod2.indexOf(mod_name) > -1) countM2 += 1;
        if (Mod3.indexOf(mod_name) > -1) countM3 += 1;
        if (Mod4.indexOf(mod_name) > -1) countM4 += 1;
        if (Mod4v2.indexOf(mod_name) > -1) countM4v2 += 1;
        if (Mod5.indexOf(mod_name) > -1) countM5 += 1;
        if (Mod6.indexOf(mod_name) > -1) countM6 += 1;
        if (Mod7.indexOf(mod_name) > -1) countM7 += 1;
        if (Mod7B.indexOf(mod_name) > -1) countM7B += 1;
        if (Mod8.indexOf(mod_name) > -1) countM8 += 1;
        if (Mod9.indexOf(mod_name) > -1) countM9 += 1;
        if (Mod10.indexOf(mod_name) > -1) countM10 += 1;
    }

    var module_count = {
        M1: countM1,
        M2: countM2,
        M3: countM3,
        M4: countM4,
        M4v2: countM4v2,
        M5: countM5,
        M6: countM6,
        M7: countM7,
        M7B: countM7B,
        M8: countM8,
        M9: countM9,
        M10: countM10,
    };

    return JSON.stringify(module_count);
}

function submitSaveQuoteForm(event) {
    event.preventDefault();

    if (modules.length == 0) {
        alert('Please add at least one module.');

        return;
    }

    const dataUrl = stage.toDataURL();
    imageFileField.value       = dataUrl;
    moduleCountField.value     = getModuleCount();
    actualSlopeField.value     = actualSlope;
    saveQuoteForm.action.value = event.target.name;

    saveQuoteForm.submit();
}


function getTotalHeight(){
    let totalHeight = calculateTotalHeight(modules)
    return convertPxToFt(totalHeight);
}

function getTotalWidth(){
    let totalWidth = calculateTotalWidth(modules)
    return convertPxToFt(totalWidth);
}

function checkRampLength() {
    if ( getTotalWidth() > 30 || getTotalHeight() > 30 ) {
        totalLengthWarning.classList.remove('dn');
    } else {
        totalLengthWarning.classList.add('dn');
    }
}

document.body.addEventListener('click', checkRampLength);

function checkForMod9_10() {
    var isAda = typeof adaCompliant !== 'undefined' && adaCompliant;

    for (var i = 0; i < modules.length; i++) {
        let mod_name = modules[i].name();
        let Mod3     = ['Module3N', 'Module3S', 'Module3W', 'Module3E'];
        let Mod8     = ['Module8N', 'Module8S', 'Module8W', 'Module8E'];
        let Mod9     = ['Module9N', 'Module9S', 'Module9W', 'Module9E'];
        let Mod10    = ['Module10N', 'Module10S', 'Module10W', 'Module10E'];

        if (Mod8.indexOf(mod_name) > -1 || Mod10.indexOf(mod_name) > -1) return true;
        if (!isAda && (Mod3.indexOf(mod_name) > -1 || Mod9.indexOf(mod_name) > -1)) return true;
    }

    return false;
}

function convertPxToFt(val) {
    return Math.round(val*10/304.8*10)/10;
}

function calculateTotalWidth(modules) {
    let totalWidth = 0;
    let previousModule = null;

    modules.forEach((module) => {
        const moduleName = module.attrs.name;
        const moduleWidth = module.attrs.width;

        if (moduleName.endsWith('W')) {
            totalWidth += moduleWidth;
            if (previousModule && (previousModule.attrs.name.endsWith('N') || previousModule.attrs.name.endsWith('S'))) {
                totalWidth += previousModule.attrs.width - 50;
            }
        }

       previousModule = module;
    });

    return totalWidth;
}

function calculateTotalHeight(modules) {
    let totalHeight = 0;
    let previousModule = null;

    modules.forEach((module) => {
        const moduleName = module.attrs.name;
        const moduleHeight = module.attrs.height;

        if (moduleName.endsWith('N')) {
            totalHeight += moduleHeight;
            if (previousModule && (previousModule.attrs.name.endsWith('W') || previousModule.attrs.name.endsWith('E'))) {
                totalHeight += (previousModule.attrs.height - 80);
            }
        }

       previousModule = module;
    });

    return totalHeight;
}

function calculateTotalLength(modules, vertical = false) {
    let total = 0;
    let prev  = null;

    modules.forEach((module) => {
        let name   = module.attrs.name;
        let length = vertical ? module.attrs.height : module.attrs.width;

        if (name.endsWith('W')) {
            total += length;

            if (prev && (prev.attrs.name.endsWith('N') || prev.attrs.name.endsWith('S'))) {
                total += prev.attrs.width - 50;
            }
        }

        prev = module;
    });

    return total;
}
