/*!
 *
 * Copyright (c) 2021-2022 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    TreatmentFlowBase.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      28-Sep-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  15-May-2021
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   the parent page of the treatment flow screens
 */
ScreenItem { id: _root
    objectName: "_TreatmentFlowBase"

    property alias  informationText : _information.text
    property alias  reasonText      : _notification.text
    property alias  header          : _titleBar
    property alias  title           : _titleText
    property alias  footer          : _footer

    property bool   instructionBased        : false
    readonly property alias instruction     : _instructionLoader.item
    property string instructionlocation     : ""
    property var    instructionStepNames    : []
    property var    instructionStepImages   : []
    readonly property bool instructionIsLast: instruction ? instruction.lastStep : true

    property bool   hasTimeCircle           : false
    readonly property alias timeCircle      : _timeCircleLoader.item
    property bool   timeCircleIsChecked     : false
    property int    timeCircleMinimum       : 0
    property int    timeCircleMaximum       : 0
    property int    timeCircleProgressValue : 0
    property int    timeCircleTimeTextValue : 0

    property bool   hasCheckList            : false
    readonly property alias checkList       : _checkListLoader.item
    property var    checkListStepNames      : []
    property int    checkListCompleteMargin : 50
    property bool   checkListTimeVisible    : false
    property int    checkListTimeSeconds    : 0


    property string completeText            : ""

    property bool   isComplete              : false

    readonly property int titleTopMargin: 110

    signal    backClicked()
    signal confirmClicked()

    // vvvvvvvvvvvvvvvvvvvvvvvvv   HEADER  vvvvvvvvvvvvvvvvvvvvvvvvv //
    StepNavigationTitleBar { id: _titleBar
        stepIndex       : stackStepIndex // shall have a definition in the parent stack
        anchors.top     : _root.top
        anchors.horizontalCenter : parent.horizontalCenter
        width           : _root.width
        confirmEnabled  : instruction ? instruction.lastStep : true
        stepNames       : [ ]
        onBackClicked   : _root.backClicked()
        onConfirmClicked: _root.confirmClicked()
    }

    Text { id: _titleText
        visible: ! instructionBased
        anchors {
            top         : _root.top
            topMargin   : titleTopMargin
            horizontalCenter: parent.horizontalCenter
        }
        text            : qsTr("TreatmentFlowBase")
        color           : Colors.textMain
        font.pixelSize  : Fonts.fontPixelTitle
    }

    // ^^^^^^^^^^^^^^^^^^^^^^^^^   HEADER  ^^^^^^^^^^^^^^^^^^^^^^^^^ //

    // vvvvvvvvvvvvvvvvvvvv Optional Components vvvvvvvvvvvvvvvvvvvv //
    Loader { id: _instructionLoader
        readonly property int outerHMargin  : 30
        readonly property int outerVMargin  : 15
        active              : _root.instructionBased
        anchors {
            top             : title.visible ? title.bottom : title.top
            bottom          : footer.top
            left            : parent.left
            right           : parent.right
            leftMargin      : outerHMargin
            rightMargin     : outerHMargin
            topMargin       : outerVMargin
            bottomMargin    : outerVMargin
        }
        sourceComponent     : InstructionView { id: _instructionView
            label           : title.visible ? "" : _root.title.text
            location        : _root.instructionlocation
            stepNames       : _root.instructionStepNames
            stepImages      : _root.instructionStepImages
        }
    }

    Loader { id : _timeCircleLoader
        active              : _root.hasTimeCircle

        anchors.top         : _root.hasCheckList ? title.bottom : undefined
        anchors.topMargin   : _root.hasCheckList ? 25           : 0
        anchors.horizontalCenter: _root.horizontalCenter
        anchors.centerIn    : ! _root.hasCheckList ? parent : undefined
        sourceComponent     : TimeCircle { id: _timeCircle
            isChecked       : _root.isComplete
            minimum         : _root.timeCircleMinimum
            maximum         : _root.timeCircleMaximum
            progressValue   : _root.timeCircleProgressValue
            timeTextValue   : _root.timeCircleTimeTextValue
            thickness           : _root.hasCheckList ? 1 : 2
            diameter            : _root.hasCheckList ? Variables.progressCircleDiameterSmall : Variables.progressCircleDiameterNormal
            timeTextPixelSize   : _root.hasCheckList ? Fonts.fontPixelCirclProgressTimeSmall : Fonts.fontPixelCirclProgressTimeNormal
        }
    }

    Loader { id: _checkListLoader
        active              :   _root.hasCheckList
        anchors.top         :   _root.hasTimeCircle ? _timeCircleLoader.bottom : undefined
        anchors.topMargin   :   _root.hasTimeCircle ? 25                       : 0
        anchors.horizontalCenter: _root.horizontalCenter
        anchors.centerIn    : ! _root.hasTimeCircle ? parent                   : undefined
        width               : Variables.checkListViewItemWidth
        height              : Variables.checkListViewItemHeight * _root.checkListStepNames.length
        sourceComponent     : CheckListView { id: _checkListView
            completeVisible : _root.isComplete
            completeText    : _root.completeText
            stepNames       : _root.checkListStepNames
            completeMargin  : _root.checkListCompleteMargin
            timeVisible     : _root.checkListTimeVisible
            timeSeconds     : _root.checkListTimeSeconds
        }
    }

    // ^^^^^^^^^^^^^^^^^^^^ Optional Components ^^^^^^^^^^^^^^^^^^^^ //

    Footer { id: _footer }

    NotificationBarSmall { id: _information
        visible     : text
        color       : Colors.transparent
        textColor   : Colors.white
        imageSource : ""
        text        : ""
    }

    NotificationBarSmall { id: _notification }

    Connections { target: vSettings
        onInstructionsChanged : {
            if ( instructionBased ) {
                let  stepName = header.stepNames[stackStepIndex]
                if ( stepName === undefined ) stepName = ""
                let group = vSettings.groupFormat.arg(stepName).arg(title.text)
                let instructionsGroup    = vSettings.instructions[group]
                if ( instructionsGroup !== undefined ) {
                    // DEBUG :
                    // console.debug(" 00000 ", group, settingsGroup, settingsCategory.groups)
                    _root.instructionlocation   = instructionsGroup.location
                    _root.instructionStepNames  = instructionsGroup.keys
                    _root.instructionStepImages = instructionsGroup.values
                }
            }
        }
    }

    onVisibleChanged: {
        if (checkList)
            checkList.resetItems()
        if (instruction)
            instruction.currentStepIndex = 0
        _notification.text = ""
        if (visible) {
            _mainMenu.hidden = true
        }
    }
}
