/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *  Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io>                   *
 *                                                                         *
 *  This file is part of nymea.                                            *
 *                                                                         *
 *  This library is free software; you can redistribute it and/or          *
 *  modify it under the terms of the GNU Lesser General Public             *
 *  License as published by the Free Software Foundation; either           *
 *  version 2.1 of the License, or (at your option) any later version.     *
 *                                                                         *
 *  This library 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      *
 *  Lesser General Public License for more details.                        *
 *                                                                         *
 *  You should have received a copy of the GNU Lesser General Public       *
 *  License along with this library; If not, see                           *
 *  <http://www.gnu.org/licenses/>.                                        *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "devicepluginwemo.h"

#include "devices/device.h"
#include "plugininfo.h"
#include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h"

#include <QDebug>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QXmlStreamAttributes>

DevicePluginWemo::DevicePluginWemo()
{
}

DevicePluginWemo::~DevicePluginWemo()
{
    hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
}

void DevicePluginWemo::init()
{
    m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
    connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginWemo::onPluginTimer);

    connect(hardwareManager()->upnpDiscovery(), &UpnpDiscovery::upnpNotify, this, &DevicePluginWemo::onUpnpNotifyReceived);
}

Device::DeviceError DevicePluginWemo::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{
    Q_UNUSED(params);
    Q_UNUSED(deviceClassId)

    UpnpDiscoveryReply *reply = hardwareManager()->upnpDiscovery()->discoverDevices("upnp:rootdevice");
    connect(reply, &UpnpDiscoveryReply::finished, this, &DevicePluginWemo::onUpnpDiscoveryFinished);
    return Device::DeviceErrorAsync;
}

Device::DeviceSetupStatus DevicePluginWemo::setupDevice(Device *device)
{
    if (device->deviceClassId() != wemoSwitchDeviceClassId) {
        return Device::DeviceSetupStatusFailure;
    }

    refresh(device);
    return Device::DeviceSetupStatusSuccess;
}

Device::DeviceError DevicePluginWemo::executeAction(Device *device, const Action &action)
{
    if (device->deviceClassId() != wemoSwitchDeviceClassId)
        return Device::DeviceErrorDeviceClassNotFound;

    // Set power
    if (action.actionTypeId() == wemoSwitchPowerActionTypeId) {
        // Check if wemo device is reachable
        if (device->stateValue(wemoSwitchConnectedStateTypeId).toBool()) {
            // setPower returns false, if the curent powerState is already the new powerState
            if (setPower(device, action.param(wemoSwitchPowerActionPowerParamTypeId).value().toBool(), action.id())) {
                return Device::DeviceErrorAsync;
            } else {
                return Device::DeviceErrorNoError;
            }
        } else {
            return Device::DeviceErrorHardwareNotAvailable;
        }
    }

    return Device::DeviceErrorActionTypeNotFound;
}

void DevicePluginWemo::deviceRemoved(Device *device)
{
    // Check if there is a missing reply for this device
    foreach (Device *d, m_refreshReplies.values()) {
        if (d->id() == device->id()) {
            QNetworkReply * reply = m_refreshReplies.key(device);
            m_refreshReplies.remove(reply);
            // Note: delete will be done in networkManagerReplyReady()
        }
    }
    foreach (Device *d, m_setPowerReplies.values()) {
        if (d->id() == device->id()) {
            QNetworkReply * reply = m_setPowerReplies.key(device);
            m_setPowerReplies.remove(reply);
            // Note: delete will be done in networkManagerReplyReady()
        }
    }
}

void DevicePluginWemo::refresh(Device *device)
{
    QByteArray getBinarayStateMessage("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>");

    QNetworkRequest request;
    request.setUrl(QUrl("http://" + device->paramValue(wemoSwitchDeviceHostParamTypeId).toString() + ":" + device->paramValue(wemoSwitchDevicePortParamTypeId).toString() + "/upnp/control/basicevent1"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=\"utf-8\""));
    request.setHeader(QNetworkRequest::UserAgentHeader,QVariant("nymea"));
    request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#GetBinaryState\"");

    QNetworkReply *reply = hardwareManager()->networkManager()->post(request, getBinarayStateMessage);
    connect(reply, &QNetworkReply::finished, this, &DevicePluginWemo::onNetworkReplyFinished);
    m_refreshReplies.insert(reply, device);
}

bool DevicePluginWemo::setPower(Device *device, const bool &power, const ActionId &actionId)
{
    // check if the power would change...
    if (device->stateValue(wemoSwitchPowerStateTypeId).toBool() == power) {
        return false;
    }

    QByteArray setPowerMessage("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>" + QByteArray::number((int)power) + "</BinaryState></u:SetBinaryState></s:Body></s:Envelope>");

    QNetworkRequest request;
    request.setUrl(QUrl("http://" + device->paramValue(wemoSwitchDeviceHostParamTypeId).toString() + ":" + device->paramValue(wemoSwitchDevicePortParamTypeId).toString() + "/upnp/control/basicevent1"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=\"utf-8\""));
    request.setHeader(QNetworkRequest::UserAgentHeader,QVariant("nymea"));
    request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");

    QNetworkReply *reply = hardwareManager()->networkManager()->post(request, setPowerMessage);
    connect(reply, &QNetworkReply::finished, this, &DevicePluginWemo::onNetworkReplyFinished);
    m_setPowerReplies.insert(reply, device);
    m_runningActionExecutions.insert(reply, actionId);
    return true;
}


void DevicePluginWemo::processRefreshData(const QByteArray &data, Device *device)
{
    if (data.contains("<BinaryState>0</BinaryState>")) {
        device->setStateValue(wemoSwitchPowerStateTypeId, false);
        device->setStateValue(wemoSwitchConnectedStateTypeId, true);
    } else if (data.contains("<BinaryState>1</BinaryState>")) {
        device->setStateValue(wemoSwitchPowerStateTypeId, true);
        device->setStateValue(wemoSwitchConnectedStateTypeId, true);
    } else {
        device->setStateValue(wemoSwitchConnectedStateTypeId, false);
    }
}

void DevicePluginWemo::processSetPowerData(const QByteArray &data, Device *device, const ActionId &actionId)
{
    if (data.contains("<BinaryState>1</BinaryState>") || data.contains("<BinaryState>0</BinaryState>")) {
        emit actionExecutionFinished(actionId, Device::DeviceErrorNoError);
        device->setStateValue(wemoSwitchConnectedStateTypeId, true);
        refresh(device);
    } else {
        device->setStateValue(wemoSwitchConnectedStateTypeId, false);
        emit actionExecutionFinished(actionId, Device::DeviceErrorHardwareNotAvailable);
    }
}

void DevicePluginWemo::onNetworkReplyFinished()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    // check HTTP status code
    if (status != 200 || reply->error() != QNetworkReply::NoError) {
        qCWarning(dcWemo()) << "Request error:" << status << reply->errorString();
        reply->deleteLater();
        return;
    }

    if (m_refreshReplies.contains(reply)) {
        QByteArray data = reply->readAll();
        Device *device = m_refreshReplies.take(reply);
        processRefreshData(data, device);
    } else if (m_setPowerReplies.contains(reply)) {
        QByteArray data = reply->readAll();
        Device *device = m_setPowerReplies.take(reply);
        ActionId actionId = m_runningActionExecutions.take(reply);
        processSetPowerData(data, device, actionId);
    }

    reply->deleteLater();
}

void DevicePluginWemo::onPluginTimer()
{
    foreach (Device* device, myDevices()) {
        refresh(device);
    }
}

void DevicePluginWemo::onUpnpDiscoveryFinished()
{
    qCDebug(dcWemo()) << "Upnp discovery finished";

    UpnpDiscoveryReply *reply = static_cast<UpnpDiscoveryReply *>(sender());
    if (reply->error() != UpnpDiscoveryReply::UpnpDiscoveryReplyErrorNoError) {
        qCWarning(dcWemo()) << "Upnp discovery error" << reply->error();
    }
    reply->deleteLater();

    QList<DeviceDescriptor> deviceDescriptors;
    foreach (const UpnpDeviceDescriptor &upnpDeviceDescriptor, reply->deviceDescriptors()) {
        if (upnpDeviceDescriptor.deviceType() == "urn:Belkin:device:controllee:1") {
            DeviceDescriptor descriptor(wemoSwitchDeviceClassId, upnpDeviceDescriptor.friendlyName(), upnpDeviceDescriptor.serialNumber());
            ParamList params;
            params.append(Param(wemoSwitchDeviceHostParamTypeId, upnpDeviceDescriptor.hostAddress().toString()));
            params.append(Param(wemoSwitchDevicePortParamTypeId, upnpDeviceDescriptor.port()));
            params.append(Param(wemoSwitchDeviceSerialParamTypeId, upnpDeviceDescriptor.serialNumber()));
            descriptor.setParams(params);

            foreach (Device *existingDevice, myDevices()) {
                if (existingDevice->paramValue(wemoSwitchDeviceSerialParamTypeId).toString() == upnpDeviceDescriptor.serialNumber()) {
                    descriptor.setDeviceId(existingDevice->id());
                    break;
                }
            }
            deviceDescriptors.append(descriptor);
        }
    }
    emit devicesDiscovered(wemoSwitchDeviceClassId, deviceDescriptors);
}

void DevicePluginWemo::onUpnpNotifyReceived(const QByteArray &notification)
{
    Q_UNUSED(notification)
}
