#ifndef MACTOOL
#define MACTOOL

/*
#include <WinSock2.h>
#include <iphlpapi.h>
#include <IPTypes.h>
#include <Windows.h>
#include <string>
*/
#include "QsLog.h"
#include <QNetworkInterface>
#include <QString>

class MacTool
{
public:

    static int GetMacByAdaptersAddresses(std::string& macOut, std::string& ipOut)
    {
        int ret = 0;
        QString macAddress ="";
        QStringList macAddressList;
        QString ipAddress ="";
        QStringList ipAddressList;
        auto interfaces = QNetworkInterface::allInterfaces();
        auto hostAddresses = QNetworkInterface::allAddresses();
        // 在线网卡可能存在多个，故还是需要像 upp 一样，将在线全部网卡进行排序后，取第一个
        QLOG_INFO()<<QString("GetMacByAdaptersAddresses:: interfaces size: %1 ,  hostAddresses: %2 ").arg( interfaces.size() ).arg( hostAddresses.size() );
        for (int i = 0; i < interfaces.length(); i++) {
            if ( interfaces[i].isValid() ) {
                if( interfaces[i].flags().testFlag(QNetworkInterface::IsUp) && interfaces[i].flags().testFlag(QNetworkInterface::IsRunning) &&
                    !interfaces[i].flags().testFlag(QNetworkInterface::IsLoopBack)
                    && interfaces[i].hardwareAddress() >12 )
                {
                    QLOG_INFO()<<QString("GetMacByAdaptersAddresses:: interfaces[ %1 ] = %2").arg( i+1 ).arg( interfaces[i].hardwareAddress() );
                    macAddressList.append( interfaces[i].hardwareAddress() );
                }
            }
        }

        // MAC地址列表大于1时(需要进行升序排序);
        if ( macAddressList.size() > 1 ) {
            qSort(macAddressList.begin(), macAddressList.end(), [] (const QString& s1, const QString& s2) {
                return s1 < s2;
            } );
            macAddress = macAddressList.first();
        // MAC地址列表等于1时(无需排序,直接取唯一的值)
        } else if ( macAddressList.size() == 1 ) {
            macAddress = macAddressList.first();
        } else {
            // Do Nothing;
        }

        // ip 地址可能存在多个，故还是需要像 upp 一样，将全部 ip 地址进行排序后，取第一个
        for (int i = 0; i < hostAddresses.length(); i++) {
            // if(address.protocol() == QAbstractSocket::IPv4Protocol) {  }   //1.1.1.1 > length > 7
            QHostAddress  hadr = hostAddresses[i];
            QLOG_INFO()<<QString("GetMacByAdaptersAddresses::  hostAddresses: %1 ").arg( hadr.toString() );
            if ( hadr.protocol() ==  QAbstractSocket::IPv4Protocol && hadr.toString().length() > 7 && !hadr.toString().contains("127.0.0.1")
                 && !hadr.toString().startsWith("192.168.", Qt::CaseInsensitive)  // 新增限制
                 && !hadr.toString().startsWith("172.", Qt::CaseInsensitive)   // 新增限制
                 )
            {
                QLOG_INFO()<<QString("GetMacByAdaptersAddresses:: hostAddresses[ %1 ] = %2 , protocol: %3")
                             .arg( i+1 ).arg( hadr.toString() ).arg( hadr.protocol() );
                ipAddressList.append( hadr.toString() );
            }
        }
        // IP地址列表大于1时(需要进行升序排序);
        if ( ipAddressList.size() > 1 ) {
            qSort(ipAddressList.begin(), ipAddressList.end(), [] (const QString& s1, const QString& s2) {
                return s1 < s2;
            } );
            ipAddress = ipAddressList.first();
            // MAC地址列表等于1时(无需排序,直接取唯一的值)
        } else if ( ipAddressList.size() == 1 ) {
            ipAddress = ipAddressList.first();
        } else {
            // Do Nothing;
        }

        if ( macAddress.isEmpty() || ipAddress.isEmpty() ) {
            QLOG_ERROR()<<QString("GetMacByAdaptersAddresses:: Local Hardware Address Acquisition Failed || Local IP Address Failed");
        } else {
            ret = 1;
            macAddress = macAddress.replace(QRegExp(":"), "-");
            macOut = macAddress.toStdString();
            ipOut = ipAddress.toStdString();
            QLOG_INFO()<<QString("GetMacByAdaptersAddresses::MAC: %1 , IP: %2").arg( macAddress ).arg(ipAddress);
        }
        return ret;

    }

};

#endif // MACTOOL

