import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import QtQml 2.14
import QtQml.Models 2.14
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.3
import QtQuick.Window 2.2

Rectangle {
    property QtObject trackBuffer: modelData
    id : defaultTrackBody
    clip: true
    width: multitrack.width 
    height: trackHeight
    color: activePalette.dark
    border.color: activePalette.button
    border.width: 1
    Repeater {
        model: modelData.clips
        delegate: Rectangle {
            Rectangle {
                clip: true
                anchors.left: parent.right
                anchors.leftMargin: Math.max(0, Math.min(1, (modelData.beginSeconds - rulerTimeBegin) / rulerTimeDuration)) * (multitrack.width - headWidth)
                width: Math.max(0, Math.min(1, (modelData.endSeconds - rulerTimeBegin) / rulerTimeDuration)) * (multitrack.width - headWidth) - anchors.leftMargin
                height: trackHeight
                color: activePalette.midlight
                border.color: activePalette.light
                border.width: 0
                Rectangle {
                    anchors.left: parent.left
                    color: activePalette.highlight
                    width: timeCursorCol.x - headWidth - parent.x
                    height: parent.height
                }
                Text {
                    visible: modelData.running
                    anchors.right: parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    text: "loading"
                    
                }
                MouseArea {
                    id: forClick
                    anchors.fill: parent
                    acceptedButtons: Qt.RightButton
                    propagateComposedEvents: true
                    property bool toolbar: false
                    onClicked: {
                        clipMenu.popup()         
                    }                
                } 

                // MouseArea {
                //     id: forDoubleClick
                //     anchors.fill: parent
                //     acceptedButtons: Qt.LeftButton
                //     propagateComposedEvents: true
                //     property bool toolbar: false
                //     onDoubleClicked: {
                //         modelData.setToolbarEnabled(toolbar)
                //         toolbar = !toolbar                   
                //     }    
                // } 
                DefaultMenu {
                    parent: defaultTrackBody
                    id : clipMenu
                    title: "Clip Menu"
                    DefaultMenuItem {
                        text: "Cut by Time"
                        
                        onClicked: {
                            beginTime.text = timeline.dateMsTimeString(timeline.beginSeconds)
                            endTime.text = timeline.dateMsTimeString(timeline.endSeconds)
                            cutWindow.show()
                        }
                        Window {
                            id: cutWindow
                            title :"Cut by Time"
                            width: cutByTimeColumn.width + 20
                            height: cutByTimeColumn.height
                            color: activePalette.window
                            flags:  Qt.Dialog 
                                    | Qt.WindowSystemMenuHint | Qt.WindowTitleHint 
                                    | Qt.WindowCloseButtonHint | Qt.WindowOkButtonHint 
                                    | Qt.FrameWindowHint

                            visible: true
                            modality: Qt.NonModal 
                            ColumnLayout {
                                id: cutByTimeColumn
                                x: 10
                                spacing: 5
                                Rectangle {
                                    height: 5 
                                }
                                RowLayout {
                                    Rectangle {
                                        width: 120
                                        height: 32
                                        color: "transparent"
                                        Label {
                                            text: "Start Timestamp "
                                            color: activePalette.text
                                            
                                            anchors.verticalCenter: parent.verticalCenter
                                        }
                                    }
                                    DefaultButton {
                                        text: "Set"
                                        implicitWidth: 60
                                        implicitHeight: 32
                                        onClicked: beginTime.text = timeline.dateMsTimeString(timeline.cursorSeconds)   
                                        ToolTip {
                                            visible: parent.hovered
                                            delay: 500
                                            text: qsTr("Set to Time Cursor")
                                        }
                                    }
                                }
                                Rectangle {
                                    width: 220
                                    height: 32
                                    color: activePalette.dark
                                    TextInput {
                                        id: beginTime
                                        width: parent.width
                                        height: parent.height
                                        inputMask: "9999-99-99 99:99:99.999"
                                        selectByMouse: true
                                        onActiveFocusChanged: {
                                            // When we first gain focus, select everything for clearing.
                                            if (activeFocus) {
                                                selectAll()
                                            }
                                        }
                                        onEditingFinished: {
                                            control.focus = false
                                        }
                                        
                                        color: activePalette.text
                                        selectionColor: activePalette.highlight
                                        selectedTextColor: activePalette.highlightedText
                                        horizontalAlignment: Qt.AlignHCenter
                                        verticalAlignment: Qt.AlignVCenter
                                    }
                                }
                                RowLayout {
                                    Rectangle {
                                        width: 120
                                        height: 32
                                        color: "transparent"
                                        Label {
                                            text: "End Timestamp "
                                            color: activePalette.text
                                            
                                            anchors.verticalCenter: parent.verticalCenter
                                        }
                                    }
                                    DefaultButton {
                                        text: "Set"
                                        implicitWidth: 60
                                        implicitHeight: 32
                                        onClicked: endTime.text = timeline.dateMsTimeString(timeline.cursorSeconds)
                                        ToolTip {
                                            visible: parent.hovered
                                            delay: 500
                                            text: qsTr("Set to Time Cursor")
                                        }
                                    }

                                }
                                Rectangle {
                                    width: 220
                                    height: 32
                                    color: activePalette.dark
                                    TextInput {
                                        id: endTime
                                        width: parent.width
                                        height: parent.height
                                        inputMask: "9999-99-99 99:99:99.999"
                                        selectByMouse: true
                                        onActiveFocusChanged: {
                                            // When we first gain focus, select everything for clearing.
                                            if (activeFocus) {
                                                selectAll()
                                            }
                                        }
                                        onEditingFinished: {
                                            control.focus = false
                                        }
                                        
                                        color: activePalette.text
                                        selectionColor: activePalette.highlight
                                        selectedTextColor: activePalette.highlightedText
                                        horizontalAlignment: Qt.AlignHCenter
                                        verticalAlignment: Qt.AlignVCenter
                                    }
                                }

                                RowLayout {
                                    Rectangle {
                                        width: 60
                                        height: 40
                                        color: activePalette.window    
                                    }
                                    DefaultButton {
                                        text: "Cancel"
                                        implicitWidth: 70
                                        implicitHeight: 32
                                        onClicked: cutWindow.close()
                                    }
                                    DefaultButton {
                                        text: "Ok"
                                        implicitWidth: 70
                                        implicitHeight: 32
                                        onClicked: {
                                            fileDialog.open()                                      
                                        }
                                    }
                                }
                            }
                        }
                        FileDialog {
                            id: fileDialog
                            title: "Please choose export file"
                            selectExisting: false
                            // folder: Qt.resolvedUrl("../csv/")
                            nameFilters: ["pcap files (*.pcap)"]
                            onAccepted: {
                                if(modelData.cutPcap(fileDialog.fileUrl, beginTime.text, endTime.text)) {
                                    cutWindow.close()
                                    controlPanel.messageDialogOpen("PCAP is cut successfully!") 
                                }
                                else {
                                    cutWindow.close()
                                    controlPanel.messageDialogOpen("PCAP is cut failed!")
                                }
                                
                            }
                        }
                    }
                    DefaultMenuItem {
                        text: "Export Detail"
                        
                        //enabled: modelData.canExportDetail
                        onClicked: {
                            beginTimeExportPointsInfo.text = timeline.dateMsTimeString(timeline.beginSeconds)
                            endTimeExportPointsInfo.text = timeline.dateMsTimeString(timeline.endSeconds)
                            exportPointsInfoWindow.show()
                        }
                        Window {
                            id: exportPointsInfoWindow
                            title :"Export Detail to multi CSVs"
                            width: detailColumn.width + 20
                            height: detailColumn.height
                            color: activePalette.window
                            flags:  Qt.Dialog 
                                    | Qt.WindowSystemMenuHint | Qt.WindowTitleHint 
                                    | Qt.WindowCloseButtonHint | Qt.WindowOkButtonHint 
                                    | Qt.FrameWindowHint

                            visible: true
                            modality: Qt.NonModal 
                            ColumnLayout {
                                x: 10
                                id: detailColumn
                                spacing: 5
                                Rectangle {
                                    height: 5
                                }
                                RowLayout {
                                    Rectangle {
                                        width: 100
                                        height: 32
                                        color: "transparent"
                                        Label {
                                            text: "Start Time "
                                            color: activePalette.text
                                            
                                            anchors.verticalCenter: parent.verticalCenter
                                        }
                                    }
                                    DefaultButton {
                                        text: "Set"
                                        implicitWidth: 60
                                        implicitHeight: 32
                                        onClicked: beginTimeExportPointsInfo.text = timeline.dateMsTimeString(timeline.cursorSeconds)  
                                        ToolTip {
                                            visible: parent.hovered
                                            delay: 500
                                            text: qsTr("Set to Time Cursor")
                                        }
                                    }
                                }
                                Rectangle {
                                    width: 220
                                    height: 32
                                    color: activePalette.dark
                                    TextInput {
                                        id: beginTimeExportPointsInfo
                                        width: parent.width
                                        height: parent.height
                                        inputMask: "9999-99-99 99:99:99.999"
                                        selectByMouse: true
                                        onActiveFocusChanged: {
                                            // When we first gain focus, select everything for clearing.
                                            if (activeFocus) {
                                                selectAll()
                                            }
                                        }
                                        onEditingFinished: {
                                            control.focus = false
                                        }
                                        
                                        color: activePalette.text
                                        selectionColor: activePalette.highlight
                                        selectedTextColor: activePalette.highlightedText
                                        horizontalAlignment: Qt.AlignHCenter
                                        verticalAlignment: Qt.AlignVCenter
                                    }
                                }


                                RowLayout {
                                    Rectangle {
                                        width: 100
                                        height: 32
                                        color: "transparent"
                                        Label {
                                            text: "End Time "
                                            color: activePalette.text
                                            
                                            anchors.verticalCenter: parent.verticalCenter
                                        }
                                    }
                                    DefaultButton {
                                        text: "Set"
                                        implicitWidth: 60
                                        implicitHeight: 32
                                        onClicked: endTimeExportPointsInfo.text = timeline.dateMsTimeString(timeline.cursorSeconds)
                                        ToolTip {
                                            visible: parent.hovered
                                            delay: 500
                                            text: qsTr("Set to Time Cursor")
                                        }
                                    }

                                }
                                Rectangle {
                                    width: 220
                                    height: 32
                                    color: activePalette.dark
                                    TextInput {
                                        id: endTimeExportPointsInfo
                                        width: parent.width
                                        height: parent.height
                                        inputMask: "9999-99-99 99:99:99.999"
                                        selectByMouse: true
                                        onActiveFocusChanged: {
                                            // When we first gain focus, select everything for clearing.
                                            if (activeFocus) {
                                                selectAll()
                                            }
                                        }
                                        onEditingFinished: {
                                            control.focus = false
                                        }
                                        
                                        color: activePalette.text
                                        selectionColor: activePalette.highlight
                                        selectedTextColor: activePalette.highlightedText
                                        horizontalAlignment: Qt.AlignHCenter
                                        verticalAlignment: Qt.AlignVCenter
                                    }
                                }



                                RowLayout {
                                    Rectangle {
                                        width: 60
                                        height: 40
                                        color: activePalette.window    
                                    }
                                    DefaultButton {
                                        text: "Cancel"
                                        implicitWidth: 70
                                        implicitHeight: 32
                                        onClicked: exportPointsInfoWindow.close()
                                    }
                                    DefaultButton {
                                        text: "Ok"
                                        implicitWidth: 70
                                        implicitHeight: 32
                                        onClicked: {
                                            detailFolderDialog.open()
                                        }
                                    }
                                }
                                Rectangle {
                                    height: 5   
                                }
                            }
                        }
                        FileDialog {
                            id: detailFolderDialog
                            title: "Please choose export folder"
                            selectExisting: false
                            // folder: Qt.resolvedUrl("../csv/")
                            nameFilters: ["csv files (*.csv)"]
                            selectFolder: true
                            onAccepted: {
                                exportPointsInfoWindow.close()
                                messageWindow.info("Detail Export Processing...")
                                detailExportTimer.start()
                            }
                        }
                        Timer {
                            id: detailExportTimer
                            repeat: false
                            onTriggered: {
                                modelData.setDetailExport(true, detailFolderDialog.fileUrl)
                                timeline.playForTimeInterval(beginTimeExportPointsInfo.text, endTimeExportPointsInfo.text)
                                modelData.setDetailExport(false)
                                messageWindow.info("Detail Export finished!") 
                            }
                        }
                    }
                    DefaultMenuItem {
                        text: "Delete Track"
                        onClicked: {
                            trackBuffer.deleteTrack();
                        }
                    }
                    DefaultMenuItem {
                        text: "Cancel"
                    }
                }
            }
        }
    }
}