import QtQuick
import QtQuick.Window
import QtQuick.Dialogs
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("DoViGUI")
    color: "darkgrey"

    Button {
        id: btnVideoFile
        anchors.topMargin: 25
        anchors.leftMargin: 25
        anchors.top: parent.top
        anchors.left: parent.left
        text: "Video File"
        font.pixelSize: 14
        onClicked: videoFileDialog.open()
        flat: false
        height: font.pixelSize + 10
        width: 100
    }

    Rectangle {
        anchors.top: btnVideoFile.bottom
        anchors.left: btnVideoFile.left
        anchors.right: parent.right
        anchors.rightMargin: 25
        color: "lightgrey"
        height: txtVideoFile.font.pixelSize + 10
        id: txtBackground

        TextEdit {
            id: txtVideoFile
            text: ""
            verticalAlignment: TextEdit.AlignVCenter
            anchors.fill: parent
            readOnly: false
            selectByMouse: true
            font.pixelSize: 14
            anchors.leftMargin: 15
            anchors.rightMargin: 15
        }
    }

    Text {
        //TODO Need to scroll and clip long text
        id: hdrType
        anchors.left: btnVideoFile.left
        anchors.top: txtBackground.bottom
        width: 200
        text: "None"
        wrapMode: TextEdit.Wrap
    }

    Text {
        id: searchResult
        anchors.left: btnVideoFile.left
        anchors.top: hdrType.bottom
        width: 200
        text: "No Results"
        wrapMode: TextEdit.Wrap
    }

    Button {
        id: btnStartEncode
        anchors.topMargin: 25
        anchors.leftMargin: 25
        anchors.top: searchResult.bottom
        anchors.left: parent.left
        text: "Start Encode"
        font.pixelSize: 14
        onClicked: handbrake.startEncode()
        flat: false
        height: font.pixelSize + 10
        width: 100
    }

    Button {
        id: btnStopEncode
        anchors.topMargin: 25
        anchors.leftMargin: 25
        anchors.top: searchResult.bottom
        anchors.left: btnStartEncode.right
        text: "Stop Encode"
        font.pixelSize: 14
        onClicked: handbrake.stopEncode()
        flat: false
        height: font.pixelSize + 10
        width: 100
    }

    Text {
        id: handbrakeStatus
        anchors.left: btnVideoFile.left
        anchors.bottom: parent.bottom
        width: 600
        text: "Encode not started."
        wrapMode: TextEdit.Wrap
    }

    FileDialog {
        id: videoFileDialog
        fileMode: FileDialog.OpenFile
        nameFilters: ["Video files (*.mkv *.mp4)", "All files (*)"]
        onAccepted: {
            //Remove "file:/// at start of URL
            var fileName = mediaInfo.fixFileName(videoFileDialog.selectedFile.toString());
            txtVideoFile.text = fileName;

            switch (mediaInfo.getHDRType(fileName)) {
                case 0:
                    hdrType.text = "SDR"
                    break
                case 1:
                    hdrType.text = "HDR"
                    break
                case 2:
                    hdrType.text = "Dolby Vision"
                    break
                default:
                    hdrType.text = "Error"
            }

            movieDB.searchMovieTitleFile(fileName);
        }
    }

    Connections {
        target: movieDB
        function onSearchResultReturned(result) {
            searchResult.text = "First Result: " + result;
        }
    }

    Connections {
        target: handbrake
        function onStatusChanged(status) {
            handbrakeStatus.text = status;
        }
    }
}
