/*
 *  SPDX-FileCopyrightText: 2025 Ross Rosales <ross.erosales@gmail.com>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef _KIS_SELECTION_ACTIONS_PANEL_H_
#define _KIS_SELECTION_ACTIONS_PANEL_H_

#include "kis_config.h"
#include "kis_types.h"
#include <QColor>
#include <QObject>
#include <QPointF>
#include <QPushButton>

#include "KoPointerEvent.h"
#include "KoSnapGuide.h"
#include "canvas/kis_canvas_decoration.h"
#include "kis_icon_utils.h"
#include "kis_painting_assistant.h"
#include <kritaui_export.h>

class KisCanvas2;
class KisCoordinatesConverter;
class KisViewManager;

class KisSelectionActionsPanel;
typedef KisSharedPtr<KisSelectionActionsPanel> KisSelectionActionsPanelSP;

using Orientation = KisConfig::SelectionActionsBarOrientation;
using Position = KisConfig::SelectionActionsBarPosition;
using Behavior = KisConfig::SelectionActionsBarBehavior;

class KRITAUI_EXPORT KisSelectionActionsPanel : public QObject
{
    Q_OBJECT
public:
    KisSelectionActionsPanel() = delete;
    KisSelectionActionsPanel(KisViewManager *viewManager);
    ~KisSelectionActionsPanel();

    void draw(QPainter &painter);
    void setVisible(bool visible);
    void setEnabled(bool enabled);
    bool eventFilter(QObject *obj, QEvent *event) override;
    void setOrientation(Orientation orientation);
    void setHandleEnabled(bool enabled);

    void canvasWidgetChanged(KisCanvasWidgetBase* canvas);

    void updatePositioning();

private Q_SLOTS:
    void showContextMenu(const QPoint& pos);
    void disableSelectionActionsPanel();
    void configureSelectionActionsPanel();
    void configChanged(bool skipResettingOffset = false);
    void canvasStateChanged();
    void themeChanged();

private:
    void recalculateDimensions();

    /// Positioning ///
    QPoint getFixedPosition() const;

    QRectF getWidgetSelectionRect() const;

    QPoint horizontalFreeFloatingTopLeftPosition(bool calculateOnlyAnchor = false) const;
    QPoint verticalFreeFloatingTopLeftPosition(bool calculateOnlyAnchor = false) const;
    QPoint freeFloatingInitialTopLeftPosition(bool calculateOnlyAnchor = false) const;

    QPoint clipPositionToCanvasBoundaries(QPoint position, QWidget *canvasWidget) const;
    QPoint currentTopLeftPosition() const;
    /// End of positioning ///


    void drawAnchorWhileMoving(QPainter &painter) const;
    void drawActionBarBackground(QPainter &gc) const;

    void drawDebugRectangle(QPainter &painter, Position position);
    void drawDebugRectanglesForFreeFloatingBehaviour(QPainter &painter);

    bool handlePress(QEvent *event, const QPoint &pos, Qt::MouseButton button = Qt::LeftButton);
    bool handleMove(QEvent *event, const QPoint &pos);
    bool handleRelease(QEvent *event);

    ///Moves all the widgets that are a part of the panel
    void movePanelWidgets();

    QPoint mouseEventPos(const QMouseEvent *mouseEvent);
    QPoint tabletEventPos(const QTabletEvent *tabletEvent);
    // Touch events can have zero touch points when pressing an entire palm on
    // the screen on Android, so this returns false if this is a non-touch.
    bool touchEventPos(const QTouchEvent *touchEvent, QPoint &outPos);

    QPoint transformHandleCoords(QPoint pos);

    struct Private;
    QScopedPointer<Private> d;
};

#endif
