function selectableFileModalOpen(e) {
    
    $('#'+$(e.target).data('target-matching-condition')).find('input[type=radio]').each(function () {
        
        $(this).get(0).checked = false;
        $(this).removeAttr('checked');
        $(this).prop('checked', null);
        let c = getCookie('FileSelectableMatchingFunction');
        
        if ($(this).val() == c) {
            $(this).get(0).checked = true;
            $(this).attr('checked', true);
            $(this).prop('checked', true);
        }
    });

    $('#' + $(e.target).data('target-data-format')).val(getCookie('multiSelectFileFormat'));

    $('#' + $(e.target).data('target-data-format')).find('option').each(function () {
        
        $(this).get(0).selected = false;
        $(this).removeAttr('selected');
        $(this).prop('selected', null);
        let c = getCookie('multiSelectFileFormat');

        if ($(this).val() == c) {
            $(this).get(0).selected = true;
            $(this).attr('selected', true);
            $(this).prop('selected', true);
        }
    });

    $('#' + $(e.target).data('target-data-format')).selectpicker('refresh');

}

function multiSelectFileFormatChange(e) {
    setCookie('multiSelectFileFormat', e.target.value);
    if ($(e.target).data('target-file'))
        resetFileCtrl($(e.target).data('target-file'));
}

function resetFileCtrl(id) {
    let fileCtrl = document.getElementById(id);
    //fileCtrl.files = [];
    fileCtrl.value = '';
    //fileCtrl.parentNode.replaceChild(fileCtrl.cloneNode(), fileCtrl);
}

function searchAndSelectFromFile(src, e) {

    if (src && src.files && src.files.length > 0) {
        let file = src.files[0];
        let targetModalId = $(src).data('target-modal');
        let selectableLbx = $get($(src).data('target-selectable'));
        let selectedLbx = $get($(src).data('target-selected'));
        let selectedHf = $get($(src).data('target-selectedhf'));
        let sortByVal = $(src).data('target-sortbyval');
        let dataFormat = $get($(src).data('target-file-format')).value;

        let reader = new FileReader();
        reader.onload = function (progressEvent) {

            let lines = new Array();
            dataFormat = dataFormat ? dataFormat : 'line';
            switch (dataFormat) {
                case 'line':
                    lines = this.result.split('\r\n');
                    break;
                case 'comma':
                    lines = this.result.split(',');
                    break;
                case 'semicolon':
                    lines = this.result.split(';');
                    break;
                default:
                    for (let l of this.result.split('\r\n'))
                        for (let m of l.split(','))
                            for (let n of m.split(';'))
                                lines.push(n);
            }

            let matchCondition = getCookie('FileSelectableMatchingFunction');

            matchCondition = matchCondition ? matchCondition : 'contains';

            for (const option of selectableLbx.options) {
                let optText = option.text.toLowerCase();//.trim();
                for (let line of lines) {
                    line = line.toLowerCase();//.trim();
                    if (!line || line == '')
                        continue;
                    let re = new RegExp(RegExp.escape(line), 'gi');
                    switch (matchCondition) {
                        case 'equals':
                        case 'notequals':
                            if (optText == line)
                                option.selected = true;
                            break;
                        case 'contains':
                        case 'notcontains':
                            if (optText.match(re) || optText.includes(line))
                                option.selected = true;
                            break;
                        case 'startswith':
                            if (optText.startsWith(line))
                                option.selected = true;
                            break;
                        case 'endswith':
                            if (optText.endsWith(line))
                                option.selected = true;
                            break;
                    }
                }

                if (matchCondition == 'notcontains' || matchCondition == 'notequals')
                    option.selected = !option.selected;

                if (option.selected) {
                    $(option).prop('selected', true);
                    $(option).attr('selected', true);
                }
                else {
                    $(option).removeAttr('selected');
                    $(option).prop('selected', null);
                }
            }

            MoveSelectableSelectedListItems(selectableLbx.id, selectedLbx.id, '0', selectedHf.id, sortByVal);

            $('.page-loader-wrapper').hide();

            if (targetModalId)
                $find(targetModalId).hide();

            resetFileCtrl(src.id);

        };

        reader.readAsText(file);
    }
}

function initMultiselectable() {

    $('button.close-modal').on('click', function () {
        if ($(this).data('target-modal'))
            $find($(this).data('target-modal')).hide();
    });

    $('button.close-selectable-file-modal').on('click', function () {
        if ($(this).data('target-file'))
            resetFileCtrl($(this).data('target-file'));
    });

    $('.multiselect-file-search-condition').each(function () {
        let targetFileId = null;
        if ($(this).data('target-file'))
            targetFileId = $(this).data('target-file');

        $(this).find('input[type=radio]')
            .on('click', function (e) {
                setCookie('FileSelectableMatchingFunction', e.target.value);
                if (targetFileId)
                    resetFileCtrl(targetFileId);
            });
    });
}

$(function () {
    initMultiselectable();
});

function multiSelectableEndRequestHandler(sender, args) {
    initMultiselectable();
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(multiSelectableEndRequestHandler);


