/*
 * 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 "abstractparticipant.h"

Models::AbstractParticipant::AbstractParticipant(Models::Item::Type p_type, const QMap<QString, QVariant>& data, Models::Item* parentItem):
    Item(p_type, data, parentItem),
    availability(Shared::Availability::offline),
    lastActivity(data.value("lastActivity").toDateTime()),
    status(data.value("status").toString()),
    client()
{
    QMap<QString, QVariant>::const_iterator itr = data.find("availability");
    if (itr != data.end())
        setAvailability(itr.value().toUInt());

    itr = data.find("client");
    if (itr != data.end())
        setClient(itr.value().value<Shared::ClientId>());
}

Models::AbstractParticipant::AbstractParticipant(const Models::AbstractParticipant& other):
    Item(other),
    availability(other.availability),
    lastActivity(other.lastActivity),
    status(other.status),
    client(other.client)
{}


Models::AbstractParticipant::~AbstractParticipant() {}

int Models::AbstractParticipant::columnCount() const {
    return 5;
}

QVariant Models::AbstractParticipant::data(int column) const {
    switch (column) {
        case 0:
            return Item::data(column);
        case 1:
            return lastActivity;
        case 2:
            return QVariant::fromValue(availability);
        case 3:
            return status;
        case 4:
            return QVariant::fromValue(client);
        default:
            return QVariant();
    }
}

Shared::Availability Models::AbstractParticipant::getAvailability() const {
    return availability;
}

QDateTime Models::AbstractParticipant::getLastActivity() const {
    return lastActivity;
}

QString Models::AbstractParticipant::getStatus() const {
    return status;
}

void Models::AbstractParticipant::setAvailability(Shared::Availability p_avail) {
    if (availability != p_avail) {
        availability = p_avail;
        changed(2);
    }
}

void Models::AbstractParticipant::setAvailability(unsigned int avail) {
    setAvailability(Shared::Global::fromInt<Shared::Availability>(avail));
}

void Models::AbstractParticipant::setLastActivity(const QDateTime& p_time) {
    if (lastActivity != p_time) {
        lastActivity = p_time;
        changed(1);
    }
}

void Models::AbstractParticipant::setStatus(const QString& p_state) {
    if (status != p_state) {
        status = p_state;
        changed(3);
    }
}

Shared::ClientId Models::AbstractParticipant::getClient() const {
    return client;
}

QString Models::AbstractParticipant::getClientNode() const {
    return client.node;
}

void Models::AbstractParticipant::setClient(const Shared::ClientId& id) {
    if (client != id) {
        client = id;
        changed(4);
    }
}


QIcon Models::AbstractParticipant::getStatusIcon(bool big) const {
    return Shared::availabilityIcon(availability, big);
}

void Models::AbstractParticipant::update(const QString& key, const QVariant& value) {
    if (key == "name") {
        setName(value.toString());
    } else if (key == "status") {
        setStatus(value.toString());
    } else if (key == "availability") {
        setAvailability(value.toUInt());
    } else if (key == "lastActivity") {
        setLastActivity(value.toDateTime());
    } else if (key == "client") {
        setClient(value.value<Shared::ClientId>());
    }
}