#include "pip.h"

#include "ccm_kbd.h"
TileProgress * tp;
void key_event(PIKbdListener::KeyEvent e, void*) {
	PICodeInfo::EnumInfo * ei = PICodeInfo::enumsInfo->value("PIKbdListener::SpecialKey");
	if (!ei) return;
	if (e.key == '-') {tp->value -= 1.; return;}
	if (e.key == '+') {tp->value += 1.; return;}
	piCout << PICoutManipulators::NewLine << "modifiers" << e.modifiers;
	piForeachC (PICodeInfo::EnumeratorInfo & i, ei->members)
		if (i.value == e.key) {
			piCout << "key" << i.name;
			return;
		}
	piCout << "key" << e.key;
}
class Catcher: public PIObject {
	PIOBJECT(Catcher)
public:
	EVENT_HANDLER2(void, event, PIScreenTile *, t, PIScreenTypes::TileEvent, e) {
		piCout << "event from" << t->name() << "type" << e.type << e.data;
		if (e.data == 2)
			delete t->parentTile();
	}
	EVENT_HANDLER1(void, eventKey, PIKbdListener::KeyEvent, e) {
		//piCout << "key" << e.key;
	}
};

using namespace PIScreenTypes;



template <typename T>
class Test {
public:
	Test(int cycles) {
		PITimeMeasurer tm;
		double tr;
		for (int c = 1; c <= cycles; ++c) {
			piCout << "*** CYCLE" << c << "***";
			
			{
				T d0;
				PICout(0) << "push_back ... " << PICoutManipulators::Flush;
				tm.reset();
				for (int i = 0; i < c; i++) {
					d0.push_back(i);
				}
				tr = tm.elapsed_m();
				piCout << tr << "ms, capacity" << d0.capacity();
			}
			
			{
				T d0;
				PICout(0) << "push_front ... " << PICoutManipulators::Flush;
				tm.reset();
				for (int i = 0; i < c; i++) {
					d0.push_front(i);
				}
				tr = tm.elapsed_m();
				piCout << tr << "ms, capacity" << d0.capacity();
			}
			
			{
				T d0;
				PICout(0) << "resize + push_back + pop_front ... " << PICoutManipulators::Flush;
				tm.reset();
				d0.resize(c);
				for (int i = 0; i < c; i++) {
					d0.push_front(i);
					d0.pop_front();
				}
				tr = tm.elapsed_m();
				piCout << tr << "ms, capacity" << d0.capacity();
			}
			
			{
				T d0;
				PICout(0) << "resize + push_front + pop_back ... " << PICoutManipulators::Flush;
				tm.reset();
				d0.resize(c);
				for (int i = 0; i < c; i++) {
					d0.push_front(i);
					d0.pop_back();
				}
				tr = tm.elapsed_m();
				piCout << tr << "ms, capacity" << d0.capacity();
			}
			
			{
				T d0, d1;
				PICout(0) << "resize + push_front + pop_back ... " << PICoutManipulators::Flush;
				tm.reset();
				for (int i = 0; i < c; i++) {
					d0 << i;
				}
				piCout << d0;
				for (int i = 0; i < c; i++) {
					piCout << i;
					d1.insert(0, d0);
					piCout << d1.size() << d1.capacity() << i;
					piCout << d1;
					d1.remove(0, i);
					piCout << d1.size() << d1.capacity() << i;
					piCout << d1;
				}
				tr = tm.elapsed_m();
				piCout << tr << "ms, capacity" << d0.capacity();
			}
			
		}
	}
};


class Parent {
public:
	virtual void print() {piCout << "Parent";}
};

class Child: public Parent {
public:
	void print() {piCout << "Child"; Parent::print();}
};
	

//#include <netdb.h>

//#include "mpint.h"
//#include "unicode/utypes.h"
//#include "unicode/stringpiece.h"
//#include "unicode/utf8.h"
//#include "unicode/ucnv.h"
//#include "unicode/uchar.h"

#include "picrypt.h"
#include "pifixedpoint.h"

int main (int argc, char * argv[]) {
	PICrypt cr;
	PIByteArray k = cr.setKey("pass");
	PIString s("1234567890");
	PIByteArray ba(s.data(),s.size());
	PIByteArray sba = cr.crypt(ba);
	piCout << ba.size() << ba;
	piCout << k.size() << k;
	piCout << cr.key().size() << cr.key();
	piCout << sba.size() << sba;
	piCout << cr.decrypt(sba).size() << cr.decrypt(sba);
	sba[random()%sba.size()]++;
	piCout << cr.decrypt(sba).size() << cr.decrypt(sba);

	piCout << PICrypt::sizeKey() << PICrypt::sizeCrypt();
	piCout << ba.size() << ba;
	PIByteArray ke = PICrypt::hash("pass");
	piCout << ke.size() << ke;
	sba = PICrypt::crypt(ba, ke);
	piCout << sba.size() << sba;
	piCout << ba.size() << PICrypt::decrypt(sba, ke);
	return 0;
//	//char uc[] = "←↑→↓АБВ";
//	char uc[] = "│─┌┐└┘├┤┬┴┼";
//	─────────────┴─┴┴──┴┴┴─┘
//	PIString us = PIString::fromUTF8(uc);
	
//	//piForeachC (PIChar & c, us)
//	//	piCout << PICoutManipulators::Hex << PIByteArray(&c, 4);
//	piCout << us << us.toByteArray() << us.size_s();
//	return 0;
	
	/*FixedPoint<16, long long> a, b;
	a = 10;
	b = 3;
	piCout << a << b << a/b;
	FixedPoint<7,ushort> c = 507.03;
	piCout << c;*/
//	gmp::mpint m1("1003456789098765432334567890743278908743789087345678909876543213456789098765422"),
//			m2("523456789085039345678909856787656787654383071478723617832987864856248765784547826784659267894659782645824317172186776677");
//	FixedPoint<1, gmp::mpint> mf1(m1);
//	PITimeMeasurer tm3;
//	for (int i=0; i< 1000; i++)
//		m1 = m1*m2;//gmp::mpint(1);
////	m1 = m1 >> 64;
//	piCout << tm3.elapsed_m();
//	tm3.reset();
//	piCout << m1;
//	piCout << tm3.elapsed_m();
	//m1++;
	/*complex<FixedPoint<8,int> > ccc;
	piCout << ccc;
	return 0;

	FixedPoint<4> x,y,z;
	y = 20;
	z = 0.01;
	x = z*y;
	piCout << x;
	piCout << y;
	piCout << z;
	return 0;*/
	/*
	hostent * he = 0;
	he = gethostbyname(argv[1]);
	piCout << he->h_name;
	piCout << he->h_aliases[0];
	return 0;*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*int cc = PIString(argv[1]).toInt();
	
	piCout << "Deque";
	Test<PIDeque<int> > testd(cc);
	
	return 0;*/
	
	/*if (!(argc == 3 || argc == 4)) {
		piCout << "UDPFileTransfer";
		piCout << "USE: piptest [src_ip_port] [dst_ip_port] {filename}";
		return 0;
	}
	PIKbdListener kbd;
	kbd.enableExitCapture();
	PIString src = argv[1];
	PIString dst = argv[2];
	UDPFileTransfer f(src, dst);
	piCout << "work directory" << f.ft.directory().absolutePath() << ", listen on" << src << ",send to" << dst;
	if (argc == 4) {
		PIString file = argv[3];
		piCout << "send file" << file;
		f.startSend(file);
		return 0;
	} else {
		piCout << "wait for receiving";
	}*/
	
	Catcher catcher;
	PIScreen screen(false, key_event);
	CONNECTU(&screen, tileEvent, &catcher, event)
	CONNECTU(&screen, keyPressed, &catcher, eventKey)
	screen.enableExitCapture(PIKbdListener::F10);
	screen.start();
	float cx = 0, cy = 0, vx = 1., vy = 0.3, t = 0.;
	PITimeMeasurer tm;
	Color col = Red;
	screen.rootTile()->back_symbol = '0';
	
	PIScreenTile * tile = new TileSimple();
	screen.rootTile()->addTile(tile);
	((TileSimple*)tile)->content << TileSimple::Row("SADHFJKL", CellFormat(Red, Default));
	tile->back_symbol = '1';
	tile->size_policy = Fixed;
	tile->minimumHeight = 3;
	
	tile = new PIScreenTile("center");
	screen.rootTile()->addTile(tile);
	tile->direction = Horizontal;
	tile->back_symbol = '*';
	tile->marginLeft = 1;
	tile->marginTop = 2;
	tile->marginRight = 3;
	tile->marginBottom = 4;
	tile->spacing = 2;
	
	PIScreenTile * tile2 = new PIScreenTile();
	tile->addTile(tile2);
	tile2->back_symbol = '4';
	tile2->back_format.flags = Bold;
	
	tile2 = new TileSimple();
	tile->addTile(tile2);
	((TileSimple*)tile2)->alignment = Right;
	((TileSimple*)tile2)->content << TileSimple::Row("red", CellFormat(Red, Default));
	((TileSimple*)tile2)->content << TileSimple::Row("┏━━┯━━┓", CellFormat(Green, Red));
	((TileSimple*)tile2)->content << TileSimple::Row("┃  │  ┃", CellFormat(Green, Red));
	((TileSimple*)tile2)->content << TileSimple::Row("┠──┴──┨", CellFormat(Green, Red));
	((TileSimple*)tile2)->content << TileSimple::Row("┃╱╲ ╱╲┃", CellFormat(Green, Red));
	((TileSimple*)tile2)->content << TileSimple::Row("┃╲╱ ╲╱┃", CellFormat(Green, Red));
	((TileSimple*)tile2)->content << TileSimple::Row("┗━━━━━┛", CellFormat(Green, Red));
	
	tile2 = new TileList("list0");
	tile->addTile(tile2);
	((TileList*)tile2)->alignment = Right;
	for (int i = 0; i < 30; ++i)
		((TileList*)tile2)->content << TileList::Row("item " + PIString(i), CellFormat(Red, Magenta));
	((TileList*)tile2)->selection_mode = TileList::SingleSelection;
	
	tile2 = new TileList("list1");
	tile->addTile(tile2);
	((TileList*)tile2)->alignment = Center;
	for (int i = 0; i < 50; ++i)
		((TileList*)tile2)->content << TileList::Row("item " + PIString(i), CellFormat(Magenta, Magenta, (i % 3 ? Bold : 0) | (i % 2 ? Underline : 0)));
	((TileList*)tile2)->selection_mode = TileList::MultiSelection;
	
	tile = new PIScreenTile();
	screen.rootTile()->addTile(tile);
	tile->back_symbol = '3';
	tile->maximumHeight = 4;
	//tile->size_policy = Expanding;
	
	tile = new PIScreenTile();
	screen.rootTile()->addTile(tile);
	tile->back_symbol = '8';
	tile->back_format.color_back = Yellow;
	tile->maximumHeight = 5;
	
	
	tile = new PIScreenTile();
	
	tile2 = new TileSimple();
	tile2->back_format.color_back = Transparent;
	((TileSimple*)tile2)->content << TileSimple::Row("label", CellFormat(Magenta, Magenta, Bold));
	((TileSimple*)tile2)->alignment = PIScreenTypes::Center;
	tile2->size_policy = PIScreenTypes::Preferred;
	tile->addTile(tile2);
	
	/*tile2 = new TileButtons("butt0");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButtons*)tile2)->content << TileButtons::Button("first", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->content << TileButtons::Button("sec", CellFormat(Green, Red));
	((TileButtons*)tile2)->content << TileButtons::Button("3", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->direction = PIScreenTypes::Horizontal;
	
	tile2 = new TileButtons("butt1");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButtons*)tile2)->content << TileButtons::Button("fF", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->content << TileButtons::Button("sec2", CellFormat(Green, Red));
	((TileButtons*)tile2)->content << TileButtons::Button("333", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->direction = PIScreenTypes::Horizontal;
	
	tile2 = new TileButtons("butt2");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButtons*)tile2)->content << TileButtons::Button("fF", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->content << TileButtons::Button("sec2", CellFormat(Green, Red));
	((TileButtons*)tile2)->content << TileButtons::Button("333", CellFormat(Green, Transparent));
	((TileButtons*)tile2)->direction = PIScreenTypes::Vertical;*/

	tile2 = new TileButton("butt0");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButton*)tile2)->text = "first";
	
	tile2 = new TileButton("butt1");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButton*)tile2)->text = "sec2";
	
	tile2 = new TileCheck("check0");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileCheck*)tile2)->text = "check";
	
	tile2 = new TileCheck("check1");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileCheck*)tile2)->text = "ch";
	((TileCheck*)tile2)->toggled = true;
	
	tile2 = new TileButton("butt2");
	tile2->back_format.color_back = Transparent;
	tile->addTile(tile2);
	((TileButton*)tile2)->text = "F";
	
	tp = new TileProgress("butt2");
	//tile2->back_format.color_back = Transparent;
	tp->maximum = 200;
	tp->prefix = "prog: ";
	tp->suffix = " bytes";
	tile->addTile(tp);
	
	((TileButton*)tile)->back_format.color_back = Yellow;
	tile->setMargins(2, 2, 1, 1);
	tile->spacing = 1;
	tile->direction = PIScreenTypes::Horizontal;
	screen.setDialogTile(tile);
	
	//screen.rootTile()->hide();
	while (!PIKbdListener::exiting) {
		cx += vx;
		cy += vy;
		t += 0.05;
		if (cx < 0) {cx = 0.; vx *= -1;}
		if (cx >= screen.windowWidth()) {cx = screen.windowWidth() - 1; vx *= -1;}
		if (cy < 0) {cy = 0.; vy *= -1;}
		if (cy >= screen.windowHeight()) {cy = screen.windowHeight() - 1; vy *= -1;}
		if (tm.elapsed_m() > 500) {
			tm.reset();
			if (col == Red) col = Green;
			else col = Red;
			//screen.tileByName("list0")->visible = !screen.tileByName("list0")->visible;
		}
		/*screen.lock();
		screen.clear();
		screen.drawer()->drawRect(0, 0, 20, 10, ' ', Default, col);
		screen.drawer()->drawLine(21, 0, 21, 10, '|', Default, col);
		screen.drawer()->drawPixel(21, 5, '#', Magenta, col);
		screen.drawer()->drawPixel(21, 4, '#', Magenta, col, Bold);
		screen.drawer()->drawText(50 + cos(t)*50, 20 + sin(t)*20, "Hello PIScreen!", Magenta);
		//screen.drawer()->drawPixel(cx, cy, 'W', Default, Blue);
		screen.unlock();*/
		piMSleep(25);
	}
	return 0;
}

