/*
    Copyright (C) 2012  Dan Vratil <dvratil@redhat.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef QMLSCREEN_H
#define QMLSCREEN_H

#include <QQuickItem>

#include <KF5/KScreen/kscreen/output.h>
#include "qmloutput.h"

class QQmlEngine;

namespace KScreen {
class Output;
class Config;
}

namespace QmlScreen {
    typedef enum {
        Recent = 0,
        Horizontal,
        Vertical,
        None,
        All
    }type;

    struct RectangleDistance{
        type m_type;
        float m_distance;
    };
}
using namespace QmlScreen;

class QMLScreen : public QQuickItem
{
    Q_OBJECT

    Q_PROPERTY(QSize maxScreenSize
               READ maxScreenSize
               CONSTANT)

    Q_PROPERTY(int connectedOutputsCount
               READ connectedOutputsCount
               NOTIFY connectedOutputsCountChanged)

    Q_PROPERTY(int enabledOutputsCount
               READ enabledOutputsCount
               NOTIFY enabledOutputsCountChanged)

    Q_PROPERTY(float outputScale
               READ outputScale
               NOTIFY outputScaleChanged)

public:
    explicit QMLScreen(QQuickItem *parent = nullptr);
    ~QMLScreen() override;

    int connectedOutputsCount() const;
    int enabledOutputsCount() const;

    QMLOutput *primaryOutput() const;
    QList<QMLOutput*> outputs() const;

    QSize maxScreenSize() const;

    float outputScale() const;

    KScreen::ConfigPtr config() const;
    void setConfig(const KScreen::ConfigPtr &config);

    void updateOutputsPlacement();

    void setActiveOutput(QMLOutput *output);

    void setScreenPos(QMLOutput *output, bool isReleased);
    void setScreenPosMulti(int connectScreenNum);
    void setScreenCenterPos();
    struct RectangleDistance calculateRectangleDistance(QMLOutput *qmlOutput1, QMLOutput *qmlOutput2);
    void resetOutputPos(QMLOutput *output, type m_type, QMLOutput *outputRef);
    
public Q_SLOTS:
    void setActiveOutput()
    {
        setActiveOutput(qobject_cast<QMLOutput *>(sender()));
    }

    void setActiveOutputByCombox(int screenId);
    void setScreenPos(bool isReleased)
    {
        setScreenPos(qobject_cast<QMLOutput*>(sender()), isReleased);
    }
    void getClickedPos();

Q_SIGNALS:
    void connectedOutputsCountChanged();
    void enabledOutputsCountChanged();

    void outputScaleChanged();

    void focusedOutputChanged(QMLOutput *output);

    void released();

private Q_SLOTS:
    void addOutput(const KScreen::OutputPtr &output);
    void removeOutput(int outputId);

    void outputConnectedChanged();
    void outputEnabledChanged();
    void outputPositionChanged();

    void viewSizeChanged();

private:
    void qmlOutputMoved(QMLOutput *qmlOutput);
    void updateCornerOutputs();
    void setOutputScale(float scale);

    KScreen::ConfigPtr m_config;
    QHash<KScreen::OutputPtr, QMLOutput *> m_outputMap;
    QVector<QMLOutput*> m_manuallyMovedOutputs;
    int m_connectedOutputsCount = 0;
    int m_enabledOutputsCount = 0;

    float m_outputScale = 1.0 / 14.0;// 缩放比例

    QList<qreal> leftPosList;
    QList<qreal> rightPosList;
    qreal leftX = 0.0;
    qreal topY  = 0.0;
};

#endif // QMLSCREEN_H
