/*!
 *
 * 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    SettingsVolumeBrightness.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      07-Jun-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  06-Jun-2021
 *
 */

// Qt
import QtQuick                  2.12
import QtQuick.Controls         2.12
import Qt.labs.folderlistmodel  2.12


// TODO: Add the fileCopy function to have control over the copy
// TODO: Add cancel
// TODO: Add Service and Treatment log export and filter
// TODO: Add selecction export
// TODO: Add error display

// Project
import Gui.Actions 0.1

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

/*!
 * \brief   SettingsExportLogs is used to Export logs, and display the SD-Card and USB device informaiton and list of files.
 */
SettingsBase                                                { id: _root
    itemIndex                                               : SettingsStack.ExportLogs
    confirmVisible                                          : false

    USBButton                                               { id: _usbEjectButton
        width                                               : 155
        height                                              :  50
        anchors.right                                       : _root.right
        anchors.top                                         : _root.top
        anchors.margins                                     : Variables.headerButtonsMargin
    }

    Row                                                     { id : _contentRect
        spacing                                             : Variables.minVGap
        anchors.fill                                        : parent
        anchors.topMargin                                   : parent.   topMarginContent - 25 // move 25 up
        anchors.bottomMargin                                : parent.bottomMarginContent
        anchors.rightMargin                                 : parent. sidesMarginContent
        anchors.leftMargin                                  : parent. sidesMarginContent
        anchors.horizontalCenter                            : parent.horizontalCenter

        readonly property int columnWidthProgress           : 350
        readonly property int columnWidthFolder             : 420
        readonly property int columnWidthFileName           : 285 // 285 best combination
        readonly property int columnWidthFileSize           : 105 // 105 best combination

        Column { id                                         : _progressColumn
            property int progressWidth                      : 125
            property int progressHeight                     : height - _logTypeCombo.height - _logTypeExportButton.height

            spacing                                         : 10
            width                                           : _contentRect.columnWidthProgress
            height                                          : parent.height
            anchors.verticalCenter                          : parent.verticalCenter

            Row { id                                        : _logTypeRow
                anchors.left                                : parent.left
                width                                       : parent.width
                height                                      : 50
                spacing                                     : _logTypeCombo.width - _logTypeExportButton.width

                // FIXME: This combobox needs to be a global Component
                ComboBox { id                               : _logTypeCombo
                    currentIndex                            : 0
                    displayText                             : currentText
                    font.pixelSize                          : Fonts.fontPixelTextRectExtra
                    width                                   : parent.width // 2
                    height                                  : parent.height
                    padding                                 : 10
                    model                                   : [
                        qsTr("Application"                  ),
                        qsTr("Service"                      ),
                        qsTr("Treatment"                    )
                    ]

                    background                              : Rectangle {
                        color                               : Colors.transparent
                        border.color                        : Colors.borderButton
                        radius                              : Variables.dialogRadius
                    }

                    delegate: ItemDelegate                  { id : _logTypeDelegate
                        width                               : _logTypeCombo.width // + anchors.margins - 2 // 2 is the border width
                        height                              : 50
                        contentItem: Text                   {
                            text                            : modelData
                            color                           : Colors.textMain
                            font                            : _logTypeCombo.font
                            padding                         : 10
                            verticalAlignment               : Text.AlignVCenter
                        }
                        background                          : Rectangle {
                            anchors.fill                    : parent
                            anchors.margins                 : 10 / 2
                            visible                         : _logTypeDelegate.down || _logTypeDelegate.highlighted || _logTypeDelegate.visualFocus
                            color                           : _logTypeDelegate.down ? Colors.backgroundButtonSelect    :
                                                                                      Colors.backgroundButtonSelectDark
                        }
                        highlighted                         : _logTypeCombo.highlightedIndex === index
                    }
                    popup: Popup {
                        y                                   : _logTypeCombo.height
                        x                                   :                     - _logTypeCombo.anchors.leftMargin
                        width                               : _logTypeCombo.width + _logTypeCombo.anchors.leftMargin
                        implicitHeight                      : contentItem.implicitHeight
                        contentItem                         : ListView {
                            clip                            : true
                            implicitHeight                  : contentHeight
                            currentIndex                    : _logTypeCombo.highlightedIndex
                            model                           : _logTypeCombo.popup.visible ? _logTypeCombo.delegateModel : null
                        }
                        background: Rectangle               {
                            color                           : Colors.backgroundMain
                            border.color                    : Colors.borderButton
                            radius                          : Variables.dialogRadius
                        }
                    }
                }
            }
            Column {
                width                                       : _progressColumn.progressWidth
                height                                      : _progressColumn.progressHeight - 2 * _progressColumn.spacing
                Item { id                                   : _SDC_item
                    width                                   : _progressColumn.progressWidth
                    height                                  : parent.height / 2

                    SDCProgressItem                         { id: _SDC_progressItem
                        thickness                           : 2
                        displayInformation                  : false
                        anchors.fill                        : parent
                    }
                    Label {
                        anchors.left                        : _SDC_progressItem.right
                        anchors.leftMargin                  : _progressColumn.spacing
                        anchors.verticalCenter              : parent.verticalCenter
                        text                                : qsTr("SD-Card") + " (MB)\n" + ("Free : %1\nTotal: %2").arg( Variables.sizeConverted( _GuiView.sdAvail, 1000, 3) ).arg( Variables.sizeConverted( _GuiView.sdTotal, 1000, 3) )
                    }
                    Label {
                        anchors.fill                        : parent
                        verticalAlignment                   : Text.AlignVCenter
                        horizontalAlignment                 : Text.AlignHCenter
                        text                                : _SDC_progressItem.percent
                    }
                }
                Item { id                                   : _USB_item
                    width                                   : _progressColumn.progressWidth
                    height                                  : parent.height / 2

                    USBProgressItem                         { id: _USB_progressItem
                        thickness                           : 2
                        displayInformation                  : false
                        anchors.fill                        : parent
                    }
                    Label {
                        anchors.left                        : _USB_progressItem.right
                        anchors.leftMargin                  : _progressColumn.spacing
                        anchors.verticalCenter              : parent.verticalCenter
                        text                                : qsTr("USB Drive") + " (MB)\n" + ("Free : %1\nTotal: %2").arg( Variables.sizeConverted( _GuiView.usbAvail, 1000, 3) ).arg( Variables.sizeConverted( _GuiView.usbTotal, 1000, 3) )
                    }
                    Label {
                        anchors.fill                        : parent
                        verticalAlignment                   : Text.AlignVCenter
                        horizontalAlignment                 : Text.AlignHCenter
                        text                                : _USB_progressItem.percent
                    }
                }
            }
            ExportButton { id                               : _logTypeExportButton
                width                                       : parent.width
                height                                      : Variables.touchRectHeight
                radius                                      : Variables.touchRectRadius
                border.width                                : Variables.borderWidth
                onClicked                                   : _GuiView.doExportLog()
            }

        }
        Column { id                                         : _sdcFolderColumn
            spacing                                         : 5
            width                                           : _contentRect.columnWidthFolder
            height                                          : parent.height
            anchors.verticalCenter                          : parent.verticalCenter

            Rectangle { id                                  : _sdcFolderRectangle
                color                                       : Colors.transparent
                border.color                                : Colors.borderButton
                radius                                      : Variables.dialogRadius
                anchors.left                                : parent.left
                width                                       : parent.width
                height                                      : parent.height

                ScrollBar {
                    anchors.fill                            : _sdcFolderView
                    flickable                               : _sdcFolderView
                }

                ListView { id                               : _sdcFolderView
                    clip                                    : true
                    anchors.fill                            : parent
                    anchors.margins                         : 10
                    spacing                                 : 5
                    FolderListModel { id                    : _sdcFolderModel
                        showDirs                            : false
                        sortField                           : FolderListModel.Time
                        folder                              : "file:///media/sd-card/log"   // FIXME: ther has to be a View for this which also get changed by log type.
                        nameFilters                         : ["*.log"]                     // FIXME: ther has to be a View for this which also get changed by log type.
                    }
                    Component { id                          : _sdcFileDelegate
                        Row { id                            : _sdcFileRow
                            width                           : parent.width
                            height                          : 40
                            Text { id                       : _sdcFileNameText
                                width                       : _contentRect.columnWidthFileName
                                text                        : fileName
                                color                       : Colors.textMain
                                font.pixelSize              : Fonts.fontPixelTextRectExtra
                                verticalAlignment           : Text.AlignVCenter
                                horizontalAlignment         : Text.AlignLeft
                            }
                            Rectangle {
                                color: Colors.borderButtonUnselected
                                width                       : 1
                                height                      : parent.height + _usbFolderColumn.spacing
                            }
                            Text { id                       : _sdcFileSizeText
                                width                       : _contentRect.columnWidthFileSize
                                text                        : Variables.sizeConverted( fileSize, 1000, 3)
                                color                       : Colors.textMain
                                font.pixelSize              : Fonts.fontPixelTextRectExtra
                                verticalAlignment           : Text.AlignVCenter
                                horizontalAlignment         : Text.AlignRight
                            }
                        }
                    }
                    model                                   : _sdcFolderModel
                    delegate                                : _sdcFileDelegate
                }
            }
        }

        Column { id                                         : _usbFolderColumn
            // FIXME: there has to be a View for this, and the timer should be removed and an event driven signal should be implemented there.
            function updateModel() {
                _usbFolderModel.folder                      = "file:///media/usb/log"   // FIXME: there has to be a View for this which also get changed by log type.
                _usbFolderModel.nameFilters                 = ["*.log"]                 // FIXME: there has to be a View for this which also get changed by log type.
            }
            function clearModel() {
                _usbFolderModel.folder                      = ""
                _usbFolderModel.nameFilters                 = []
            }

            // FIXME: there has to be a View for this, and the timer should be removed and an event driven signal should be implemented there.
            Timer { id: _usbUpdate
                interval    : 500
                repeat      : true
                running     : _GuiView.exportRunning
                onTriggered : {
                    _usbFolderColumn. clearModel()
                    _usbFolderColumn.updateModel()
                    console.debug("Updating usb")
                }
            }

            Connections { target: _GuiView
                onDidUSBDriveUmount : _usbFolderColumn. clearModel()
                onDidUSBDriveRemove : _usbFolderColumn. clearModel()
                onDidExportLog      : _usbFolderColumn. clearModel()
                onDidUSBDriveMount  : _usbFolderColumn.updateModel()
                onDidExport         : _usbFolderColumn.updateModel()
            }

            spacing                                         : 5
            width                                           : _contentRect.columnWidthFolder
            height                                          : parent.height
            anchors.verticalCenter                          : parent.verticalCenter

            Rectangle { id                                  : _usbFolderRectangle
                color                                       : Colors.transparent
                border.color                                : Colors.borderButton
                radius                                      : Variables.dialogRadius
                anchors.left                                : parent.left
                width                                       : parent.width
                height                                      : parent.height

                ScrollBar {
                    anchors.fill                            : _usbFolderView
                    flickable                               : _usbFolderView
                }

                ListView { id                               : _usbFolderView
                    clip                                    : true
                    anchors.fill                            : parent
                    anchors.margins                         : 10
                    spacing                                 : 5
                    FolderListModel { id                    : _usbFolderModel // FIXME: I don't like this model, it's too lazy and I don't have control over it. There has to be a Model for this.
                        showDirs                            : false
                        sortField                           : FolderListModel.Time
                        folder                              : "file:///media/usb"
                        nameFilters                         : ["*"]
                    }
                    Component { id                          : _usbFileDelegate
                        Row { id                            : _usbFileRow
                            width                           : parent.width
                            height                          : 40
                            Text { id                       : _usbFileNameText
                                width                       : _contentRect.columnWidthFileName
                                text                        : fileName
                                color                       : Colors.textMain
                                font.pixelSize              : Fonts.fontPixelTextRectExtra
                                verticalAlignment           : Text.AlignVCenter
                                horizontalAlignment         : Text.AlignLeft
                            }
                            Rectangle {
                                color: Colors.borderButtonUnselected
                                width: 1
                                height: parent.height + _usbFolderColumn.spacing
                            }
                            Text { id                       : _usbFileSizeText
                                width                       : _contentRect.columnWidthFileSize
                                text                        : Variables.sizeConverted( fileSize, 1000, 3)
                                color                       : Colors.textMain
                                font.pixelSize              : Fonts.fontPixelTextRectExtra
                                verticalAlignment           : Text.AlignVCenter
                                horizontalAlignment         : Text.AlignRight
                            }
                        }
                    }
                    model                                   : _usbFolderModel
                    delegate                                : _usbFileDelegate
                }
            }
        }
    }
}
