/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2022 Tianjin KYLIN Information Technology 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef DBUSADAPTOR_H
#define DBUSADAPTOR_H

#include <QtCore/QObject>
#include <QtDBus/QtDBus>
#include <QtDBus/QDBusMetaType>
#include <QStringList>

#include "dbus_adaptor.h"
#include "dbus_interface.h"

#include "tabpage.h"
#include "../dbus-interface/kylinnetworkdeviceresource.h"
QT_BEGIN_NAMESPACE
class QByteArray;
//template<class T> class QList;
template<class Key, class Value> class QMap;
class QString;
class QVariant;
QT_END_NAMESPACE

/*
 * Adaptor class for interface com.kylin.weather
 */
#define SYSTEM_DBUS_SERVICE   "com.kylin.network.qt.systemdbus"
#define SYSTEM_DBUS_PATH      "/"
#define SYSTEM_DBUS_INTERFACE "com.kylin.network.interface"

#include "mainwindow.h"

class DbusAdaptor: public QObject, protected QDBusContext
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "com.kylin.network")
public:
    explicit DbusAdaptor(QString display, MainWindow *m, QObject *parent = nullptr);
    ~DbusAdaptor();

public: // PROPERTIES
public Q_SLOTS: // METHODS
    //无线列表
    QVariantList getWirelessList(QString devName);
    //有线列表
    QVariantList getWiredList(QString devName);
    //有线总开关
    Q_NOREPLY void setWiredSwitchEnable(bool enable);
    //无线总开关
    Q_NOREPLY void setWirelessSwitchEnable(bool enable);
    //有线网卡开关
    Q_NOREPLY void setDeviceEnable(QString devName, bool enable);
    //设置默认网卡
//    Q_NOREPLY void setDefaultWiredDevice(QString deviceName);
//    QString getDefaultWiredDevice();
//    Q_NOREPLY void setDefaultWirelessDevice(QString deviceName);
//    QString  getDefaultWirelessDevice();
    //连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid
    Q_NOREPLY void activateConnect(int type, QString devName, QString ssid);
    //断开连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid
    Q_NOREPLY void deActivateConnect(int type, QString devName, QString ssid);

    //断开连接 根据网卡类型 参数1 0:lan 1:wlan 参数2 true:只断开 false:断开并清除连接的网络信息 参数3 为ssid/uuid
    Q_NOREPLY void deActivateConnectConcise(int type, bool concise, const QString &devName, const QString &ssid);

    //delete network connect
    Q_NOREPLY void deleteConnect(int type,QString Uuid);
    //dev autoconnect
    Q_NOREPLY void setDeviceAutoConnectState(QString devName, bool state);

    //获取设备列表和启用/禁用状态
    QVariantMap getDeviceListAndEnabled(int devType);
    //获取无线设备能力
    QVariantMap getWirelessDeviceCap();
    //唤起属性页 根据网卡类型 参数2 为ssid/uuid
    Q_NOREPLY void showPropertyWidget(QString devName, QString ssid);
    //唤起新建有线连接界面
    Q_NOREPLY void showCreateWiredConnectWidget(QString devName);
    //唤起加入其他无线网络界面
    Q_NOREPLY void showAddOtherWlanWidget(QString devName);
    //开启热点
    void activeWirelessAp(const QString apName, const QString apPassword, const QString band, const QString apDevice);
    //断开热点
    void deactiveWirelessAp(const QString apName, const QString uuid);
    //连接新wifi
    void passwordConnect(QString devName, QString ssid, QString type, QString psk, bool autoConnect);
    //获取热点
    QStringList getStoredApInfo();
    QStringList getApInfoBySsid(QString devName, QString ssid);
    QString getApConnectionPath(QString uuid);
    QString getActiveConnectionPath(QString uuid);
    //wifi扫描
    void reScan();
    //keyring
    void keyRingInit();
    void keyRingClear();
    //just show
    void showKylinNM(int type);

    bool getWirelessSwitchBtnState();
    bool getWiredMainSwitchBtnState();

    int getDeviceConnectivity(const QString deviceName);

    //获取网卡是否插入了网线
    bool getCableStateByDevice(const QString deviceName);
Q_SIGNALS: // SIGNALS
//    void wirelessActivating(QString devName, QString ssid);
//    void wiredActivating(QString devName, QString ssid);
    void lanAdd(QString devName, QStringList info);
    void lanRemove(QString dbusPath);
    void lanUpdate(QString devName, QStringList info);
    void wlanAdd(QString devName, QStringList info);
    void wlanRemove(QString devName,QString ssid);
    void wlanactiveConnectionStateChanged(QString devName, QString ssid, QString uuid, int status);
    void lanActiveConnectionStateChanged(QString devName, QString uuid, int status);
    //仅失败，若成功直接发listUpdate
    void activateFailed(QString errorMessage);
    void deactivateFailed(QString errorMessage);
    //设备插拔
    void deviceStatusChanged();
    void wirelessDeviceStatusChanged();
    void deviceNameChanged(QString oldName, QString newName, int type);
    void wirelessSwitchBtnChanged(bool state);
    void wiredMainSwitchBtnChanged(bool state);
    //热点断开
    void hotspotDeactivated(QString devName, QString ssid);
    //热点连接
    void hotspotActivated(QString devName, QString ssid, QString uuid, QString activePath, QString settingPath);
    //信号强度变化
    void signalStrengthChange(QString devName, QString ssid, int strength);
    //安全性变化
    void secuTypeChange(QString devName, QString ssid, QString secuType);
    //列表排序
    void timeToUpdate();

    void showKylinNMSignal(QString display, int type);

    //唤起属性页 根据网卡类型 参数2 为ssid/uuid
    void showPropertyWidgetSignal(QString display, QString devName, QString ssid);
    //唤起新建有线连接界面
    void showCreateWiredConnectWidgetSignal(QString display, QString devName);
    //唤起加入其他无线网络界面
    void showAddOtherWlanWidgetSignal(QString display, QString devName);

private:
    MainWindow *m_mainWindow;
    QString m_display;
    QDBusServiceWatcher *m_watcher = nullptr;
    NetworkAdaptor* mNetworkAdaptor=nullptr;
    QDBusInterface *m_pSysBusInterfaces;

    QString checkDisplay();
    QString displayFromPid(uint pid);
    void connectToMainwindow();
    bool registerService();
private Q_SLOT:
    void onServiceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner);
    void onWiredMainSwitchBtnChanged(bool);
};

#endif
