/* -*- 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 <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include <QThread>
#include <QObject>
#include "../../libnm-kylin/libnm-base-kylin/kylinnetworkmanager.h"
#include "stubext.h"

using namespace stub_ext;

class NetWiredTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        thread = new QThread;
		manager = new KyNetworkManager();
		manager->moveToThread(thread);
        QObject::connect(thread, &QThread::started, manager, &KyNetworkManager::kylinNetworkManagerInit);
        QObject::connect(thread, &QThread::finished, manager, &KyNetworkManager::deleteLater);
        QObject::connect(thread, &QThread::finished, [=](){
			qDebug() << "NetConnect thread quit";
		});
		thread->start();
        sleep(2);
    }

    static void TearDownTestSuite()
    {
	if (thread) {
			thread->quit();
			thread->wait();
			delete thread;
	}

    }

    static KyNetworkManager *manager;
    static QThread *thread;
   static KyWiredConnectOperation* moperate;

};

KyNetworkManager *NetWiredTest::manager = nullptr;
QThread *NetWiredTest::thread = nullptr;
KyWiredConnectOperation *NetWiredTest::moperate=nullptr;
TEST_F(NetWiredTest, getWireDevice)
{
    KyConnectStatus connectType;
    QMap<QString,QList<KyWiredItem>> map;
    if (!manager) {
       return;
    }
   bool bEnable=manager->getNetworkingEnabled();
    EXPECT_TRUE(bEnable);
    bEnable=manager->getWiredEnabled();
    manager->getConnectStatus(connectType);
    //ASSERT_EQ(newValue, false);
    QStringList wiredDevList;
    wiredDevList.clear();
    manager->getNetworkDeviceList(DEVICE_TYPE_ETHERNET, wiredDevList);
    EXPECT_FALSE(wiredDevList.isEmpty());
    qDebug() << wiredDevList;
    printf("3333\n");
	if (!wiredDevList.isEmpty())
	{
        for (int i = 0; i < wiredDevList.size(); ++i)
        {
            KyConnectivity connectivity;
            QString devName = wiredDevList.at(i);

            KyWirelessConnectSetting connetSetting;
            connetSetting.setIfaceName(devName);
            connetSetting.setIpConfigType(IPADDRESS_V4, CONFIG_IP_DHCP);
            connetSetting.m_connectName=QString("mqtest");

            moperate->createWiredConnect(connetSetting);
            //manager->onCreateWiredConnect(connetSetting);

            qDebug() << devName;
            if( manager->getDeviceManaged(devName))
            {
               manager->getDeviceState(devName);
               manager->getDeviceConnectivity(devName,connectivity);
            }
		}
	}
}

TEST_F(NetWiredTest, WireConnect)
{
    QMap<QString,QList<KyWiredItem>> map;
    KyConnectOperation operate;
    KyConnectSetting connectSetting;
    KyDetailInfo detailConnectSetting;
    QList<KyActivateItem> connectItemList;
    if (!manager) {
       return;
    }
    manager->getWiredList(map);

    EXPECT_FALSE(map.isEmpty());
    QMap<QString, QList<KyWiredItem> >::iterator iter = map.begin();
    while (iter != map.end()) {

       QList<KyWiredItem>listCon =iter.value();
       manager->getActiveConnectionList(iter.key(),CONNECT_TYPE_WIRED, connectItemList);
        qDebug() <<"device name "<<iter.key();
       for (int i = 0; i < listCon.length(); i++) {
           qDebug() << "[NetConnect]m_connectName " << listCon.at(i).m_connectName;
           manager->getWiredConnectDetail(iter.key(),listCon.at(i).m_connectUuid,detailConnectSetting);
           manager->getConnectIpInfo(listCon.at(i).m_connectUuid,connectSetting);
           if(listCon.at(i).m_connectName==QString("mqtest"))
           {
               manager->onDeactivateConnection(listCon.at(i).m_connectUuid);
               manager->onDeactivateConnection(listCon.at(i).m_connectUuid);
               manager->onDeleteConnect(listCon.at(i).m_connectUuid);
           }
       }
        iter++;
    }
}

TEST_F(NetWiredTest, getWirelessList)
{
    KyConnectStatus connectType;
    KyConnectSetting connectSetting;
     KyDetailInfo detailConnectSetting;
     KyWpaPasswordInfo wpainfo;
     KyEapMethodLeapInfo leapinfo;
     KyEapMethodPeapInfo peapinfo;
     KyEapMethodTtlsInfo ttlsinfo;
    if (!manager) {
       return;
    }
    bool bEnable=manager->getNetworkingEnabled();
     EXPECT_TRUE(bEnable);
     bEnable=manager->getWirelessEnabled();
     manager->getConnectStatus(connectType);


    manager->onRequestWirelessScan();
    //ASSERT_EQ(newValue, false);
    QStringList wirelessDevList;
    wirelessDevList.clear();
    manager->getNetworkDeviceList(DEVICE_TYPE_WIFI, wirelessDevList);
    EXPECT_FALSE(wirelessDevList.isEmpty());
    manager->getAcivateWifiSignal();
    qDebug() << wirelessDevList;
    if (!wirelessDevList.isEmpty())
    {
        QList<KyWirelessNetItem> list;
        for (int i = 0; i < wirelessDevList.size(); ++i)
        {
            QString devName = wirelessDevList.at(i);
            qDebug() << devName;
            if( manager->getDeviceManaged(devName))
            {
               manager->getWifiNetworkList(devName, list);
               if(!list.isEmpty())
               {
                 manager->getWirelessConnectDetail(devName, list.at(0).m_NetSsid,list.at(0).m_connectUuid,detailConnectSetting);
                 QString uuid=list.at(0).m_connectUuid;

                 manager->getNormalWifiConnectSecuInfo(uuid, wpainfo);

                 manager->getEnterPriseInfoPeap(uuid, peapinfo);

                 manager->getEnterPriseInfoTtls(uuid, ttlsinfo);

                 manager->getEnterPriseInfoLeap(uuid, leapinfo);



                 qDebug() << list.at(0).m_NetSsid<<list.at(0).m_dbusPath;
                 manager->getConnectIpInfo(list.at(0).m_connectUuid,connectSetting);
               }
            }
        }
    }
}

