/*
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import QtQuick 2.0
import QtQuick.Layouts 1.12
import org.ukui.sidebar.core 1.0
import org.ukui.sidebar.shortcut.core 1.0
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0 as Platform

Item {
    signal exitEdit()
    height: panelLayout.height + 14
    onExitEdit: {
        dropArea.isEdit = false;
    }

    ColumnLayout {
        id: panelLayout
        width: parent.width
        spacing: 16

        anchors.top: parent.top
        anchors.topMargin: 14

        Item {
            Layout.preferredWidth: parent.width - 40
            Layout.preferredHeight: 32
            Layout.alignment: Qt.AlignHCenter

            RowLayout {
                anchors.fill: parent
                spacing: 0

                RowLayout {
                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    UkuiItems.DtThemeText {
                        Layout.fillWidth: true
                        Layout.preferredHeight: 20
                        Layout.alignment: Qt.AlignVCenter

                        text: dropArea.isEdit ? qsTr("Added components") : qsTr("Shortcuts")
                        textColor: Platform.GlobalTheme.kFontPrimary
                        elide: Text.ElideRight
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignLeft
                    }
                }

                Component {
                    id: editButton
                    UkuiItems.DtThemeButton {
                        //kGrayAlpha0
                        property var kTransparent : Platform.GradientColor {
                            gradientDeg : 0
                            gradientStart : Qt.rgba(0/255, 0/255, 0/255, 0)
                            gradientEnd : Qt.rgba(0/255, 0/255, 0/255, 0)
                            gradientBackground : Qt.rgba(0/255, 0/255, 0/255, 0)
                        }
                        width: 32
                        height: 32

                        background.radius: mainWindow.isTabletMode ? Platform.GlobalTheme.kRadiusMax : Platform.GlobalTheme.kRadiusNormal
                        background.backgroundColor: containsPress ? Platform.GlobalTheme.kContainAlphaClick
                                                                  : containsMouse ? Platform.GlobalTheme.kContainAlphaHover
                                                                                  : kTransparent

                        icon.source: editModel.editIconName()
                        icon.mode: UkuiItems.Icon.AutoHighlight

                        onClicked: {
                            dropArea.isEdit = !dropArea.isEdit;
                        }

                        UkuiItems.Tooltip {
                            anchors.fill: parent;
                            posFollowCursor: false;
                            mainText: qsTr("Click to enter editing mode");
                            location: UkuiItems.Dialog.BottomEdge;
                        }
                    }
                }

                Component {
                    id: backButton
                    UkuiItems.DtThemeBackground {
                        width: 80
                        height: 32

                        radius: mainWindow.isTabletMode ? Platform.GlobalTheme.kRadiusMax : Platform.GlobalTheme.kRadiusNormal
                        backgroundColor: control.containsPress ? Platform.GlobalTheme.kContainAlphaClick
                                                               : control.containsMouse ? Platform.GlobalTheme.kContainAlphaHover
                                                                                       : Platform.GlobalTheme.kContainSecondaryAlphaNormal
                        border.width: 1
                        borderColor: control.containsPress ? Platform.GlobalTheme.kGrayAlpha3
                                                             : control.containsMouse ? Platform.GlobalTheme.kGrayAlpha2
                                                                                       : Platform.GlobalTheme.kGrayAlpha1
                        UkuiItems.DtThemeText {
                            anchors.centerIn: parent
                            width: Math.min(implicitWidth, parent.width)
                            text: qsTr("Done")
                            textColor: Platform.GlobalTheme.kFontPrimary
                            pointSizeOffset: -2
                            elide: Text.ElideRight
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignLeft
                        }

                        MouseArea {
                            id: control
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: {
                                dropArea.isEdit = !dropArea.isEdit;
                            }

                            UkuiItems.Tooltip {
                                anchors.fill: parent;
                                posFollowCursor: false;
                                mainText: qsTr("Click to exit editing mode");
                                location: UkuiItems.Dialog.BottomEdge;
                            }
                        }
                    }
                }

                Loader {
                    id: loader
                    Layout.preferredWidth: item ? item.width : 0
                    Layout.preferredHeight: item ? item.height : 0
                    Layout.alignment: Qt.AlignVCenter

                    sourceComponent: dropArea.isEdit ? backButton : editButton
                }
            }
        }

        EditButtonView {
            property bool isEdit: false
            id: dropArea
            Layout.preferredWidth: childrenRect.width
            Layout.preferredHeight: childrenRect.height
            Layout.alignment: Qt.AlignHCenter

            Connections {
                target: mainWindow
                function onVisibleChanged() {
                    if (!mainWindow.visible) dropArea.isEdit = false;
                }
            }
        }

        Item {
            Layout.preferredWidth: childrenRect.width
            Layout.preferredHeight: childrenRect.height
            Layout.alignment: Qt.AlignHCenter

            BaseButtonView {
                anchors.centerIn: parent
                Layout.alignment: Qt.AlignHCenter
                columns: 1
                itemWidth: 356
                itemHeight: 56
                rowSpacing: 16
                buttoonType: ShortcutPlugin.ProgressBar
                buttonComponent: ProgressBar {}
            }
        }

        Item {
            Layout.fillWidth: true
            Layout.preferredHeight: 56
            Layout.alignment: Qt.AlignHCenter

            UkuiItems.DtThemeBackground {
                anchors.top: parent.top
                width: parent.width
                height: 1
                backgroundColor: Platform.GlobalTheme.kDivider
                useStyleTransparency: false
            }

            UkuiItems.DtThemeBackground {
                visible: powerButton.isEnable
                anchors.left: parent.left
                anchors.leftMargin: 16
                anchors.verticalCenter: parent.verticalCenter
                width: powerIcon.width + powerText.width + 22
                height: 32
                backgroundColor: powerControl.containsPress ? Platform.GlobalTheme.kContainAlphaClick
                                                            : powerControl.containsMouse ? Platform.GlobalTheme.kContainAlphaHover
                                                                                         : Platform.GlobalTheme.kGrayAlpha0
                useStyleTransparency: false
                radius: Platform.GlobalTheme.kRadiusNormal

                UkuiItems.Icon {
                    id: powerIcon
                    anchors.left: parent.left
                    anchors.leftMargin: 8
                    anchors.verticalCenter: parent.verticalCenter
                    width: 16
                    height: 16
                    source: powerButton.icon
                    mode: UkuiItems.Icon.AutoHighlight
                }

                UkuiItems.DtThemeText {
                    id: powerText
                    anchors.left: powerIcon.right
                    anchors.leftMargin: 6
                    anchors.verticalCenter: parent.verticalCenter
                    elide: Text.ElideRight
                    textColor: Platform.GlobalTheme.kFontPrimary
                    text: powerButton.percentage + "%"
                }

                MouseArea {
                    id: powerControl
                    anchors.fill: parent
                    hoverEnabled: true

                    onClicked: {
                        appManager.launchAppWithArguments("/usr/share/applications/ukui-control-center.desktop", ["-m", "Power"], "ukui-control-center");
                    }

                    UkuiItems.Tooltip {
                        anchors.fill: parent
                        posFollowCursor: true
                        mainText: powerButton.tooltip + "%"
                        active: powerControl.containsMouse
                    }
                }
            }

            UkuiItems.DtThemeButton {
                id: sidebarSystemSetting
                width: 32
                height: 32

                anchors.right: parent.right
                anchors.rightMargin: 16
                anchors.verticalCenter: parent.verticalCenter

                icon.width: 16
                icon.height: 16
                icon.source: "application-system-symbolic"
                icon.mode: UkuiItems.Icon.AutoHighlight

                background.backgroundColor: containsPress ? Platform.GlobalTheme.kContainAlphaClick
                                                          : containsMouse ? Platform.GlobalTheme.kContainAlphaHover
                                                                          : Platform.GlobalTheme.kGrayAlpha0
                background.radius: Platform.GlobalTheme.kRadiusNormal

                onClicked: {
                    appManager.launchAppWithArguments("/usr/share/applications/ukui-control-center.desktop", [], "ukui-control-center");
                }

                UkuiItems.Tooltip {
                    anchors.fill: parent
                    posFollowCursor: true
                    mainText: qsTr("Open settings")
                }
            }
        }
    }
}
