import QtQuick 2.15
import org.deepin.dtk 1.0 as D

Item {
    id: item


    property bool blockCreateKeys: false
    // 初始页面仅允许 Ctrl+N / F1，其它快捷键全部禁用
    property bool initialOnlyCreateFolder: false
    // 录音过程中禁用录音快捷键，避免重复启动录音导致崩溃
    property bool blockRecordingKey: false
    // 录音状态，用于禁用播放快捷键
    property bool isRecordingAudio: false
    // 拖拽状态下禁用所有快捷键
    property bool isDragging: false

    signal copy
    signal createFolder
    signal createNote
    signal playPauseVoice
    signal renameFolder
    signal renameNote
    signal saveNote
    signal saveVoice
    signal showShortCutView
    signal startRecording
    signal showJsContextMenu

    Shortcut {
        id: ctrl_F1

        //帮助手册
        autoRepeat: false
        enabled: !item.isDragging
        sequence: "F1"

        onActivated: {
            D.ApplicationHelper.handleHelpAction();
        }
    }

    Shortcut {
        id: ctrl_Shift_H
        enabled: !item.isDragging && !item.initialOnlyCreateFolder

        //快捷键界面
        sequence: "Ctrl+Shift+/"

        onActivated: {
            showShortCutView();
        }
    }

    Shortcut {
        id: ctrl_S
        enabled: !item.isDragging && !item.initialOnlyCreateFolder

        sequence: "Ctrl+S"

        onActivated: {
            saveNote();
        }
    }

    Shortcut {
        id: ctrl_D
        enabled: !item.isDragging && !item.initialOnlyCreateFolder

        sequence: "Ctrl+D"

        onActivated: {
            saveVoice();
        }
    }

    Shortcut {
        id: ctrl_N

        sequence: "Ctrl+N"

        enabled: !item.isDragging && !item.blockCreateKeys

        onActivated: {
            createFolder();
        }
    }

    Shortcut {
        id: rename
        enabled: !item.isDragging && !item.initialOnlyCreateFolder

        sequence: "F2"

        onActivated: {
            renameFolder();
        }
    }

    Shortcut {
        id: renameNoteShort
        enabled: !item.isDragging && !item.initialOnlyCreateFolder

        sequence: "F3"

        onActivated: {
            renameNote();
        }
    }

    Shortcut {
        id: ctrl_R
        // 初始页面、录音中或显式要求屏蔽录音快捷键时，均不响应 Ctrl+R
        enabled: !item.isDragging && !item.initialOnlyCreateFolder && !rootWindow.isRecordingAudio && !item.blockRecordingKey

        sequence: "Ctrl+R"

        onActivated: {
            console.warn("Ctrl+R activated - initialOnlyCreateFolder:", item.initialOnlyCreateFolder,
                         "isRecordingAudio:", rootWindow.isRecordingAudio,
                         "blockRecordingKey:", item.blockRecordingKey);

            // 双重保护：只在未录音时启动录音，录音过程中不响应
            if (!rootWindow.isRecordingAudio) {
                startRecording();
            } else {
                console.warn("Ctrl+R ignored: recording is already in progress");
            }
        }
    }

    // 移除全局空格键快捷键，让JavaScript层独自处理语音播放/暂停
    // JavaScript层已经有完整的录音状态检查和选中状态检查逻辑

    Shortcut {
        id: ctrl_B

        sequence: "Ctrl+B"

        enabled: !item.isDragging && !item.blockCreateKeys && !item.initialOnlyCreateFolder

        onActivated: {
            createNote();
        }
    }

    Shortcut {
        id: ctrl_A

        sequence: "Ctrl+A"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {}
    }

    Shortcut {
        id: ctrl_C

        sequence: "Ctrl+C"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {
            copy();
        }
    }

    Shortcut {
        id: ctrl_X

        sequence: "Ctrl+X"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {}
    }

    Shortcut {
        id: ctrl_V

        sequence: "Ctrl+V"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {}
    }

    Shortcut {
        id: alt_M

        sequence: "Alt+M"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {
            showJsContextMenu();
        }
    }

    Shortcut {
        id: ctrl_Z

        sequence: "Ctrl+Z"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {}
    }

    Shortcut {
        id: ctrl_Shift_Z

        sequence: "Ctrl+Shift+Z"

        enabled: !item.isDragging && !item.initialOnlyCreateFolder
        onActivated: {}
    }
}
