// SPDX-FileCopyrightText: 2024-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15
import VNote 1.0
import org.deepin.dtk 1.0
import "../" as VNoteComponents

TitleBar {
    id: titleBar

    property bool imageBtnEnable: true
    property bool isInitialInterface: true
    property bool isPlaying: false
    property bool isRecording: false
    property bool isRecordingAudio: false  
    property bool isSearching: false
    property bool isVoiceToText: false
    property bool recorderBtnEnable: true
    property bool recordingHover: false
    property bool recordBtnEnabled: recordBtn.enabled

    signal createNote
    signal insertImage
    signal startRecording
    signal titleOpenSetting

    enableInWindowBlendBlur: false
    separatorVisible: false

    anchors.fill: parent

    background: Rectangle {
        color: DTK.themeType === ApplicationHelper.LightType ? "white" : "#242424"
    }
    content: Item {
        Component.onCompleted: {
            parent.Layout.leftMargin = 0;
        }
    }
    menu: TitleBarMenu {
        id: tMenu

        onOpenPrivacy: {
            VNoteMainManager.showPrivacy();
        }
        onOpenSetting: {
            titleBar.titleOpenSetting();
        }
    }

    VNoteComponents.VNoteToolButton {
        id: newNoteBtn

        anchors.left: titleBar.left
        anchors.leftMargin: 10
        anchors.verticalCenter: titleBar.verticalCenter
        enabled: !isPlaying && !isSearching && !isRecordingAudio && !isVoiceToText
        hoverEnabled: !isInitialInterface
        visible: recordBtn.x > x + width
        icon.height: 16
        icon.name: "new_note"
        icon.width: 16
        height: 30
        width: 30

        onClicked: {
            createNote();
        }

        ToolTip {
            text: qsTr("Create Note")
            visible: newNoteBtn.hovered
        }
    }

    Rectangle {
        id: maskRect

        anchors.fill: recordBtn
        color: "transparent"
        visible: !recordBtn.enabled

        MouseArea {
            hoverEnabled: true

            onEntered: {
                recordingHover = true;
            }
            onExited: {
                recordingHover = false;
            }
        }
    }

    VNoteComponents.VNoteToolButton {
        id: recordBtn

        anchors.right: insImgBtn.left
        anchors.rightMargin: 6
        anchors.verticalCenter: titleBar.verticalCenter
        enabled: recorderBtnEnable && imageBtnEnable && !isPlaying && !isSearching
        hoverEnabled: !isInitialInterface
        icon.height: 16
        icon.name: "record"
        icon.width: 16
        height: 30
        width: 30

        onClicked: {
            startRecording();
            isRecording = true;
        }

        ToolTip {
            text: recordBtn.enabled ? qsTr("Start recording") : qsTr("No recording device detected")
            visible: (recordBtn.hovered || recordingHover) && !isRecording && !isPlaying && !isInitialInterface
        }
    }

    VNoteComponents.VNoteToolButton {
        id: insImgBtn

        anchors.verticalCenter: titleBar.verticalCenter
        enabled: imageBtnEnable
        hoverEnabled: !isInitialInterface
        icon.height: 16
        icon.name: "img"
        icon.width: 16
        height: 30
        width: 30
        x: titleBar.__includedAreaX - recordBtn.width - 10

        onClicked: {
            insertImage();
        }

        ToolTip {
            text: qsTr("Insert picture")
            visible: insImgBtn.hovered
        }
    }
}
