import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

import "../style"

Rectangle {
    id: root

    property string densityMode: "normal"

    ApplicationStyle {
        id: applicationStyle
        densityMode: root.densityMode
    }

    property var analysisController: null
    property var bandModel: null
    property var harmonicModel: null
    property var signalBandModel: null
    property bool markerModeReady: false
    property bool settingsButtonVisible: true
    property bool dragToDetachEnabled: false
    property bool locked: false
    property bool lockButtonVisible: false

    signal settingsRequested()
    signal detachRequested(real globalX, real globalY,
                           real pressOffsetX, real pressOffsetY)
    signal lockToggled(bool locked)

    function syncMarkerMode() {
        if (!analysisController)
            return
        if (markerTabs.currentIndex !== 2)
            markerTabs.currentIndex = analysisController.markerCreationMode
                    === "band" ? 0 : 1
    }

    onAnalysisControllerChanged: {
        if (markerModeReady)
            syncMarkerMode()
    }

    Component.onCompleted: {
        syncMarkerMode()
        markerModeReady = true
    }

    Connections {
        target: root.analysisController

        function onMarkerCreationModeChanged() {
            root.syncMarkerMode()
        }

    }

    Connections {
        target: root.analysisController
                ? root.analysisController.signalBandControllerObject : null

        function onMarkerSelected(markerType, index) {
            if (markerType === "band" && index >= 0)
                markerTabs.currentIndex = 2
        }
    }

    color: applicationStyle.panelColor
    border.width: applicationStyle.borderWidth
    border.color: applicationStyle.subtleBorderColor
    radius: 7
    clip: true

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 5
        spacing: 4

        RowLayout {
            Layout.fillWidth: true
            Layout.minimumHeight: 38
            Layout.preferredHeight: 38
            Layout.maximumHeight: 38
            spacing: 4

            PanelTitleBar {
                Layout.fillWidth: true
                Layout.fillHeight: true
                title: "Маркеры"
                lockButtonVisible: root.lockButtonVisible
                locked: root.locked
                dragToDetachEnabled: root.dragToDetachEnabled && !root.locked
                onLockToggled: function(locked) {
                    root.lockToggled(locked)
                }
                onDetachRequested: function(globalX, globalY,
                                             pressOffsetX, pressOffsetY) {
                    root.detachRequested(globalX, globalY,
                                         pressOffsetX, pressOffsetY)
                }
            }

            AppToolButton {
                id: markerSettingsButton

                visible: root.settingsButtonVisible
                Layout.minimumWidth: visible ? 34 : 0
                Layout.preferredWidth: visible ? 34 : 0
                Layout.maximumWidth: visible ? 34 : 0
                Layout.fillHeight: true
                hoverEnabled: true
                focusPolicy: Qt.TabFocus
                onClicked: root.settingsRequested()

                contentItem: Image {
                    source: "../assets/settings-gear.svg"
                    sourceSize.width: 23
                    sourceSize.height: 23
                    fillMode: Image.PreserveAspectFit
                    opacity: markerSettingsButton.enabled ? 1 : 0.5
                }

                background: Rectangle {
                    radius: 5
                    color: markerSettingsButton.down
                           ? applicationStyle.controlPressedColor
                           : markerSettingsButton.hovered
                             ? applicationStyle.controlHoverColor
                             : applicationStyle.controlColor
                    border.color: markerSettingsButton.down
                                  ? applicationStyle.borderColor
                                  : applicationStyle.controlBorderColor

                }

                AppToolTip {
                    target: markerSettingsButton
                    text: "Настроить маркеры по умолчанию"
                }

            }
        }

        TabBar {
            id: markerTabs

            Layout.fillWidth: true
            Layout.minimumHeight: 42
            Layout.preferredHeight: 42
            Layout.maximumHeight: 42
            currentIndex: 1
            spacing: 4
            onCurrentIndexChanged: {
                if (root.markerModeReady && root.analysisController
                        && currentIndex !== 2)
                    root.analysisController.markerCreationMode =
                            currentIndex === 0 ? "band" : "harmonic"
            }

            background: Rectangle {
                color: "transparent"
            }

            AppTabButton {
                id: bandTab

                width: markerTabs.width / 3 - 3
                hoverEnabled: enabled
                focusPolicy: enabled ? Qt.TabFocus : Qt.NoFocus
                text: "Полосовые ("
                      + (root.bandModel ? root.bandModel.count : 0) + ")"

                contentItem: Text {
                    renderType: Text.NativeRendering
                    text: bandTab.text
                    color: bandTab.checked
                           ? applicationStyle.inverseTextColor
                           : applicationStyle.controlTextColor
                    font.bold: bandTab.checked
                    font.pixelSize: applicationStyle.fontSizeSubtitle
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }

                background: Rectangle {
                    radius: 5
                    color: bandTab.checked
                           ? (bandTab.enabled && bandTab.hovered
                              ? applicationStyle.accentHoverColor
                              : applicationStyle.accentColor)
                           : (bandTab.enabled && bandTab.hovered
                              ? applicationStyle.controlHoverColor
                              : applicationStyle.controlColor)
                    border.color: bandTab.checked
                                  ? applicationStyle.accentColor
                                  : applicationStyle.controlBorderColor

                    Behavior on color {
                        enabled: bandTab.checked
                        ColorAnimation {
                            duration: bandTab.hoverAnimationDuration
                        }
                    }
                }
            }

            AppTabButton {
                id: harmonicTab

                width: markerTabs.width / 3 - 3
                hoverEnabled: enabled
                focusPolicy: enabled ? Qt.TabFocus : Qt.NoFocus
                text: "Гармонические ("
                      + (root.harmonicModel
                         ? root.harmonicModel.count : 0) + ")"

                contentItem: Text {
                    renderType: Text.NativeRendering
                    text: harmonicTab.text
                    color: harmonicTab.checked
                           ? applicationStyle.inverseTextColor
                           : applicationStyle.controlTextColor
                    font.bold: harmonicTab.checked
                    font.pixelSize: applicationStyle.fontSizeSubtitle
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }

                background: Rectangle {
                    radius: 5
                    color: harmonicTab.checked
                           ? (harmonicTab.enabled
                              && harmonicTab.hovered
                              ? applicationStyle.accentHoverColor
                              : applicationStyle.accentColor)
                           : (harmonicTab.enabled
                              && harmonicTab.hovered
                              ? applicationStyle.controlHoverColor
                              : applicationStyle.controlColor)
                    border.color: harmonicTab.checked
                                  ? applicationStyle.accentColor
                                  : applicationStyle.controlBorderColor

                    Behavior on color {
                        enabled: harmonicTab.checked
                        ColorAnimation {
                            duration: harmonicTab.hoverAnimationDuration
                        }
                    }
                }
            }

            AppTabButton {
                id: signalBandTab

                width: markerTabs.width / 3 - 3
                hoverEnabled: enabled
                focusPolicy: enabled ? Qt.TabFocus : Qt.NoFocus
                text: "Полосы сигнала ("
                      + (root.signalBandModel
                         ? root.signalBandModel.count : 0) + ")"

                contentItem: Text {
                    renderType: Text.NativeRendering
                    text: signalBandTab.text
                    color: signalBandTab.checked
                           ? applicationStyle.inverseTextColor
                           : applicationStyle.controlTextColor
                    font.bold: signalBandTab.checked
                    font.pixelSize: applicationStyle.fontSizeSubtitle
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }

                background: Rectangle {
                    radius: 5
                    color: signalBandTab.checked
                           ? (signalBandTab.enabled && signalBandTab.hovered
                              ? applicationStyle.accentHoverColor
                              : applicationStyle.accentColor)
                           : (signalBandTab.enabled && signalBandTab.hovered
                              ? applicationStyle.controlHoverColor
                              : applicationStyle.controlColor)
                    border.color: signalBandTab.checked
                                  ? applicationStyle.accentColor
                                  : applicationStyle.controlBorderColor
                }
            }
        }

        StackLayout {
            Layout.fillWidth: true
            Layout.fillHeight: true
            currentIndex: markerTabs.currentIndex

            BandAnalysisPanel {
                densityMode: root.densityMode
                analysisController: root.analysisController
                bandModel: root.bandModel
            }

            HarmonicAnalysisPanel {
                densityMode: root.densityMode
                analysisController: root.analysisController
                harmonicModel: root.harmonicModel
            }

            BandAnalysisPanel {
                densityMode: root.densityMode
                analysisController: root.analysisController
                                    ? root.analysisController.signalBandControllerObject
                                    : null
                bandModel: root.signalBandModel
                rangeUnit: "с"
                showPeriodFrequency: true
                instructionText: "Зажмите ЛКМ на временном сигнале и протяните границы полосы."
            }
        }

        AppButton {
            id: clearMarkersButton

            Layout.fillWidth: true
            Layout.preferredHeight: 40
            hoverEnabled: enabled
            focusPolicy: enabled ? Qt.TabFocus : Qt.NoFocus
            text: markerTabs.currentIndex === 0
                  ? "Очистить полосовые"
                  : markerTabs.currentIndex === 2
                    ? "Очистить полосы сигнала"
                    : "Очистить гармонические"
            enabled: root.analysisController
                     && (markerTabs.currentIndex === 0
                         ? root.bandModel && root.bandModel.count > 0
                         : markerTabs.currentIndex === 2
                           ? root.signalBandModel
                             && root.signalBandModel.count > 0
                           : root.harmonicModel
                             && root.harmonicModel.count > 0)
            onClicked: {
                if (markerTabs.currentIndex === 2)
                    root.analysisController.signalBandControllerObject
                            .clearMarkers("band")
                else
                    root.analysisController.clearMarkers(
                                markerTabs.currentIndex === 0
                                ? "band" : "harmonic")
            }

            contentItem: Text {
                renderType: Text.NativeRendering
                text: clearMarkersButton.text
                color: clearMarkersButton.enabled
                       ? applicationStyle.dangerTextColor
                       : applicationStyle.controlBorderColor
                font.bold: true
                font.pixelSize: applicationStyle.fontSizeSubtitle
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                elide: Text.ElideRight
            }

            background: Rectangle {
                radius: 5
                color: !clearMarkersButton.enabled
                       ? applicationStyle.controlColor
                       : clearMarkersButton.down
                         ? applicationStyle.dangerPressedColor
                         : clearMarkersButton.enabled
                           && clearMarkersButton.hovered
                           ? applicationStyle.controlColor
                           : applicationStyle.panelColor
                border.color: clearMarkersButton.enabled
                              ? applicationStyle.dangerBorderColor
                              : applicationStyle.disabledBorderColor

            }
        }

    }
}
