/*
    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"
//#include "pivariant.h"
//#include "picodeparser.h"
//#include "pidir.h"
#include "piconnection.h"
//#include <QList>

PIMutex mutex;
PIDeque<int> deq;

void timerE(void * t, int d) {
	//PIMutexLocker l(mutex);
	/*if (d == 1)
		deq.push_back(int(t));
	else
		deq.push_front(int(t));*/
	//printf("%d\n", deq.size());
	piCout << "tick" << (int)t << d;
}



class A: public PIObject {
	PIOBJECT(A)
public:
	A() {}
	EVENT_HANDLER1(void, handlerA1, PIString, s) {piCout << "handlerA1 s" << s;}
	EVENT_HANDLER1(void, handlerA1, float, f) {piCout << "handlerA1 f" << f;}
	//uchar _[0x10];
};


class B: public PIObject {
	PIOBJECT(B)
public:
	B() {}
	EVENT1(eventB1, float, f);
	EVENT1(eventB2, PIString, s);
	//uchar _[0x20];
};


class C: public A, public B {
public:
	C() {}
};

int main (int argc, char * argv[]) {
	PIFile f("test.txt");
	f.clear();
	f << "1234566789\n";
	piCout << f.size();
	f.close();
	piCout << f.size();
	
	/*CONNECTU(&b, eventB1, &a, handlerA1)
	CONNECTU(&b, eventB2, &a, handlerA1)
	a.dump();
	b.dump();
	b.eventB1(0.33);
	b.eventB2("str");*/
	
	
	
	//conf.dump();
	/*PIObject * t_o = &timer, * a_o = &a, * b_o = &b;
	CONNECTU(t_o, tickEvent, a_o, handlerT)
	CONNECTU(b_o, eventB1, &a, handlerA1)
	CONNECTU(&b, eventB2, a_o, hAAA)
	a_o->dump("* ");
	b_o->dump("# ");
	//dumpApplication();
	b.eventB(10, "str");
	b.eventB1(10.5);
	b.eventB2("test");*/
	
	/*PIKbdListener kbd;
	PITimer timer(timerE);
	timer.start(100);
	kbd.enableExitCapture();
	WAIT_FOR_EXIT*/
	
	/*
	PIMathVectorT3d v(1., -2., 3.);
	PIMathMatrixT33d m0;
	m0.setCol(0, v.normalized());
	m0.setCol(1, PIMathVectorT3d(v[1], -v[0], 0.).normalized());
	m0.setCol(2, m0.col(1) * m0.col(0));
	
	piCout << m0.determinant();
	//m0.setRow(1, PIMathVectorT3d(1., 2., 3.));
	//piCout << m0 << NewLine << NewLine << m0.;
	//PIMathMatrixT33d m0_(-1., 2., 3., 4., -5., 6., 7., 8., -9.);
	//PIMathMatrixT33d m1_(10., 2., 30., 4., 50., 6., 70., 8., 90.);
	//piCout << m0_.determinant() << m1_.determinant();
	*/
	
	/*
	PITimer::TimerImplementation ti_ = PITimer::ThreadRT;
	PITimer t_(timerE, 0, ti_);
	t_.setData((void*)0x10);
	t_.start(100);
	piSleep(1);
	piCout << "end";
	t_.stop();
	piCout << "end 1";
	t_.waitForFinish();
	piCout << "end 2";
	*/
	
	/*
	PIVector<PITimer*> timers;
	int tc = PIString(argv[1]).toInt();
	piCout << "create ...";
	for (int i = 0; i < tc; ++i) {
		PITimer * t = new PITimer(timerE, 0, PITimer::Thread);
		t->setData((void*)i);
		//t->dump();
		timers << t;
		if (i > 10) t->addDelimiter(i / 10);
		t->start(100);
	}
	piCout << "create ok";
	piSleep(1);
	dumpApplication();
	piCout << "delete ...";
	piForeach (PITimer * t, timers)
		t->stop();
	piForeach (PITimer * t, timers)
		delete t;
	piCout << "delete ok";
	*/
	
	/*
	PIConsole console(false);
	PISystemMonitor mon;
	console.addVariable("", &mon);
	mon.startOnSelf();
	console.enableExitCapture();
	console.start();
	console.waitForFinish();
	return 0;
	*/
	
	/*PITimeMeasurer tm_;
	piCout << PIIODevice::normalizeFullPath("ser:///dev/ttyS1");
	piCout << tm_.elapsed_u();
	tm_.reset();
	piCout << PIIODevice::normalizeFullPath("ser:///dev/ttyS2");
	piCout << tm_.elapsed_u();
	tm_.reset();
	piCout << PIIODevice::normalizeFullPath("ser:///dev/ttyS3");
	piCout << tm_.elapsed_u();
	tm_.reset();
	piCout << PIIODevice::normalizeFullPath("ser:///dev/ttyS1");
	piCout << tm_.elapsed_u();
	tm_.reset();*/
	//msleep(6000000);
	
	/*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(2000);
	//piCout << tc.diagnostic("file://piiodevice.h")->receiveBytesPerSec();
	piCout << tc.makeConfig();*/
	
	/*PITimer timer;
	piForTimes (50) {
		piUSleep(10);
		piCout << PISystemTime::current() << timer.elapsed_u();
	}
	piCout << NewLine;
	piForTimes (50) {
		piUSleep(100);
		piCout << PISystemTime::current(true) << timer.elapsed_u();
	}*/
	
	/*PIConsole console;
	PIEthernet eth(PIEthernet::TCP_Client);
	eth.connect("192.168.20:5006");
	eth.startThreadedRead(readed);
	console.enableExitCapture();
	console.start();
	console.waitForFinish();*/
	
	/*PIBinaryLog log_;
	GPS_Data gps;
	log_.open("log_gps__2014_07_03__11_22_18", PIIODevice::ReadOnly);
	while (!log_.isEnd()) {
		log_.read(&gps, sizeof(gps));
		printf("%f %f\n", gps.lat, gps.lng);
	}*/
	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();
}


