/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 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.
 *
 */

#include "dbusadaptor.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>

const QByteArray GSETTINGS_SCHEMA_KYLIN_NM = "org.ukui.kylin-nm.switch";
const QString    KEY_WIRELESS_SWITCH          = "wirelessswitch";
const QString    KEY_WIRED_SWITCH             = "wiredswitch";

/*
 * Implementation of adaptor class DbusAdaptor
 */

DbusAdaptor::DbusAdaptor(MainWindow *parent)
    : QDBusAbstractAdaptor(parent)
{
    // constructor
    qDBusRegisterMetaType<QMap<QString, bool> >();
    qDBusRegisterMetaType<QMap<QString, int> >();
    qDBusRegisterMetaType<QVector<QStringList> >();
    qDBusRegisterMetaType<QMap<QString, QVector<QStringList> >>();
    //setAutoRelaySignals(true)后会自动转发mainwindow发出的同名信号，因此不必再额外写一个转发
    setAutoRelaySignals(true);
}

DbusAdaptor::~DbusAdaptor()
{
    // destructor
}

//无线列表
QMap<QString, QVector<QStringList> > DbusAdaptor::getWirelessList()
{
    QMap<QString, QVector<QStringList> > map;
    parent()->getWirelessList(map);
    return map;
}

bool DbusAdaptor::getWirelessSwitchBtnState()
{
    return parent()->getWirelessSwitchBtnState();
}

//有线列表
QMap<QString, QVector<QStringList>> DbusAdaptor::getWiredList()
{
    QMap<QString, QVector<QStringList>> map;
    parent()->getWiredList(map);
    return map;
}

//有线开关
void DbusAdaptor::setWiredSwitchEnable(bool enable)
{
    //todo mainwindow调用backend 对开关 打开/关闭
    if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_KYLIN_NM)) {
        QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_KYLIN_NM);
        if (gsetting->get(KEY_WIRED_SWITCH).toBool() != enable) {
            gsetting->set(KEY_WIRED_SWITCH, enable);
        }
        delete gsetting;
        gsetting = nullptr;
    } else {
        qDebug()<<"isSchemaInstalled false";
    }
}

//无线开关
void DbusAdaptor::setWirelessSwitchEnable(bool enable)
{
    //todo mainwindow调用backend 对开关 打开/关闭
//    if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_KYLIN_NM)) {
//        QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_KYLIN_NM);
//        if (gsetting->get(KEY_WIRELESS_SWITCH).toBool() != enable) {
//            gsetting->set(KEY_WIRELESS_SWITCH, enable);
//        }
//        delete gsetting;
//        gsetting = nullptr;
//    } else {
//        qDebug()<<"isSchemaInstalled false";
//    }
    parent()->setWirelessSwitchEnable(enable);
}

//启用/禁用网卡
void DbusAdaptor::setDeviceEnable(QString devName, bool enable)
{
    parent()->setWiredDeviceEnable(devName, enable);
}

//设置默认网卡
//void DbusAdaptor::setDefaultWiredDevice(QString deviceName)
//{
//    if (!checkDeviceExist(WIRED, deviceName)) {
//        return;
//    }
//    setDefaultDevice(WIRED, deviceName);
//    parent()->setWiredDefaultDevice(deviceName);
//    return;
//}

//QString DbusAdaptor::getDefaultWiredDevice()
//{
//    QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat);
//    m_settings->beginGroup("DEFAULTCARD");
//    QString key("wired");
//    QString deviceName = m_settings->value(key, "").toString();
//    m_settings->endGroup();
//    delete m_settings;
//    m_settings = nullptr;
//    return deviceName;
//}

//void DbusAdaptor::setDefaultWirelessDevice(QString deviceName)
//{
//    if (!checkDeviceExist(WIRED, deviceName)) {
//        return;
//    }
//    setDefaultDevice(WIRELESS, deviceName);
//    parent()->setWirelessDefaultDevice(deviceName);
//    return;
//}

//QString  DbusAdaptor::getDefaultWirelessDevice()
//{
//    QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat);
//    m_settings->beginGroup("DEFAULTCARD");
//    QString key("wireless");
//    QString deviceName = m_settings->value(key, "").toString();
//    m_settings->endGroup();
//    delete m_settings;
//    m_settings = nullptr;
//    return deviceName;
//}

//删除
void DbusAdaptor::deleteConnect(int type, QString ssid)
{
    if (type == WIRED) {
        parent()->deleteWired(ssid);
    } else if (type == WIRELESS) {
        //待实现
    } else {
        qDebug() << "[DbusAdaptor] deleteConnect type is invalid";
    }
}

//连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid
void DbusAdaptor::activateConnect(int type, QString devName, QString ssid)
{
    if (type == WIRED) {
        parent()->activateWired(devName,ssid);
    } else if (type == WIRELESS) {
        parent()->activateWireless(devName,ssid);
    } else {
        qDebug() << "[DbusAdaptor] activateConnect type is invalid";
    }
}

//断开连接 根据网卡类型 参数1 0:lan 1:wlan 参数3 为ssid/uuid
void DbusAdaptor::deActivateConnect(int type, QString devName, QString ssid)
{
    if (type == WIRED) {
        qDebug()  << "deactivateWired";
        parent()->deactivateWired(devName,ssid);
    } else if (type == WIRELESS) {
        parent()->deactivateWireless(devName,ssid);
    } else {
        qDebug() << "[DbusAdaptor] deactivateConnect type is invalid";
    }
}

//获取设备列表和启用/禁用状态
QMap<QString, bool> DbusAdaptor::getDeviceListAndEnabled(int devType)
{
    QMap<QString, bool> map;
    map.clear();
    getDeviceEnableState(devType, map);
    return map;
}

//获取无线设备能力
QMap<QString, int> DbusAdaptor::getWirelessDeviceCap()
{
    QMap<QString, int> map;
    parent()->getWirelessDeviceCap(map);
    return map;
}

//唤起属性页 根据网卡类型 参数2 为ssid/uuid
void DbusAdaptor::showPropertyWidget(QString devName, QString ssid)
{
    qDebug() << "showPropertyWidget";
    parent()->showPropertyWidget(devName,ssid);
}

//唤起新建有线连接界面
void DbusAdaptor::showCreateWiredConnectWidget(QString devName)
{
    qDebug() << "showCreateWiredConnectWidget";
    parent()->showCreateWiredConnectWidget(devName);
}

//唤起加入其他无线网络界面
void DbusAdaptor::showAddOtherWlanWidget(QString devName)
{
    qDebug() << "showAddOtherWlanWidget";
    parent()->showAddOtherWlanWidget(devName);
}

//开启热点
void DbusAdaptor::activeWirelessAp(const QString apName, const QString apPassword, const QString band, const QString apDevice)
{
    parent()->activeWirelessAp(apName, apPassword, band, apDevice);
}

//断开热点
void DbusAdaptor::deactiveWirelessAp(const QString apName, const QString uuid)
{
    parent()->deactiveWirelessAp(apName, uuid);
}

//获取热点
QStringList DbusAdaptor::getStoredApInfo()
{
    QStringList list;
    list.clear();
    parent()->getStoredApInfo(list);
    return list;
}

//获取热点path
QString DbusAdaptor::getApConnectionPath(QString uuid)
{
    QString path;
    path.clear();
    parent()->getApConnectionPath(path, uuid);
    return path;
}

//获取热点path
QString DbusAdaptor::getActiveConnectionPath(QString uuid)
{
    QString path;
    path.clear();
    parent()->getActiveConnectionPath(path, uuid);
    return path;
}

QStringList DbusAdaptor::getApInfoBySsid(QString devName, QString ssid)
{
    QStringList list;
    list.clear();
    parent()->getApInfoBySsid(devName, ssid, list);
    return list;
}

void DbusAdaptor::showKylinNM(int type)
{
    parent()->onShowMainWindow(type);
}

//扫描
void DbusAdaptor::reScan()
{
    parent()->rescan();
}

void DbusAdaptor::keyRingInit()
{
    parent()->keyRingInit();
}

void DbusAdaptor::keyRingClear()
{
    parent()->keyRingClear();
}
