// findWhichGroupUtils
//  updateBtns
//      read from storage to determine which button to select, then select that button
//  showGroup
//  hideGroup
//  showGroupIfNec
//  handleAllBtnClick
//  handleBkBtnClickAsync

const findWhichGroupUtils = {

    updateBtns: function updateBtns() {
        // update the style of the "find which" buttons to indicate which is selected
        // reads data from searchStateUtils

        log('findWhichGroupUtils.updateBtns: enter');

        const findingAll = findWhichGroupStateUtils.getFindAll();
        // are we set to find all lenses?

        log('findWhichGroupUtils.updateBtns: findingAll=' + findingAll);

        const elemAll = $('#labAll');
        // get the label element for "all"

        const elemBk = $('#labBk');
        // get the label element for "bookmarked"

        if (findingAll) {
            // if we are finding all lenses

            elemAll.removeClass('btn-outline-info');
            // remove the outline class

            elemAll.addClass('btn-info');
            // add the filled class

            elemBk.removeClass('btn-info');
            // remove the filled class

            elemBk.addClass('btn-outline-info');
            // add the outline class
        }
        else {
            // if we are finding bookmarked lenses

            elemAll.removeClass('btn-info');
            // remove the filled class

            elemAll.addClass('btn-outline-info');
            // add the outline class

            elemBk.addClass('btn-info');
            // add the filled class

            elemBk.removeClass('btn-outline-info');
            // remove the outline class
        }

        log('findWhichGroupUtils.updateBtns: leave');
    },

    showGroup: function showGroup() {
        log('findWhichGroupUtils.showGroup: enter');

        const theElem = $('#divFindWhichBtnsNav');
        // get a reference to the element

        const value = theElem.css('display');
        // get the current value of the display attribute

        if (value === 'flex') {
            // if the group is already visible

            log('findWhichGroupUtils.showGroup: group is already visible, so we are done');
            log('findWhichGroupUtils.showGroup: leave');
            return;
        }

        theElem.addClass("d-flex");
        theElem.addClass("flex-row");
        theElem.addClass("flex-wrap");
        // add the classes that we removed when we hid the element

        theElem.css('display', '');
        // show it

        topBtnsUIUtils.showHideTopBtnsHR();
        // show or hide the top buttons horizontal rule

        log('findWhichGroupUtils.showGroup: leave');
    },

    hideGroup: function hideGroup() {
        log('findWhichGroupUtils.hideGroup: enter');

        // calling .hide on #divFindWhichBtnsNav doesn't hide it.
        // something about the two classes column and col-auto prevent this approach from working

        const theElem = $('#divFindWhichBtnsNav');

        theElem.removeClass("d-flex");
        theElem.removeClass("flex-row");
        theElem.removeClass("flex-wrap");

        theElem.css('display', 'none');
        // hide the "which to find" group

        topBtnsUIUtils.showHideTopBtnsHR();
        // show or hide the top buttons horizontal rule

        log('findWhichGroupUtils.hideGroup: leave');
    },

    showGroupIfNec: function showGroupIfNec() {
        const logHeader = 'findWhichGroupUtils.showGroupIfNec: ';

        log(logHeader + 'enter');

        const findCount = lensesToFindStorageUtils.count();
        // how many lenses did the user explicitly find?

        log(logHeader + 'findCount=' + findCount);

        if (findCount > 0) {
            // if any

            findWhichGroupUtils.hideGroup();

            lensUIUtils.showHideFindWhichDisplayByColAsNec();
            // show or hide the parent column as necessary

            log(logHeader + 'leave')
            return;
        }

        const bkCount = bookmarkStorageUtils.count();
        // how many lenses are bookmarked?

        log(logHeader + 'bkCount=' + bkCount);

        if (bkCount > 0) {
            findWhichGroupUtils.showGroup();

            lensUIUtils.showHideFindWhichDisplayByColAsNec();
            // show or hide the parent column as necessary

            log(logHeader + 'leave')
            return;
        }

        const showingAll = findWhichGroupStateUtils.getFindAll();
        // are we set to find all lenses?
        // true => find all
        // false => find bookmarked

        log(logHeader + 'showingAll=' + showingAll);

        if (showingAll) {
            // if there are no bookmarks and we are finding all lenses (not only bookmarked)

            findWhichGroupUtils.hideGroup();
            // hide the find which button group
        }
        else {
            // if no lenses are bookmarked, but we are showing bookmarked lenses
            // the list will be empty, which may confuse the user

            findWhichGroupUtils.hideGroup();
            // hide the find which button group

            findWhichGroupStateUtils.setFindAll(true);
            // switch back to finding all
        }

        lensUIUtils.showHideFindWhichDisplayByColAsNec();
        // show or hide the parent column as necessary

        log(logHeader + 'leave');
    },

    isFindWhichVisible: function isFindWhichVisible() {
        const logHeader = 'findWhichGroupUtils.isFindWhichVisible: ';
        log(logHeader + 'enter');

        const rowJQ = $('#divFindWhichBtnsNav');

        const displayValue = rowJQ.css('display');
        // get the value of the display item

        const visible = (displayValue !== 'none');
        // if not hidden it's visible

        log(logHeader + 'visible=' + visible);
        log(logHeader + 'leave');

        return visible;
    },

    handleAllBtnClickAsync: async function handleAllBtnClickAsync() {
        // handle a click in the "find all" button

        log('handleAllBtnClickAsync: enter');

        findWhichGroupStateUtils.setFindAll(true);
        // find all

        findWhichGroupUtils.updateBtns();
        // update buttons to indicate which is selected

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

        await uiUtils.handleLensCountChangedAsync();

        log('handleAllBtnClickAsync: leave');
    },

    handleBkBtnClickAsync: async function handleBkBtnClickAsync() {
        // handle a click in the "find bookmarked" button

        log('handleBkBtnClickAsync: enter');

        const count = bookmarkStorageUtils.count();
        // get the number of bookmarked lenses

        log('handleBkBtnClick: count=' + count);

        findWhichGroupStateUtils.setFindAll(false);
        // find all = false => find bookmarked

        findWhichGroupUtils.updateBtns();
        // update buttons to indicate which is selected

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

        await uiUtils.handleLensCountChangedAsync();

        log('handleBkBtnClickAsync: leave');
    },

    handleFindWhichGroupClickAsync: async function handleFindWhichGroupClickAsync(elemId) {
        // handle a click in an option button in the "which lenses to find" group
        // elemId: the DOM element id of the clicked input element

        log('handleFindWhichGroupClickAsync: enter');
        log('handleFindWhichGroupClickAsync: elemId=' + elemId);

        switch (elemId) {
            case "inpOptionAll":

                await findWhichGroupUtils.handleAllBtnClickAsync();

                break;

            case "inpOptionBk":

                await findWhichGroupUtils.handleBkBtnClickAsync();

                break;
        }

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