// ************************************************************************************************************
// Author: Marcin Załęczny
// Release Date: 2010-11-10
// Licence: The GNU Lesser General Public License, version 3.0 (LGPLv3)
// ************************************************************************************************************
#include "mzKPRedForm.h"
#include "mzStringList.h"

VOID CKPRedForm::_commonInit(CString day, CString month, CString year, CString nr) {
    this->setRealClassName(L"CKPRedForm");
    this->day = day;
    this->month = month;
    this->year = year;
    this->nr = nr;
}

CKPRedForm::CKPRedForm(CString day, CString month, CString year, CString nr,
                     CString receiver, CString receiver2, CString receiverAccount, CString amount,
                     CString senderAccount, CString sender, CString sender2,
                     CString title, CString title2)
    : CTransferForm(TRANSFER_TYPE_KPRED, receiver, receiver2, receiverAccount, amount, senderAccount, sender, sender2, title, title2)
{
    this->_commonInit(day, month, year, nr);
}


CKPRedForm::CKPRedForm(CString day, CString month, CString year, CString nr,
                     CString receiver, CString receiver2, CString receiverAccount,
                     CString receiverBank, CString amount,
                     CString senderAccount, CString sender, CString sender2,
                     CString title, CString title2, CString kasPlanSymbol, CString amountByWords)
    : CTransferForm(TRANSFER_TYPE_KPRED, receiver, receiver2, receiverAccount, receiverBank, amount, senderAccount, sender, sender2,
                    title, title2, kasPlanSymbol, amountByWords)
{
    this->_commonInit(day, month, year, nr);
}

CKPRedForm::~CKPRedForm() {
}

CXmlNode* CKPRedForm::generateBlanket(CXpsPackage* xps, CXpsDocument* xpsDoc, CXpsPage* page, CString fontName, float pageWidth, float pageHeight, float paddingTop) {
    HANDLE heap = GetProcessHeap();
    DWORD size;
    BYTE* dataBuffer;
    CXmlNode* result;
    
    if (xps) {
        CXmlNode* mainCanvas;
        CXmlNode* canvas;
        
        if (xpsDoc) {
            this->_blanketWidth = 2*pageWidth / 3;
            this->_blanketHeight = 310;

            CString title, title1, title2, title3;
            CStringList* slist;
            
            title = this->title;
            if (this->title2.length() > 0) {
                title += CString(L'\n') + this->title2;
            }
            slist = title.explode(L'\n');
            
            title1 = title;
            if (slist) {
                if (slist->size() > 0) {
                    title1 = slist->getAt(0);
                    if (slist->size() > 1) {
                        title2 = slist->getAt(1);
                        if (slist->size() > 2) {
                            title3 = slist->getAt(2);
                        }
                    }
                }
                
                delete slist;
            }
            
            if (page) {
                float x = (pageWidth - this->_blanketWidth)/2;
                float y = 57;
                float space = 30;
                float pageMargin = 20;
                CString lightPinkColor = L"#A0A0A0";//L"#FFBEBE";
                
                // CANVAS for form data (empty, without data)
                mainCanvas = page->addCanvas();
                result = mainCanvas;
                page->addItem(mainCanvas);

                this->_drawBlanket(page, mainCanvas, fontName, x, y);
                this->_drawTextContent(page, mainCanvas, fontName, x, y,
                        this->day, this->month, this->year, this->nr,
                        this->sender, this->sender2, title1, title2, title3,
                        amount, amountByWords);
                y += this->_blanketHeight + space;
                
                page->addDashedHLine(mainCanvas, pageMargin, y - space/2, pageWidth - 2*pageMargin, lightPinkColor, 1, 1, 2, 2);
                
                this->_drawBlanket(page, mainCanvas, fontName, x, y);
                this->_drawTextContent(page, mainCanvas, fontName, x, y,
                        this->day, this->month, this->year, this->nr,
                        this->sender, this->sender2, title1, title2, title3,
                        amount, amountByWords);
                y += this->_blanketHeight + space;
                
                page->addDashedHLine(mainCanvas, pageMargin, y - space/2, pageWidth - 2*pageMargin, lightPinkColor, 1, 1, 2, 2);
                
                this->_drawBlanket(page, mainCanvas, fontName, x, y);
                this->_drawTextContent(page, mainCanvas, fontName, x, y,
                        this->day, this->month, this->year, this->nr,
                        this->sender, this->sender2, title1, title2, title3,
                        amount, amountByWords);
            }
        }
    }
    
    return result;
}

VOID CKPRedForm::_drawBlanket(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, float topX, float topY) {
    float x, y, height, width3, height3, height6, height6_1;
    float tmp_x, tmp_w, tmp_y;
    float textFieldHeight;
    float factor;
    INT i;
    
    CString redColor = L"#A0A0A0";//L"#FF0000";
    CString lightPinkColor = L"#F0F0F0";//L"#FFBEBE";
    CString whiteColor = L"#FFFFFF";
    CString fontColor = L"#000000";
    
    float labelFontSize = 7;
    float mediumFontSize = 10;
    float largeFontSize = 14;
    float largeFontLineheight = 1.2 * largeFontSize;
    float labelFontLineheight = 1.2 * labelFontSize;
    float mediumFontLineheight = 1.2 * mediumFontSize;
    float thinLine = 0.8;
    float strokeMiterLimit = 1.0;
    float dateFieldHeight;
    
    width3 = this->_blanketWidth/3;
    height3 = this->_blanketHeight/3;
    height6 = this->_blanketHeight/6;
    height6_1 = height6 + 6;
    
    x = topX;
    y = topY;
    
    // First line-block
    // left column
    page->addRectangleBR(mainCanvas, x, y + height6_1 - 5, width3, height6_1 - 4, redColor, thinLine, strokeMiterLimit);
    page->addVLine(mainCanvas, x, y + height6_1 - 3 - 6.7, 5, redColor, thinLine, strokeMiterLimit);
    page->addLabel(mainCanvas, x + width3 - 26, y + height6 - labelFontLineheight/2, L"m.p.", 23, labelFontLineheight + 2, fontName, labelFontSize, fontColor, L"None", COMMON_EMPTY_STRING, whiteColor, thinLine, strokeMiterLimit, 3.0, 0.0, 0.0);
    
    // middle column
    dateFieldHeight = labelFontSize + 4;
    page->addDateFieldDDMMYYYY(mainCanvas, x + width3 + 4, y + height6 - 8, width3 - 8, dateFieldHeight, 4, labelFontSize, fontName, redColor, thinLine, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + width3 + 4, y + largeFontSize - 4, L"Dowód wpłaty", redColor, fontName, largeFontSize);
    page->addGlyph(mainCanvas, x + width3 + 4, y + largeFontSize + largeFontLineheight - 4, L"Pokwitowanie", redColor, fontName, largeFontSize);
    // third column
    page->addRectangle(mainCanvas, x + 2*width3, y, width3/2, height6/2 - 4, redColor, thinLine, strokeMiterLimit, lightPinkColor);
    page->addLabel(mainCanvas, x + 2*width3, y + height6/2, L"KP", height6_1 - height6/2, height6_1 - height6/2, fontName, largeFontSize, whiteColor, L"BoldSimulation", COMMON_EMPTY_STRING, redColor, thinLine, strokeMiterLimit, 7.0, 7.0, 0.0);
    page->addGlyph(mainCanvas, x + 2*width3 + (height6_1 - height6/2) + 4, y + height6_1 - 3, L"Nr", redColor, fontName, largeFontSize);
    tmp_x = x + this->_blanketWidth - 0.65*width3;
    page->addRectangleLBR(mainCanvas, tmp_x, y + height6_1 - 4 - 3, 0.65*width3, 4, redColor, thinLine, strokeMiterLimit);
    
    
    textFieldHeight = height3/5;
    
    // Second line-block
    y += height6_1;
    height = height3 - height6_1;
    page->addGlyph(mainCanvas, x + 4, y + mediumFontSize, L"Od Kogo", redColor, fontName, mediumFontSize, 0, L"BoldSimulation");
    page->addRectangle(mainCanvas, x, y, this->_blanketWidth, height, redColor, thinLine, strokeMiterLimit, COMMON_EMPTY_STRING);
    page->addRectangleLBR(mainCanvas, x + 4, y + textFieldHeight - 3, 2*width3 - 8, 3, redColor, thinLine, strokeMiterLimit);
    page->addRectangleLBR(mainCanvas, x + 4, y + 2*textFieldHeight - 3, 2*width3 - 8, 3, redColor, thinLine, strokeMiterLimit);
    page->addVLine(mainCanvas, x + 2*width3, y, height, redColor, 1, strokeMiterLimit);
    page->addRectangle(mainCanvas, x + 2*width3 + 1, y + 1, width3-2, 3*height/4 - 4, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
    factor = 0.6;
    tmp_w = 30;
    page->addRectangle(mainCanvas, x + 2*width3 + factor*width3 - tmp_w, y + 3*height/4 - 4,
                        tmp_w, height/4 + 4, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);

    page->addGlyph(mainCanvas, x + 2*width3 + (factor*width3 - 60)/2, y + largeFontSize, L"Winien", redColor, fontName, largeFontSize, 0, L"BoldSimulation");
    page->addGlyph(mainCanvas, x + 2*width3 + (factor*width3 - 40)/2, y + largeFontSize + largeFontLineheight, L"Kasa", redColor, fontName, largeFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 + ((1-factor)*width3-25)/2, y + largeFontSize, L"Ma", redColor, fontName, largeFontSize, 0, L"BoldSimulation");
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 + ((1-factor)*width3-45)/2, y + largeFontSize + largeFontLineheight, L"Konto", redColor, fontName, largeFontSize, 0, L"None");

    page->addGlyph(mainCanvas, x + 2*width3 + (factor*width3 - 32)/2, y + mediumFontSize + 3*height/4 - 1, L"zł", redColor, fontName, mediumFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 - 22, y + mediumFontSize + 3*height/4 - 1, L"gr", redColor, fontName, mediumFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 + ((1-factor)*width3-38)/2, y + mediumFontSize + 3*height/4 - 1, L"Numer", redColor, fontName, mediumFontSize, 0, L"None");

    page->addVLine(mainCanvas, x + 2*width3 + factor*width3, y, height, redColor, thinLine, strokeMiterLimit);
    
    
    // Third line-block
    y += height;
    height = height3;
    page->addRectangle(mainCanvas, x, y, this->_blanketWidth, height, redColor, thinLine, strokeMiterLimit, COMMON_EMPTY_STRING);
    page->addVLine(mainCanvas, x + 2*width3, y, height, redColor, 1, strokeMiterLimit);
    page->addRectangle(mainCanvas, x + 2*width3 + factor*width3 - tmp_w, y + 1,
                       tmp_w, height - 2, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
    page->addVLine(mainCanvas, x + 2*width3 + factor*width3, y, height, redColor, thinLine, strokeMiterLimit);
    for (i = 0; i < 4; i++) {
        page->addHLine(mainCanvas, x, y + (i+1) * textFieldHeight, this->_blanketWidth, redColor, thinLine/2, strokeMiterLimit);
    }
    page->addLabel(mainCanvas, x + 1, y + textFieldHeight/2 - 1, L"za co", 40, mediumFontSize + 6, fontName, mediumFontSize, fontColor, L"BoldSimulation", COMMON_EMPTY_STRING, whiteColor, 0, strokeMiterLimit, 3, 3, 0.0);
    
    
    // Rectangle for last two rows
    y += height;
    page->addRectangle(mainCanvas, x, y, this->_blanketWidth, height3, redColor, thinLine, strokeMiterLimit, COMMON_EMPTY_STRING);

    // Fourth line-block
    height = height6;
    tmp_w *= 2;
    page->addRectangle(mainCanvas, x + 2, y + 4, 2*width3 - tmp_w - 8, height/2 - 5, lightPinkColor, thinLine, strokeMiterLimit, lightPinkColor);
    page->addLabel(mainCanvas, x + 2*width3 - tmp_w - 1, y + 1, L"RAZEM", tmp_w, height/2 - 2, fontName, mediumFontSize, whiteColor, L"BoldSimulation", redColor, redColor, 0, strokeMiterLimit, 12, 8, 0.0);
    page->addRectangle(mainCanvas, x + 3, y + height/2 + 3, 2*width3 - 6, height/2 - 6, lightPinkColor, thinLine, strokeMiterLimit, lightPinkColor);
    page->addVLine(mainCanvas, x + 2*width3, y, height, redColor, 1, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + 4, y + 1.9*labelFontLineheight, L"Słownie", redColor, fontName, labelFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 4, y + 2.9*labelFontLineheight, L"złotych", redColor, fontName, labelFontSize, 0, L"None");
    page->addVLine(mainCanvas, x + 2*width3 + factor*width3, y, height, redColor, thinLine, strokeMiterLimit);
    page->addHLine(mainCanvas, x + 2*width3, y + height/2 - 2, width3, redColor, thinLine, strokeMiterLimit);
    page->addRectangle(mainCanvas, x + 2*width3 + factor*width3 - tmp_w/2, y + 1,
                       tmp_w/2 - 1, height/2 - 2 - 2, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
                       
    page->addRectangle(mainCanvas, x + 2*width3 + factor*width3 + 1, y + 1,
                       (1-factor)*width3 - 2, height/2 - 2 - 2, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 + 10, y + 0.9*mediumFontLineheight, L"SYMBOLE", redColor, fontName, mediumFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 2*width3 + factor*width3 + 6, y + 1.9*mediumFontLineheight, L"PL. KAS. Nr", redColor, fontName, mediumFontSize, 0, L"None");
    
    page->addRectangle(mainCanvas, x + 2*width3 + 1, y + height/2 - 2 + 1,
                       factor*width3 - 2, height - (height/2 - 2) - 1.4, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
    page->addDashedHLine(mainCanvas, x + 2*width3, y + height/2 + 2 + (height/2 - 5)/2, width3, redColor, thinLine, strokeMiterLimit, 1.75, 1.75);
    page->addHLine(mainCanvas, x + 1, y + height, this->_blanketWidth - 1, redColor, thinLine/2, strokeMiterLimit);


    
    // Fifth line-block
    y += height;
    page->addRectangle(mainCanvas, x + 1, y + 0.7,
                       this->_blanketWidth - 2, labelFontLineheight + 2, lightPinkColor, 0, strokeMiterLimit, lightPinkColor);
    tmp_w = 2*width3 / 4;
    for (i = 0; i < 4; i++) {
        page->addVLine(mainCanvas, x + (i+1)*tmp_w, y + 1, height - 2, redColor, thinLine, strokeMiterLimit);
    }
    page->addGlyph(mainCanvas, x + (tmp_w-35)/2, y + labelFontLineheight, L"Wystawił", redColor, fontName, labelFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + tmp_w + (tmp_w-40)/2, y + labelFontLineheight, L"Sprawdził", redColor, fontName, labelFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 2*tmp_w + (tmp_w-45)/2, y + labelFontLineheight, L"Zatwierdził", redColor, fontName, labelFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 3*tmp_w + (tmp_w-50)/2, y + labelFontLineheight, L"Rap. Kasowy", redColor, fontName, labelFontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 4*tmp_w + (2*tmp_w-100)/2, y + labelFontLineheight, L"Kwotę powyższą otrzymałem", redColor, fontName, labelFontSize, 0, L"None");
    page->addHLine(mainCanvas, x + 1, y + 0.7 + labelFontLineheight + 2, this->_blanketWidth - 1, redColor, thinLine/2, strokeMiterLimit);

    tmp_y = y + 3.5*labelFontLineheight;
    page->addGlyph(mainCanvas, x + 3*tmp_w + 4, tmp_y - 3, L"Nr", redColor, fontName, labelFontSize, 0, L"None");
    page->addRectangleLBR(mainCanvas, x + 3*tmp_w + 22, tmp_y - labelFontLineheight - 3, 0.7*tmp_w, 9, redColor, thinLine/2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + 3*tmp_w + 4, tmp_y + 2.5*labelFontLineheight - 3, L"poz.", redColor, fontName, labelFontSize, 0, L"None");
    page->addRectangleLBR(mainCanvas, x + 3*tmp_w + 22, tmp_y + 2.5*labelFontLineheight - labelFontLineheight - 3, 0.7*tmp_w, 9, redColor, thinLine/2, strokeMiterLimit);

    page->addGlyph(mainCanvas, x + this->_blanketWidth - 55, y + height - 2, L"podpis kasjera", redColor, fontName, labelFontSize, 0, L"None");
    
    // border frame
    //page->addRectangle(mainCanvas, topX, topY, this->_blanketWidth, this->_blanketHeight, L"#ff0000", 1, COMMON_EMPTY_STRING);
}

VOID CKPRedForm::_drawTextContent(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, float topX, float topY,
                        CString day, CString month, CString year, CString nr,
                        CString sender, CString sender2,
                        CString title, CString title2, CString title3,
                        CString amount, CString amountByWords) {
    CString blackColor = L"#000000";
    float fontSize = 12.0;
    float fontLineHeight = 1.3 * fontSize;
    float deltaX = 20;
    float deltaY = 0.0;
    CString value;
    CStringList* lst;
    INT i;
    INT count;
    BOOL finish;
    INT pos;
    CString zl;
    CString gr;
    float width3 = this->_blanketWidth/3;
    float height3 = this->_blanketHeight/3;
    float height6 = this->_blanketHeight/6;
    float x = topX;
    float y = topY - 4;
    
    page->addCollectionOfGlyphs(mainCanvas, x + width3 + 8, y + 48, day, blackColor, fontName, fontSize, deltaX, deltaY);
    page->addCollectionOfGlyphs(mainCanvas, x + width3 + 8 + 44, y + 48, month, blackColor, fontName, fontSize, deltaX, deltaY);
    page->addCollectionOfGlyphs(mainCanvas, x + width3 + 8 + 90, y + 48, year, blackColor, fontName, fontSize, deltaX, deltaY);
    page->addGlyph(mainCanvas, x + 2*width3 + width3/3 + 10, y + height6, nr, blackColor, fontName, fontSize, 0, L"None");
    
    page->addGlyph(mainCanvas, x + 8, y + height6 + 1.78*fontLineHeight, sender, blackColor, fontName, fontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 8, y + height6 + 3.1*fontLineHeight, sender2, blackColor, fontName, fontSize, 0, L"None");
    page->addGlyph(mainCanvas, x + 50, y + height3 + 1.3*fontLineHeight, title, blackColor, fontName, fontSize, 0, L"None");    
    page->addGlyph(mainCanvas, x + 20, y + height3 + 2.7*fontLineHeight, title2, blackColor, fontName, fontSize, 0, L"None");    //lst = title2.explode(L' ', 6);
    page->addGlyph(mainCanvas, x + 20, y + height3 + 4.1*fontLineHeight, title3, blackColor, fontName, fontSize, 0, L"None");    //lst = title2.explode(L' ', 6);
    

    pos = amount.strpos(L",");
    if (pos < 0) {
        zl = amount;
        gr = L"00";
    } else {
        zl = amount.substr(0, pos);
        gr = amount.substr(pos+1);
        if (gr.length() == 1) {
            gr += L"0";
        }
    }
    // Amount
    page->addGlyph(mainCanvas, x + 2*width3 + 20, y + height3 + 1.3*fontLineHeight, zl, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x + 2*width3 + 0.6*width3 - 22, y + height3 + 1.3*fontLineHeight, gr, L"#ff000000", fontName, fontSize);
    // Amount - Together
    page->addGlyph(mainCanvas, x + 2*width3 + 20, y + height3 + 8.0*fontLineHeight, zl, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x + 2*width3 + 0.6*width3 - 22, y + height3 + 8.0*fontLineHeight, gr, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x + 33, y + 2*height3 + 1.4*fontLineHeight, amountByWords, blackColor, fontName, fontSize, 0, L"None");    
}
