/*
 * 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 {
        useStyleTransparency: false
        anchors.fill: parent
        radius: mainWindow.isTabletMode ? Platform.GlobalTheme.kRadiusMax * 2 : Platform.GlobalTheme.kRadiusMax
        backgroundColor: buttonColor === PluginColorRole.HighLight ? iconHighLightBackGroundColor()
                                                                   : iconNormalBackGroundColor()
        border.width: 1
        borderColor: buttonColor === PluginColorRole.HighLight ? Platform.GlobalTheme.kGrayAlpha1
                                                               : iconNormalBorderColor()

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

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

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

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

            UkuiItems.Tooltip {
                anchors.fill: parent
                mainText: name
                posFollowCursor: true
                active: buttonName.truncated
            }
        }

        ColumnLayout {
            anchors.fill: parent
            anchors.topMargin: 8
            spacing: 4

            Item {
                Layout.fillWidth: true
                Layout.fillHeight: true

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

            UkuiItems.DtThemeText {
                id: buttonName
                Layout.fillWidth: true
                Layout.preferredHeight: contentHeight + 8

                text: name
                elide: Qt.ElideRight
                textColor: disabled ? Platform.GlobalTheme.kFontPrimaryDisable
                                    : buttonColor === PluginColorRole.HighLight ? Platform.GlobalTheme.kFontWhite
                                                                                : Platform.GlobalTheme.kFontPrimary

                pointSizeOffset: -2
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignTop
            }
        }
    }
}
