
#include "cal.h"
// #include "vlabel.h"

#include <qlayout.h>
#include <qlabel.h>
#include <qdatetime.h>

#include <qpainter.h>
#include <qsize.h>


#include <set>

Cal::~Cal()
{

}
void Cal::initModel()
{
	QDate today = QDate::currentDate();
	year = today.year();
//	today = today.addDays(-14); 
	QDate this_sunday;

	int day = today.dayOfWeek();
	if ( day == 7 ){
		this_sunday = today;
	}else {
		this_sunday = today.addDays(-day);
	}

	weeks[0] = today;
	for(int i=1;i<=5;i++)
	{
		weeks[i] = this_sunday.addDays( 7 * (i-1));
	}
}
Cal::Cal(QWidget * parent ): QWidget(parent)
{
//		2003 M T W R F S S
//		  |  1 2 3 4 5 6 7
//		T |  A B C D E F G
//		E
//		X
//		T
//		-------------------
//
	setPaletteForegroundColor ( Qt::black ); 
	setPaletteBackgroundColor ( Qt::white ); 
	initModel();
	grid = new QGridLayout(this,6,8);

	year_label = newLabel(year); 
	grid->addWidget( year_label,0,0);
	grid->addWidget( newLabel("Sunday") ,0,1 );
	grid->addWidget( newLabel("Monday"),0,2 );
	grid->addWidget( newLabel("Tuesday"),0,3 );
	grid->addWidget( newLabel("Wednesday"),0,4 );
	grid->addWidget( newLabel("Thursday"),0,5 );
	grid->addWidget( newLabel("Friday"),0, 6 );
	grid->addWidget( newLabel("Saturday"),0,7 );
	
	buildMonthLabel(grid);
	createWeek(grid,1);
	createWeek(grid,2);
	createWeek(grid,3);
	createWeek(grid,4);
	createWeek(grid,5);
}
void Cal::paintEvent(QPaintEvent *e)
{
	if (!e) return;
	updateMonthLabel();
	updateWeek(1);
	updateWeek(2);
	updateWeek(3);
	updateWeek(4);
	updateWeek(5);
}
void Cal::updateMonthLabel()
{
	for(int i=1;i<=5;i++)
	{
		int mon = weeks[i].month();
		labels[(i-1)*8]->setText( QDate::shortMonthName(mon)); 
	}
	for(int i=2;i<=5;i++)
	{
		if ( weeks[i-1].month() == weeks[i].month())
			labels[(i-1)*8]->setText(""); 
	}


}
void Cal::buildMonthLabel(QGridLayout *grid)
{
	for(int i=1;i<=5;i++)
	{
		int mon = weeks[i].month();
		QLabel *l = newLabel( QDate::shortMonthName(mon)); 
		labels[(i-1)*8] = l;
		grid->addWidget(l,i,0);
	}
	for(int i=2;i<=5;i++)
	{
		if ( weeks[i-1].month() == weeks[i].month())
			labels[(i-1)*8]->setText(""); 
	}
}
QDate Cal::getWeek(int n)
{
	return weeks[n];
}
QLabel * Cal::createDay(int num)
{
	QLabel *day = new QLabel(this);
	day->setMargin(20);
	day->setAlignment(Qt::AlignCenter);
	day->setFrameStyle(QFrame::Panel | QFrame::Raised);
	day->setLineWidth(1);
	day->setNum(num);
	return day;
}
void Cal::updateWeek(int row)
{
	QDate sunday = getWeek(row);
	QDate day; 
	QLabel *l;
	for(int i=1;i<8;i++)
	{	
		day = sunday.addDays(i);
		l=labels[(row-1)*8+i];
		l->setPaletteForegroundColor ( Qt::black ); 
		if ( day == QDate::currentDate())
		{
			l->setPaletteBackgroundColor ( Qt::red.light(175) ); 
		} else {
			l->setPaletteBackgroundColor ( Qt::white ); 
		}
		l->setNum(day.day()); 
	}
	l->update();
}
void Cal::createWeek(QGridLayout *grid, int row)
{
	QDate sunday = getWeek(row);

	QDate today = QDate::currentDate();
	QDate day;
	for(int i=1;i<8;i++)
	{
		day = sunday.addDays(i);
		QLabel *l =createDay(day.day());
		if ( day == today )
		{
			l->setPaletteBackgroundColor ( Qt::red.light(175) ); 
		}
		labels[(row-1)*8+i] = l;
		grid->addWidget(l,row,i);
	}

}
QLabel * Cal::newLabel(int num)
{
	QLabel *yr = newLabel("year");
	yr->setNum(num);
	return yr;
}
QLabel * Cal::newLabel(char * str)
{
	return newLabel(QString(str));
}
QLabel * Cal::newLabel(QString str)
{
	QLabel *sep = new QLabel(str,this);
	sep->setFrameStyle(QFrame::Panel | QFrame::Raised);
	sep->setLineWidth(2);
	return sep;
}
/*
QLabel * Cal::newVsep()
{
	QLabel *sep = new QLabel(this);
	sep->setFrameStyle(QFrame::VLine | QFrame::Plain);
	sep->setLineWidth(1);
	return sep;
}
QLabel * Cal::newHsep()
{
	QLabel *sep = new QLabel(this);
	sep->setFrameStyle(QFrame::HLine | QFrame::Plain);
	sep->setLineWidth(1);
	return sep;
}
*/
void Cal::updateYear( set<int> years)
{
	typedef set<int>::iterator SETI;
	SETI p=years.begin(); 

	QString complete_text; 
	QString cur_text;

	//error;
	if ( years.size() > 5 || years.size() == 0)
		printf("Error: with year count. skipping\n");
	
	cur_text.setNum(*p);
	p++;
	complete_text.append(cur_text);

	while(p!=years.end())
	{
		complete_text.append("/");
		cur_text.setNum(*p);
		complete_text.append(cur_text);
		p++;
	}
	year_label->setText(complete_text);

}
void Cal::scrollForward()
{
	set<int> years;

	for(int i=1;i<=5;i++)
	{
		weeks[i] = weeks[i].addDays( 7 );
		years.insert(weeks[i].year());
	}
	updateYear(years);
	update();
}
void Cal::scrollBack()
{
	set<int> years;
	for(int i=1;i<=5;i++)
	{
		weeks[i] = weeks[i].addDays( -7 );
		years.insert(weeks[i].year());
	}
	updateYear(years);
	update();
}
