/*
    PIP - Platform Independent Primitives
    PICloud Client
    Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

    This program 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 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "picloudclient.h"

#include "picloudtcp.h"
#include "pitranslator.h"


PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode), PICloudBase() {
	tcp.setRole(PICloud::TCP::Client);
	setThreadedReadBufferSize(eth.threadedReadBufferSize());
	setName("cloud_client");
	is_connected = false;
	is_deleted   = false;
	//	setReopenEnabled(false);
	CONNECTL(&eth, connected, [this]() {
		opened_ = true;
		tcp.sendStart();
	});
	CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed);
	CONNECTL(&eth, disconnected, [this](bool) {
		if (is_deleted) return;
		bool need_disconn = is_connected;
		// piCoutObj << "eth disconnected";
		eth.stop();
		opened_ = false;
		internalDisconnect();
		if (need_disconn) disconnected();
		// piCoutObj << "eth disconnected done";
	});
}


PICloudClient::~PICloudClient() {
	// piCoutObj << "~PICloudClient() ..." << this;
	is_deleted = true;
	stopAndWait();
	close();
	internalDisconnect();
	// piCoutObj << "~PICloudClient() done" << this;
}


void PICloudClient::setServerName(const PIString & server_name) {
	setName("cloud_client__" + server_name);
	tcp.setServerName(server_name);
}


void PICloudClient::setKeepConnection(bool on) {
	eth.setParameter(PIEthernet::KeepConnection, on);
}


void PICloudClient::interrupt() {
	cond_buff.notifyOne();
	cond_connect.notifyOne();
	eth.interrupt();
}


bool PICloudClient::openDevice() {
	// piCoutObj << "open";// << path();
	bool op = eth.connect(PINetworkAddress::resolve(path()), false);
	if (op) {
		mutex_connect.lock();
		eth.startThreadedRead();
		// piCoutObj << "connecting...";
		bool conn_ok = cond_connect.waitFor(mutex_connect, eth.readTimeout());
		// piCoutObj << "conn_ok" << conn_ok << is_connected;
		mutex_connect.unlock();
		if (!conn_ok) {
			mutex_connect.lock();
			eth.stopAndWait();
			eth.close();
			mutex_connect.unlock();
		}
		return is_connected;
	} else {
		// eth.close();
		return false;
	}
}


bool PICloudClient::closeDevice() {
	// PIThread::stop();
	if (is_connected) {
		internalDisconnect();
	}
	eth.stopAndWait();
	eth.close();
	return true;
}


ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
	if (is_deleted || max_size <= 0) return -1;
	// piCoutObj << "readDevice ...";
	if (!is_connected && eth.isClosed()) openDevice();
	ssize_t sz = -1;
	mutex_buff.lock();
	if (is_connected) {
		if (buff.isEmpty()) {
			sz = 0;
		} else {
			sz = piMin<ssize_t>(max_size, buff.size_s());
			memcpy(read_to, buff.data(), sz);
			buff.remove(0, sz);
		}
		if (sz == 0) cond_buff.wait(mutex_buff);
	}
	mutex_buff.unlock();
	if (!is_connected) opened_ = false;
	// piCoutObj << "readDevice done" << sz;
	return sz;
}


ssize_t PICloudClient::writeDevice(const void * data, ssize_t size) {
	if (is_deleted || !is_connected) return -1;
	// piCoutObj << "writeDevice" << size;
	return tcp.sendData(PIByteArray(data, size));
}


void PICloudClient::internalDisconnect() {
	// piCoutObj << "internalDisconnect";
	is_connected = false;
	cond_buff.notifyOne();
	cond_connect.notifyOne();
	streampacker.clear();
	buff.clear();
}


void PICloudClient::_readed(PIByteArray & ba) {
	if (is_deleted) return;
	PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
	// piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
	if (hdr.second == tcp.role()) {
		switch (hdr.first) {
		case PICloud::TCP::Connect:
			if (tcp.parseConnect(ba) == 1) {
				mutex_connect.lock();
				is_connected = true;
				mutex_connect.unlock();
				cond_connect.notifyOne();
				connected();
			}
			break;
		case PICloud::TCP::Disconnect:
			eth.stop();
			opened_ = false;
			eth.close();
			break;
		case PICloud::TCP::Data:
			if (is_connected) {
				mutex_buff.lock();
				if (buff.size_s() > threadedReadBufferSize()) {
					piCoutObj << "Error: buffer overflow, drop %1 bytes"_tr("PICloud").arg(ba.size());
					mutex_buff.unlock();
					return;
				}
				buff.append(ba);
				mutex_buff.unlock();
				cond_buff.notifyOne();
			}
			break;
		default: break;
		}
		// piCoutObj << "readed" << ba.toHex();
	}
	// piCoutObj << "_readed done";
}
