#include "mzChart.h"
#include "mzstdguiGlobals.h"
#include "mzApplication.h"
#include "mzCanvas.h"


CChartSerie::CChartSerie(CString name) {
    this->name = name;
    this->values = new CArrayList(30);
}

CChartSerie::~CChartSerie() {
    if (this->values) {
        this->clear();
        delete this->values;
        this->values = NULL;
    }
}

VOID CChartSerie::clear() {
    if (this->values) {
        CChartValue* v;
        ULONG size = this->values->size();
        for (ULONG i = 0; i < size; i++) {
            v = (CChartValue*)this->values->getAt(i);
            delete v;
        }
        this->values->clear();
    }
}

VOID CChartSerie::addXYValue(double x, double y) {
    CChartXYValue* v = new CChartXYValue(x, y);
    this->values->append((LONG)v);
}

VOID CChartSerie::addBarValue(CString x, double y) {
    CChartBarValue* v = new CChartBarValue(x, y);
    this->values->append((LONG)v);
}






VOID CChart::_commonInit(LONG width, LONG height) {
    this->setRealClassName(L"CChart");
    this->setSize(width, height);
    
    this->series = new CArrayList();
    this->addSerie(L"default");
    
	this->_backgroundColor = RGB(0xf0, 0xf0, 0xf0);
	this->_borderColor = RGB(0, 0, 0);
    
    this->_headerFontName = L"Arial";
    this->_headerFontSize = 12;
    
    this->_labelFontName = L"Arial";
    this->_labelFontSize = 8;
    
    this->_marginLeft = 0;
	this->_marginTop = 0;
	this->_marginRight = 0;
	this->_marginBottom = 0;
    
	this->_wLeft = 0;
	this->_wTop = 0;
	this->_wRight = 0;
	this->_wBottom = 0;
	this->_wWidth = 0;
	this->_wHeight = 0;
    
    this->_h1 = 1;
    this->_h2 = 2;
    this->_h3 = 3;
    this->_h4 = 4;
    this->_h5 = 5;
    this->_doAnimation = TRUE;
}

CChart::CChart(CSprite* parent, CString name, LONG x, LONG y, LONG width, LONG height)
    :CGuiControl(parent, name, NULL, x, y, FALSE)
{
    this->_commonInit(width, height);
}

CChart::CChart(CSprite* parent, CStyledWindow* parentWnd, CString name, LONG x, LONG y, LONG width, LONG height)
    :CGuiControl(parent, parentWnd, name, NULL, x, y, FALSE)
{
    this->_commonInit(width, height);
}

CChart::~CChart() {
    if (this->series) {
        this->clearSeries();
        delete this->series;
        this->series = NULL;
    }
}

VOID CChart::clearSeries() {
    if (this->series) {
        CChartSerie* s;
        ULONG size = this->series->size();
        for (ULONG i = 0; i < size; i++) {
            s = (CChartSerie*)this->series->getAt(i);
            delete s;
        }
        this->series->clear();
    }
}

VOID CChart::removeSerie(WCHAR* name) {
    if (this->series) {
        CChartSerie* s;
        ULONG size = this->series->size();
        for (ULONG i = 0; i < size; i++) {
            s = (CChartSerie*)this->series->getAt(i);
            if (s->name == name) {
                delete s;
                this->series->delAt(i);
            }
        }
    }
}

VOID CChart::addSerie(WCHAR* name) {
    if (this->series) {
        CChartSerie* s = new CChartSerie(name);
        this->series->append((LONG)s);
    }
}

VOID CChart::onDraw(CBitmap *dest) {
	CSprite::onDraw(dest);
}

VOID CChart::_updateCanvas() {
    this->_needUpdate = FALSE;
    if (!this->canvas) {
		return;
    }

	LONG x = 0;
    LONG y = 0;
    LONG width = this->width;
    LONG height = this->height;
    HDC hdc = GetDC(0);
	CTheme *t = this->_theme;
    
	this->canvas->beginPaint(hdc);
    CCanvas* cnv = new CCanvas(this->canvas->getDC());
    if (cnv) {
        // ================================================================
        // Rysujemy tło
        // ================================================================
        this->canvas->fillRectangle(this->_borderColor, this->_backgroundColor, 0, 0, width, height, 1, 0);
        cnv->drawGradientedRectangle(10, this->height - this->_h1 - 10, 30, this->_h1, 1, RGB(0,0,0), RGB(0x99,0xcc,0xff), RGB(0x00,0x00,0x66), 1);
        cnv->drawGradientedRectangle(50, this->height - this->_h2 - 10, 30, this->_h2, 1, RGB(0,0,0), RGB(0x99,0xff,0x33), RGB(0xff,0x33,0x00), 1);
        cnv->drawGradientedRectangle(90, this->height - this->_h3 - 10, 30, this->_h3, 1, RGB(0,0,0), RGB(0x00,0x00,0x00), RGB(0x99,0xcc,0xff), 1);
        cnv->drawGradientedRectangle(130, this->height - this->_h4 - 10, 30, this->_h4, 1, RGB(0,0,0), RGB(0xcc,0x33,0x00), RGB(0x99,0xcc,0xff), 1);
        cnv->drawGradientedRectangle(170, this->height - this->_h5 - 10, 30, this->_h5, 1, RGB(0,0,0), RGB(0xff,0xcc,0x00), RGB(0xff,0x33,0x00), 1);
        // ================================================================
        // Rysujemy tło
        // ================================================================
        delete cnv;
    }
    
    this->canvas->endPaint();
	ReleaseDC(0, hdc);
}


BOOL WINAPI CChart::update() {
    BOOL retval = TRUE;
    CApplication* application = getApplication();

    if (!application) {
        return FALSE;
    }
    
    static LONG lastTick = application->getCurTickCount();
    if (this->_doAnimation) {        
        LONG curTick = application->getCurTickCount();
        LONG delta = curTick - lastTick;
        
        if (delta < 20) {
            return FALSE;
        }

        BOOL animate = FALSE;
        
        if (this->_h1 < this->height - 50) {
            this->_h1 += 12;
            animate = TRUE;
        }
        if (this->_h2 < this->height - 50) {
            this->_h2 += 13;
            animate = TRUE;
        }
        if (this->_h3 < this->height - 50) {
            this->_h3 += 14;
            animate = TRUE;
        }
        if (this->_h4 < this->height - 50) {
            this->_h4 += 17;
            animate = TRUE;
        }
        if (this->_h5 < this->height - 50) {
            this->_h5 += 20;
            animate = TRUE;
        }
        
        if (!animate) {
            this->_doAnimation = FALSE;
        }
        
        lastTick = curTick;
    }
    
    return TRUE;
}
