/*
 * 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

BaseButton {
    UkuiItems.DtThemeBackground {
        anchors.fill: parent
        radius: mainWindow.isTabletMode ? Platform.GlobalTheme.kRadiusMax * 2 : Platform.GlobalTheme.kRadiusMax
        backgroundColor: Platform.GlobalTheme.kContainSecondaryAlphaNormal
        border.width: 1
        borderColor: Platform.GlobalTheme.kGrayAlpha1
        useStyleTransparency: false

        MouseArea {
            anchors.right: parent.right
            height: rowLayout.height
            width: parent.width - buttonIcon.width - rowLayout.anchors.leftMargin
            onClicked: {
                widgetPage.showWidget(widgetId, true, name, 250)
            }
        }

        RowLayout {
            id: rowLayout
            anchors.fill: parent
            anchors.leftMargin: mainWindow.isTabletMode ? 24 : 10
            anchors.rightMargin: 4
            spacing: 8

            UkuiItems.DtThemeBackground {
                id: buttonIcon
                useStyleTransparency: false
                Layout.preferredWidth: mainWindow.isTabletMode ? 64 : 48
                Layout.preferredHeight: mainWindow.isTabletMode ? 64 : 48
                Layout.alignment: Qt.AlignVCenter
                radius: width/2

                backgroundColor: buttonColor === PluginColorRole.HighLight ? menuHighLightBackGroundColor()
                                                                           : menuNormalBackGroundColor()
                border.width: 1
                borderColor: buttonColor === PluginColorRole.HighLight ? Platform.GlobalTheme.kGrayAlpha1
                                                                       : menuNormalBorderColor()

                function menuHighLightBackGroundColor() {
                    return mouseArea.containsPress ? Platform.GlobalTheme.kBrandClick
                                                   : mouseArea.containsMouse ? Platform.GlobalTheme.kBrandHover
                                                                             : Platform.GlobalTheme.kBrandNormal
                }

                function menuNormalBackGroundColor() {
                    return mouseArea.containsPress ? Platform.GlobalTheme.kContainAlphaClick
                                                   : mouseArea.containsMouse ? Platform.GlobalTheme.kContainAlphaHover
                                                                             : Platform.GlobalTheme.kContainSecondaryAlphaNormal
                }

                function menuNormalBorderColor() {
                    return mouseArea.containsPress ? Platform.GlobalTheme.kGrayAlpha3
                                                   : mouseArea.containsMouse ? Platform.GlobalTheme.kGrayAlpha2
                                                                             : Platform.GlobalTheme.kGrayAlpha1
                }

                MouseArea {
                    id: mouseArea
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: {
                        active(ShortcutPlugin.Click);
                    }
                }

                UkuiItems.Icon {
                    width: parent.width/2
                    height: parent.width/2
                    anchors.centerIn: parent
                    source: icon
                    // mode: UkuiItems.Icon.AutoHighlight
                    mode: (buttonColor === PluginColorRole.HighLight) ? UkuiItems.Icon.Highlight :
                        (buttonColor === PluginColorRole.Disable) ? UkuiItems.Icon.Disabled : UkuiItems.Icon.AutoHighlight
                }
            }

            ColumnLayout {
                id: columnLayout
                Layout.preferredWidth: parent.width - buttonIcon.width - swipeIcon.width - parent.spacing * 2
                Layout.fillHeight: true
                spacing: 2

                UkuiItems.DtThemeText {
                    Layout.preferredWidth: parent.width
                    Layout.preferredHeight: (parent.height - parent.spacing) / 2
                    text: name
                    elide: Text.ElideRight
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    textColor: Platform.GlobalTheme.kFontPrimary
                    UkuiItems.Tooltip {
                        anchors.fill: parent
                        posFollowCursor: true
                        mainText: name
                        active: parent.truncated
                    }
                }

                UkuiItems.DtThemeText {
                    Layout.preferredWidth: parent.width
                    Layout.preferredHeight: (parent.height - parent.spacing) / 2
                    text: statusName
                    pointSizeOffset: -2
                    elide: Text.ElideRight
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    textColor: Platform.GlobalTheme.kFontPrimary
                    UkuiItems.Tooltip {
                        anchors.fill: parent
                        posFollowCursor: true
                        mainText: statusName
                        active: parent.truncated
                    }
                }
            }

            UkuiItems.Icon {
                id: swipeIcon
                Layout.preferredWidth: 24
                Layout.preferredHeight: 24
                source: LayoutMirroring.enabled ? "ukui-start-symbolic" : "pan-end-symbolic"
                mode: UkuiItems.Icon.AutoHighlight
                visible: widgetId === "" ? false : true
            }
        }
    }
}
