/*
 * 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.2
import QtQuick.Controls 2.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

        RowLayout {
            spacing: 8
            anchors.fill: parent
            anchors.leftMargin: mainWindow.isTabletMode ? 24 : 12
            anchors.rightMargin: mainWindow.isTabletMode ? 24 : 12

            UkuiItems.Icon {
                Layout.preferredWidth: mainWindow.isTabletMode ? 32 : 24
                Layout.preferredHeight: mainWindow.isTabletMode ? 32 : 24
                Layout.alignment: Qt.AlignVCenter
                source: icon
                mode: UkuiItems.Icon.AutoHighlight

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true;

                    UkuiItems.Tooltip {
                        anchors.fill: parent
                        posFollowCursor: true
                        active: parent.containsMouse
                        mainText: tooltip
                    }

                    onClicked: {
                        active(ShortcutPlugin.Click);
                    }
                }
            }

            UkuiItems.TouchSlider {
                id: control;
                Layout.fillWidth: true;
                Layout.preferredHeight: mainWindow.isTabletMode ? 40 : 16;
                Layout.alignment: Qt.AlignVCenter;
                value: buttonValue / 100;
                padding: 0;
                stepSize:0.01;
                backgroundColor: Platform.GlobalTheme.kContainSecondaryAlphaNormal;
                borderColor: Platform.GlobalTheme.kGrayAlpha1;
                enabled: !disabled;

                onPressedChanged: {
                    disableValueUpdate(pressed);
                    //松开鼠标重置buttonValue
                    if (!pressed) {
                        updateValue();
                    }
                }

                onValueChanged: {
                    setValue(Math.round(value * 100));
                }
            }

            Item {
                Layout.preferredWidth: 24
                Layout.preferredHeight: 24
                Layout.alignment: Qt.AlignVCenter
                visible: mainWindow.isTabletMode ? false : true

                UkuiItems.Icon {
                    anchors.fill: parent
                    source: LayoutMirroring.enabled ? "ukui-start-symbolic" : "pan-end-symbolic"
                    mode: UkuiItems.Icon.AutoHighlight
                    visible: widgetId === "" ? false : true

                    MouseArea {
                        anchors.fill: parent;
                        onClicked: widgetPage.showWidget(widgetId, true, name, 250)
                    }
                }
            }
        }
    }
}
