// Squawk messenger. 
// Copyright (C) 2019  Yury Gubich <blue@macaw.me>
// 
// 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 3 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, see <http://www.gnu.org/licenses/>.

#include "clientid.h"

Shared::ClientId::ClientId():
    node(),
    verification(),
    hash()
{}

Shared::ClientId::ClientId(const QString& p_node, const QString& p_ver, const QString& p_hash):
    node(p_node),
    verification(p_ver),
    hash(p_hash)
{}


Shared::ClientId::ClientId(const Shared::ClientId& other):
    node(other.node),
    verification(other.verification),
    hash(other.hash)
{}

Shared::ClientId & Shared::ClientId::operator=(const Shared::ClientId& other) {
    node = other.node;
    verification = other.verification;
    hash = other.hash;

    return *this;
}

bool Shared::ClientId::operator==(const Shared::ClientId& other) const {
    return hash == other.hash && verification == other.verification && node == other.node;
}

bool Shared::ClientId::operator!=(const Shared::ClientId& other) const {
    return hash != other.hash && verification != other.verification && node != other.node;
}

bool Shared::ClientId::operator<(const Shared::ClientId& other) const {
    if (hash < other.hash)
        return true;

    if (hash > other.hash)
        return false;

    if (verification < other.verification)
        return true;

    if (verification > other.verification)
        return false;

    if (node < other.node)
        return true;

    return false;
}

bool Shared::ClientId::operator>(const Shared::ClientId& other) const {
    if (hash > other.hash)
        return true;

    if (hash < other.hash)
        return false;

    if (verification > other.verification)
        return true;

    if (verification < other.verification)
        return false;

    if (node > other.node)
        return true;

    return false;
}

bool Shared::ClientId::operator<=(const Shared::ClientId& other) const {
    if (hash < other.hash)
        return true;

    if (hash > other.hash)
        return false;

    if (verification < other.verification)
        return true;

    if (verification > other.verification)
        return false;

    if (node < other.node)
        return true;

    if (node > other.node)
        return false;

    return true;
}

bool Shared::ClientId::operator>=(const Shared::ClientId& other) const {
    if (hash > other.hash)
        return true;

    if (hash < other.hash)
        return false;

    if (verification > other.verification)
        return true;

    if (verification < other.verification)
        return false;

    if (node > other.node)
        return true;

    if (node < other.node)
        return false;

    return true;
}

QString Shared::ClientId::getId() const {
    return node + "/" + verification;
}

bool Shared::ClientId::valid() const {
    return node.size() > 0 && verification.size() > 0 && hash.size() > 0;
}

QDataStream & Shared::ClientId::operator<<(QDataStream& stream) {
    stream >> node;
    stream >> verification;
    stream >> hash;
    return stream;
}

QDataStream & Shared::ClientId::operator>>(QDataStream& stream) const {
    stream << node;
    stream << verification;
    stream << hash;
    return stream;
}

QDataStream & operator<<(QDataStream& stream, const Shared::ClientId& info) {
    info >> stream;
    return stream;
}

QDataStream & operator>>(QDataStream& stream, Shared::ClientId& info) {
    info << stream;
    return stream;
}