// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef IPCONFILCTCHECKER_H
#define IPCONFILCTCHECKER_H

#include <QObject>
#include "netutils.h"

#include "networkconst.h"

class QThread;

namespace dde {
namespace network {

class NetworkDeviceBase;
class NetworkProcesser;
class DeviceIPChecker;
class NetworkDBusProxy;

class IPConfilctChecker : public QObject
{
    Q_OBJECT

Q_SIGNALS:
    void conflictStatusChanged(NetworkDeviceBase *, const bool);

public:
    explicit IPConfilctChecker(NetworkProcesser *networkProcesser, const bool ipChecked, QObject *parent = nullptr);
    ~IPConfilctChecker();

    void release();

private Q_SLOT:
    void onDeviceAdded(QList<NetworkDeviceBase *> devices);
    void onIPConfilct(const QString &ip, const QString &macAddress);
    void onSenderIPInfo(const QStringList &ips);

private:
    void handlerIpConflict(const QString &ip, const QString &macAddress, const QString &activeConnectionInfo);
    QMap<QString, NetworkDeviceBase *> parseDeviceIp(const QString &activeConnectionInfo);
    void clearUnExistDevice();

private:
    NetworkDBusProxy *m_networkInter;
    NetworkProcesser *m_networkProcesser;
    QList<DeviceIPChecker *> m_deviceCheckers;
    bool m_ipNeedCheck;
    QThread *m_thread;
};

class DeviceIPChecker : public QObject
{
    Q_OBJECT

Q_SIGNALS:
    void conflictStatusChanged(NetworkDeviceBase *, const bool);
    void ipConflictCheck(const QStringList &);

public:
    explicit DeviceIPChecker(NetworkDeviceBase *device, NetworkDBusProxy *netInter, QObject *parent);
    ~DeviceIPChecker();
    NetworkDeviceBase *device();
    void setDeviceInfo(const QStringList &ipv4, const QString &macAddress);
    void handlerIpConflict();
    QStringList ipV4();
    bool ipConflicted();

private:
    NetworkDeviceBase *m_device;
    NetworkDBusProxy *m_networkInter;
    QStringList m_ipV4;
    QString m_macAddress;
    int m_conflictCount;
    int m_clearCount;
    int m_count;
    bool m_ipConflicted;
    QList<QStringList> m_changeIpv4s;
};

}
}

#endif // IPCONFILCTCHECKER_H
