/*
    PIP - Platform Independent Primitives
    Test program
    Copyright (C) 2014  Ivan Pelipenko peri4ko@gmail.com

    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/>.
*/
//#define PIP_DEBUG
/*#include "pip.h"


class ElementA: public PIObject {
	PIOBJECT(ElementA)
	// ...
};
ADD_NEW_TO_COLLECTION(ab_group, ElementA)

class ElementB: public PIObject {
	PIOBJECT(ElementB)
	// ...
};
ADD_NEW_TO_COLLECTION(ab_group, ElementB)

class ElementC: public PIObject {
	PIOBJECT(ElementC)
	// ...
};
ADD_NEW_TO_COLLECTION(c_group, ElementC)

class ElementD: public PIObject {
	PIOBJECT(ElementD)
	// ...
};
*/
#include "pip.h"
#include "pivariant.h"





#include "picodeparser.h"
#include "pidir.h"
#include "piconnection.h"

bool readed(void*, uchar * data, int size) {
	piCout << Hex << "readed" << PIByteArray(data, size);
	//piCout << PIString((char*)data, size);
	return true;
}

class A: public PIObject {
	PIOBJECT(A)
public:
	EVENT_HANDLER2(void, cr, const PIString &, from, const PIByteArray &, data) {
		piCout << "A readed" << from << Hex << data;
	}
	EVENT_HANDLER2(void, per, uchar *, data, int, size) {
		piCout << "A readed" << size << ":\"" << PIString((const char *)data, size) << "\"" << NewLine;
		//piCout << "A readed \"";
	}
};

class TC: public PIConnection {
public:
	TC() {
		PIPacketExtractor * pe = addFilter("h&f", addDevice("file://piiodevice.h", PIIODevice::ReadOnly, false), PIPacketExtractor::HeaderAndFooter);
		pe->setHeader(PIString("//!").toByteArray());
		pe->setFooter(PIString("\n").toByteArray());
		pe = addFilter(" h ", "file://piiodevice.h", PIPacketExtractor::Header);
		addChannel(pe, addDevice("file://out.txt", PIIODevice::WriteOnly));
		pe->setHeader(PIString("PI").toByteArray());
		pe->setPayloadSize(3);
		startAllThreadedReads();
	}
	virtual void dataReceived(const PIString & from, const PIByteArray & data) {
		piCout << "dataReceived" << from << (data.size());
	}
	virtual void packetReceived(const PIString & from, const PIByteArray & data) {
		piCout << "packetReceived" << from << (data.size()) << PIString(data);
	}
	virtual bool filterValidatePayload(const PIString & filter_name, uchar * rec, int size) {
		//piCout << "filterValidatePayload" << filter_name << PIString((char*)rec, size);
		if (filter_name == " h ") return PIString((char*)rec, size) == "IOD";
		return false;
	}
};

int main (int argc, char * argv[]) {
	/*A a_;
	PIFile file("piiodevice.h", PIIODevice::ReadOnly);
	PIByteArray header = PIString("PI").toByteArray();
	PIByteArray footer = PIString("}").toByteArray();
	PIPacketExtractor pe(&file);
	//pe.setPacketData(header.data(), header.size_s(), 10);
	pe.setSplitMode(PIPacketExtractor::Footer);
	pe.setHeader(header);
	pe.setFooter(footer);
	pe.setPayloadSize(3);
	pe.setThreadedReadBufferSize(40);
	//pe.setBufferSize(256);
	//pe.setPacketData(0, 0, 20);
	CONNECT2(void, uchar * , int , &pe, packetReceived, &a_, per)
	pe.startThreadedRead();
	piMSleep(500);*/
	
	TC tc;
	piMSleep(500);
	piCout << tc.makeConfig();
	return 0;
	
	/*tm = PISystemTime::current();
	for (int i = 0; i < 10000000; ++i) {
		ql.append(i*10);
		ql.prepend(i*10 + 1);
	}
	piCout << (PISystemTime::current() - tm).toMicroseconds();
	*/
	//tm = PISystemTime::current();
	/*for (int i = 0; i < 100000000; ++i) {
		pl.append(i*10);
		pl.prepend(i*10 + 1);
	}*/
	//PICodeParser cd_;
	//cd.includeDirectory("../qpicalculator");
	//cd_.parseFile("piincludes.h");
	//piCout << (PISystemTime::current() - tm).toMilliseconds();
	/*piCout << NewLine;
	piForeachCA (i, pl)
		piCout << i;
		
	pl.remove(1, 2).prepend(111).prepend(222);
	pl.remove(1, 1);
	piCout << NewLine;
	piForeachCA (i, pl)
		piCout << i;*/
	/*piCout << NewLine;
	for (int i = 0; i < pl.size_s(); ++i)
		piCout << pl[i];
	*/
	
	/*PIEthernet eth(PIEthernet::UDP);
	eth.setReadAddress("192.168.0.30:4001");
	eth.setSendAddress("192.168.0.50:4001");
	eth.startThreadedRead(readed);
	piCout << "Connected";
	//eth.send(PIString("This is test string!\n").toByteArray());
	FOREVER_WAIT*/
	
	if (argc < 2) return 0;
	PICodeParser cd;
	//cd.includeDirectory("../qpicalculator");
	cd.parseFile(argv[1]);
	piForeachC (PICodeParser::Enum & e, cd.enums)
		piCout << e.name << e.members;
	

	//piCout << v.toType<float>();
	//piCout << v.toType<float>().toType<PIString>();
	//PIFile::remove("ki");
	/*PIConfig conf("protocols_commod.conf");
	piCout << conf.allTree();
	conf.setValue("rmd.123", 456);*/
	
	/*PITimer tm;
	piCout << tm.debug() << tm.properties();
	tm.setDebug(false);
	piCout << tm.debug() << tm.properties();
	tm.setDebug(true);
	piCout << tm.debug() << tm.properties();*/

	//PIObject * ser = (PIObject * )PIIODevice::createFromFullPath("file://OM2:38400:7");
	//piCout << ser << NewLine << ser->properties();
}


