/*!
 *
 * Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    Footer.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      06-Oct-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"

/*!
 * \brief   Denali project Footer section which gives all the children same width with same spacing, aligned in center.
 * \details It's been created because the row layout is always keeping it's children aligned at left and not at center.
 *          This mainly being used to keep buttons on a screen alinged with the same width and space between them and horisontalley centered
 *          on the bottom of the screen (footer),
 *          and with enough bottom marging to give room to the alarm bar.
 */
Row { id: _root
    property int childrenWidth  : _root.width / (childrenCount * 2 + 1 )
    property int childrenCount  : children.length

    QtObject { id: _private
        property int spacing    : 0
    }

    function update() {
        // check there is a child
        let count = childrenCount
        if ( ! count ) return

        let width   = childrenWidth

        let visibleCount = 0
        for (let i = 0; i < count; i++) {
            //DEBUG: console.debug(children[i].text.text)
            if ( children[i].visible ) {

                visibleCount += 1
                if (children[i].width !== width) {
                    children[i].width   = width
                }
            }
            else {
                children[i].width   = 0
            }
        }
        let spacing = (_root.width - (width * visibleCount)) / (visibleCount + 1)
        //DEBUG: console.debug( " ~~~~~~~~~~ ", count, visibleCount, spacing)
        _private.spacing = spacing
    }

    // this code will run once and will not run for each child,
    // that's becuase the available property to add children is the children property
    // and that property can only change by assigning a list to it.
    // so it happens once with no performance issue of multiple redundant call.
    onVisibleChanged    : update()

    spacing             : _private.spacing
    rightPadding        : _private.spacing
    leftPadding         : _private.spacing
    width               : parent.width
    anchors.bottom      : parent.bottom
    anchors.bottomMargin: Variables.notificationHeight + Variables.minVGap
}
