/*!
 *
 * Copyright (c) 2021-2024 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    TouchGrid.qml
 * \author  (last)      Dara Navaei
 * \date    (last)      06-Mar-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  14-Apr-2021
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   TouchGrid is a list of touchRect components layed out in a Grid
 */
Item { id: _root
    objectName: "_TouchGrid"

    property var    itemsValue      : []
    property var    itemsText       : []
    property var    itemsUnit       : []

    property var    itemsEnabled    : []
    property var    itemsVisible    : []
    property var    itemsHasImage   : []
    property var    itemsHasLine    : []
    property var    itemsHasIndent  : []
    property var    itemsTouchable  : []
    property bool   touchable       : true

    property int    itemWidth       : 350
    property int    itemHeight      : 50
    property alias  layoutOrder     : _grid.flow
    property alias  rowSpacing      : _grid.rowSpacing
    property alias  colSpacing      : _grid.columnSpacing
    property color  lineColor       : Colors.borderButtonHalfDarker
    property real   lineThickness   : 1.5
    property real   itemsValueLeftMargin    : itemWidth / 2
    property real   itemsUnitLeftMargin     : itemWidth / 4

    property bool   alignCenter     : true

    property int rowCount           : Math.floor( _root.height / itemHeight )
    property int colCount           : itemsText.length > rowCount ? Math.ceil(itemsText.length / rowCount) : 1
    readonly property int titleTopMargin: 110

    readonly property alias itemsVisibleCount : _private.itemsVisibleCount

    signal itemClicked(int vIndex)

    QtObject { id: _private
        property int itemsVisibleCount : 0
    }


    height: parent.height

    onItemsVisibleChanged: {
        let count = 0
        for ( let itemVisible of itemsVisible ) {
            if ( itemVisible ) {
                count++
            }
        }
        _private.itemsVisibleCount = count
    }

    function undef(vValue, vOtherwise) {
        if ( vValue === undefined ) {
            return vOtherwise
        }
        return vValue
    }

    // DEBUG: Rectangle { anchors.fill: _grid }
    Grid { id: _grid
        flow: Grid.TopToBottom
        anchors.centerIn:   _root.alignCenter ? parent      : undefined
        anchors.top     : ! _root.alignCenter ? _root.top   : undefined
        columns         : _root.colCount
        rows            : _root.rowCount
        rowSpacing      : 25
        columnSpacing   : itemsVisibleCount > rowCount ? 50 : 0
        Repeater {
            model       : _root.itemsText
            TouchRect { id: _touchItem
                readonly property int indentMargin : (undef( _root.itemsHasIndent[index], false ) ? 20 : 0)
                clip                : true
                touchable           : undef( _root.itemsTouchable[index], _root.touchable )
                text.anchors.horizontalCenter: undefined
                text.leftPadding    : Variables.minVGap + indentMargin
                text.text           : modelData
                border.width        : 0
                height              : _root.itemHeight
                width               : _root.itemWidth
                radius              : Variables.dialogRadius
                enabled             : undef( _root.itemsEnabled[index], true )
                visible             : undef( _root.itemsVisible[index], true )
                onClicked           : _root.itemClicked(index)
                Text { id           : _itemsValue
                    text            : undef( _root.itemsValue[index], "")
                    font.pixelSize  : _touchItem.pixelSize
                    color           : _touchItem.fgColor
                    anchors.left    : parent.left
                    anchors.leftMargin      : _root.itemsValueLeftMargin
                    anchors.verticalCenter  : _touchItem.verticalCenter
                }
                Text { id: _itemsUnit
                    text            : undef( _root.itemsUnit[index], "" )
                    font.pixelSize  : _touchItem.pixelSize
                    color           : _touchItem.fgColor
                    anchors.left    : parent.left
                    anchors.leftMargin      : _root.itemsUnitLeftMargin
                    anchors.verticalCenter  : _touchItem.verticalCenter
                }
                Image { id: _image
                    visible         : undef( _root.itemsHasImage[index] , true )
                                      &&     _touchItem.touchable
                                      &&     _touchItem.enabled
                                      &&   ! _touchItem.isPressed
                                      &&     _root.itemsText[index]
                    anchors.right           : _touchItem.right
                    anchors.verticalCenter  : _touchItem.verticalCenter
                    width           : Variables.arrowWidth
                    height          : Variables.arrowHeight
                    source          : "qrc:/images/iArrowRight"
                }
                Line { id: _line
                    visible         : undef( _root.itemsHasLine[index], true )
                                   // &&   ! _touchItem.isPressed
                                      &&     _root.itemsText[index]
                                   // &&     _touchItem.enabled
                    thickness       : _root.lineThickness
                    color           : _touchItem.enabled ? _root.lineColor : Colors.borderDisableButton
                    anchors.left    : _touchItem.left
                    anchors.right   : _touchItem.right
                    anchors.bottom  : _touchItem.bottom
                }
            }
        }
    }
}
