const prevNextLensUIUtils = {

    showHideClearBtn: function showHideClearBtn() {
        // show or hide the Clear button as necessary

        log('showHideClearBtn: enter');

        const count = lensesToFindStorageUtils.count();
        // how many lenses are selected?

        const show = (count > 0);
        // if any, show the button

        if (show) {
            $('#divClearBtn').show();
        }
        else {
            $('#divClearBtn').hide();
        }

        log('showHideClearBtn: leave');
    },

    clearSelectedListItem: function clearSelectedListItem() {
        log('clearSelectedListItem: enter');

        if (findLensComponent === null) {
            // if the list has not yet been initialized

            log('clearSelectedListItem: findLensComponent is NULL, so we will leave early without clearing the selection');
            log('clearSelectedListItem: leave');
            return;
        }

        if (findLensList === null) {
            // if the list has not yet been created

            log('clearSelectedListItem: findLensList is NULL, so we will leave early without clearing the selection');
            log('clearSelectedListItem: leave');
            return;
        }

        var selItems = [];

        log('clearSelectedListItem: atc findLensList.dxList');

        var list = findLensList.dxList('instance');
        // get a reference to the dxList

        log('clearSelectedListItem: bf findLensList.dxList');

        list.option('selectedItems', selItems);
        list.repaint();

        log('clearSelectedListItem: leave');
    },

    selectItemInList: function selectItemInList(lensName) {
        log('selectItemInList: enter');
        log('selectItemInList: lensName=' + lensName);

        if (findLensComponent === null) {
            // if the list has not yet been initialized

            log('selectItemInList: findLensComponent is NULL, so we will leave early without selecting an item');
            log('selectItemInList: leave');
            return;
        }

        var selItems = [];
        selItems.push(lensName);

        var list = findLensList.dxList('instance');
        // get a reference to the dxList

        list.option('selectedItems', selItems);
        list.repaint();

        log('selectItemInList: leave');
    },

    show: function show() {
        log('prevNextLensUIUtils.show: enter');

        $('#divPrevLens').show();
        $('#divNextLens').show();

        log('prevNextLensUIUtils.show: leave');
    },

    hide: function hide() {
        log('prevNextLensUIUtils.hide: enter');

        $('#divPrevLens').hide();
        $('#divNextLens').hide();

        log('prevNextLensUIUtils.hide: leave');
    },

    handleClearClickAsync: async function handleClearClickAsync() {
        log('handleClearClickAsync: enter');

        if (findLensComponent === null) {
            // if the list has not yet been initialized

            log('handleClearClickAsync: findLensComponent is NULL, so we will leave early without clearing');
            log('handleClearClickAsync: leave');
            return;
        }

        this.clearSelectedListItem();
        // clear the selected item in the list widget

        this.hide();
        // hide the prev and next buttons

        lensesToFindStorageUtils.clear();
        // clear the stored id of the selected lens

        this.showHideClearBtn();
        // hide the Clear button

        noTabsCardExpStorageUtils.clear();
        tabsCardExpStorageUtils.clear();
        // clear the expanded flags for all lenses

        await lensUIUtils.displayCurrentLensesAsync();
        // display the current set of lenses

        await uiUtils.handleLensCountChangedAsync();

        expandCollapseBtnUtils.showHideExpandCollapseBtnsAndCol();
        // show or hide the expand and collapse all buttons and their parent column as necessary

        log('handleClearClickAsync: leave');
    },

    handlePrevClickAsync: async function handlePrevClickAsync() {
        log('handlePrevClickAsync: enter');

        if (findLensComponent === null) {
            // if the list has not yet been initialized

            log('handlePrevClickAsync: findLensComponent is NULL, so we will leave early without selecting the previous item');
            log('handlePrevClickAsync: leave');
            return;
        }

        const selLensId = lensesToFindStorageUtils.getFirstLensId();
        // get the id of the selected lens, or -1 if none

        if (selLensId < 1)
            return;

        const prevLensName = lensUtils.getPrevLensName(selLensId);
        // get the name of the lens before the selected lens in the alphabet

        if (prevLensName.length < 1)
            return;

        const prevLensId = lensUtils.getLensIdByName(prevLensName);

        if (prevLensId > 0) {

            lensesToFindStorageUtils.clear();
            // for now, every lens the user finds clears any previous one

            lensesToFindStorageUtils.addToLensIds(prevLensId);

            findLensComponent.option("value", prevLensName);
            // display the lens name in the combo box

            prevNextLensUIUtils.selectItemInList(prevLensName);
            // select the lens name in the list contained in the drop down

            await lensUIUtils.displayCurrentLensesAsync();
            // display the current set of lenses

            await uiUtils.handleLensCountChangedAsync();
        }

        log('handlePrevClickAsync: leave');
    },

    handleNextClickAsync: async function handleNextClickAsync() {
        log('handleNextClickAsync: enter');

        if (findLensComponent === null) {
            // if the list has not yet been initialized

            log('handleNextClickAsync: findLensComponent is NULL, so we will leave early without selecting the next item');
            log('handleNextClickAsync: leave');
            return;
        }

        const selLensId = lensesToFindStorageUtils.getFirstLensId();
        // get the id of the selected lens, or -1 if none

        log('handleNextClickAsync: selLensId=' + selLensId);

        if (selLensId < 1) {
            log('handleNextClickAsync: NO selected lens, so we give up');
            log('handleNextClickAsync: leave');
            return;
        }

        const selLensName = lensUtils.getLensName(selLensId);
        // get the name of the selected lens

        log('handleNextClickAsync: selLensName=' + selLensName);

        const nextLensName = lensUtils.getNextLensName(selLensId);
        // get the name of the lens after the selected lens in the alphabet

        if (nextLensName.length < 1) {
            log('handleNextClickAsync: failed to get lens name, so we give up');
            log('handleNextClickAsync: leave');
            return;
        }

        log('handleNextClickAsync: nextLensName=' + nextLensName);

        const nextLensId = lensUtils.getLensIdByName(nextLensName);

        log('handleNextClickAsync: nextLensId=' + nextLensId);

        if (nextLensId < 1) {
            log('handleNextClickAsync: failed to get next lens id, so we give up');
            log('handleNextClickAsync: leave');
            return;
        }

        log('handleNextClickAsync: we found the id of the lens to select, so we will select it');

        lensesToFindStorageUtils.clear();
        // for now, every lens the user finds clears any previous one

        lensesToFindStorageUtils.addToLensIds(nextLensId);

        log('handleNextClickAsync: setting lens name displayed in combo box to ' + nextLensName);

        findLensComponent.option("value", nextLensName);
        // display the lens name in the combo box

        prevNextLensUIUtils.selectItemInList(nextLensName);
        // select the lens name in the list contained in the drop down

        await lensUIUtils.displayCurrentLensesAsync();
        // display the current set of lenses

        await uiUtils.handleLensCountChangedAsync();

        log('handleNextClickAsync: leave');
    }
};