// file: poc-qt/poc002/qml/main.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtMultimedia

ApplicationWindow {
    visible: true
    width: 960
    height: 640
    title: "POC002 - Video Player"

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 12
        spacing: 8

        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: "black"
            border.width: 1
            border.color: "#555"

            VideoOutput {
                id: videoOutput
                anchors.fill: parent
                fillMode: VideoOutput.PreserveAspectFit
            }
        }

        RowLayout {
            Layout.fillWidth: true
            spacing: 8

            Button {
                text: player.playbackState === MediaPlayer.PlayingState ? "Pause" : "Play"
                onClicked: {
                    if (player.playbackState === MediaPlayer.PlayingState) {
                        player.pause()
                    } else {
                        player.play()
                    }
                }
            }

            Button {
                text: "Stop"
                onClicked: player.stop()
            }

            Slider {
                Layout.fillWidth: true
                from: 0
                to: player.duration > 0 ? player.duration : 1
                value: player.position
                onMoved: player.position = value
            }

            Label {
                text: Math.floor(player.position / 1000) + " / " + Math.floor(player.duration / 1000) + " s"
            }
        }

        Label {
            Layout.fillWidth: true
            text: player.errorString.length > 0
                ? ("Error: " + player.errorString)
                : ("Source: " + player.source)
            wrapMode: Text.WrapAnywhere
        }
    }

    MediaPlayer {
        id: player
        source: "../../videos/001.webm"
        audioOutput: AudioOutput {}
        videoOutput: videoOutput
        loops: MediaPlayer.Infinite
    }

    Component.onCompleted: {
        player.play()
    }
}
