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


VOID CPP519ACompactTransferForm::_commonInit() {
    this->setRealClassName(L"CPP519ACompactTransferForm");
}

CPP519ACompactTransferForm::CPP519ACompactTransferForm(CString receiver, CString receiver2, CString receiverAccount, CString amount,
                     CString senderAccount, CString sender, CString sender2,
                     CString title, CString title2)
    : CTransferForm(TRANSFER_TYPE_PP519A_COMPACT, receiver, receiver2, receiverAccount, amount, senderAccount, sender, sender2, title, title2)
{
    this->_commonInit();
    this->_blanketForBankWidth = 0.0;
    this->_blanketForBankHeight = 0.0;
    this->_blanketForCustomerWidth = 0.0;
    this->_blanketForCustomerHeight = 0.0;
}

CPP519ACompactTransferForm::~CPP519ACompactTransferForm() {
}

CXmlNode* CPP519ACompactTransferForm::generateBlanket(CXpsPackage* xps, CXpsDocument* xpsDoc, CXpsPage* page, CString fontName, float pageWidth, float pageHeight, float paddingTop) {
    HANDLE heap = GetProcessHeap();
    DWORD size;
    BYTE* dataBuffer;
    CXmlNode* result = NULL;
    
    if (xps) {
        CXmlNode* mainCanvas;
        CXmlNode* canvas;
        float pageMargin = 20;
        float pageMarginTop = 0;
        INT factor = 10;
        
        if (xpsDoc) {
            this->_blanketForBankWidth = (factor-3) * pageWidth / factor - pageMargin;
            this->_blanketForBankHeight = 340;
            this->_blanketForCustomerWidth = 0.4 * this->_blanketForBankWidth;
            this->_blanketForCustomerHeight = 340;
            
            //dataBuffer = GetPP519aThumbnailData(&size);
            //xpsDoc->addPageThumbnail(1, dataBuffer, size);
            //HeapFree(heap, 0, dataBuffer);
            if (page) {
                CString thinFrameColor = L"#A97285";
                float x, y, deltaX, deltaY;
                float space = 8;
                float strokeMiterLimit = 1.0;
                INT fontSize = 12;
                INT smallerFontSize = 10;
                INT pos;
                CString tmp;
                
                // CANVAS for form data (empty, without data)
                mainCanvas = page->addCanvas();
                result = mainCanvas;
                page->addItem(mainCanvas);
            
                x = (pageWidth - this->_blanketForBankWidth - this->_blanketForCustomerWidth - space - 16)/2;
                y = pageMarginTop + paddingTop;
                
                this->_drawBlanketForCustomer(page, mainCanvas, fontName, x, y);
                page->addDashedVLine(mainCanvas, x + this->_blanketForCustomerWidth + space/2, y - 5, this->_blanketForCustomerHeight + 2 * 5, thinFrameColor, 1, strokeMiterLimit, 2, 2);
                this->_drawBlanketForBank(page, mainCanvas, fontName, x + this->_blanketForCustomerWidth + space, y);
                
                
                // Now we fill in data on blanket 1
                canvas = page->addCanvas();
                page->addItem(canvas);
                x = 268;
                y = pageMarginTop + 21 + paddingTop;
                page->addGlyph(canvas, x, y, this->receiver, L"#ff000000", fontName, fontSize);
                page->addGlyph(canvas, x, y + 30, this->receiver2, L"#ff000000", fontName, fontSize);
                page->addGlyph(canvas, x, y + 142, this->sender, L"#ff000000", fontName, fontSize);
                page->addGlyph(canvas, x, y + 173, this->sender2, L"#ff000000", fontName, fontSize);
                
                CString tmpTitle = this->title + this->title2;
                if (tmpTitle.length() > 64) {
                    page->addGlyph(canvas, x, y + 204, title, L"#ff000000", fontName, smallerFontSize);
                    page->addGlyph(canvas, x, y + 221, title2, L"#ff000000", fontName, smallerFontSize);
                } else {
                    page->addGlyph(canvas, x, y + 204, title, L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 221, title2, L"#ff000000", fontName, fontSize);
                }

                deltaX = 16.9;
                deltaY = 0;
                page->addCollectionOfGlyphs(canvas, x + 33, y + 60, this->receiverAccount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
                page->addCollectionOfGlyphs(canvas, x, y + 117, this->senderAccount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
                page->addCollectionOfGlyphs(canvas, x + 260, y + 88, this->amount, L"#ff000000", fontName, fontSize, deltaX, deltaY);

                // Now we fill in data on compact blanket on the left side
                canvas = page->addCanvas();
                page->addItem(canvas);
                
                // And now text data
                fontSize = 9.0;
                x = 45;
                y = pageMarginTop + 18 + paddingTop;
                pos = this->receiver.strpos(L" ", 21);
                if (pos == -1) {
                    page->addGlyph(canvas, x, y + 64, this->receiver, L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 77, this->receiver2, L"#ff000000", fontName, fontSize);
                } else {
                    page->addGlyph(canvas, x, y + 64, this->receiver.substr(0, pos), L"#ff000000", fontName, fontSize);
                    tmp = this->receiver.substr(pos+1);
                    tmp.trim();
                    pos = tmp.strpos(L" ", 26);
                    if (pos == -1) {
                        page->addGlyph(canvas, x, y + 77, this->receiver.substr(pos+1), L"#ff000000", fontName, fontSize);
                        page->addGlyph(canvas, x, y + 89, this->receiver2, L"#ff000000", fontName, fontSize);
                    } else {
                        page->addGlyph(canvas, x, y + 77, tmp.substr(0, pos), L"#ff000000", fontName, fontSize);
                        tmp = tmp.substr(pos+1);
                        tmp.trim();
                        page->addGlyph(canvas, x, y + 89, tmp, L"#ff000000", fontName, fontSize);
                        page->addGlyph(canvas, x, y + 101, this->receiver2, L"#ff000000", fontName, fontSize);
                    }
                }
                
                pos = this->sender.strpos(L" ", 21);
                if (pos == -1) {
                    page->addGlyph(canvas, x, y + 194, this->sender, L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 206, this->sender2, L"#ff000000", fontName, fontSize);
                } else {
                    page->addGlyph(canvas, x, y + 194, this->sender.substr(0, pos), L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 206, this->sender.substr(pos+1), L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 218, this->sender2, L"#ff000000", fontName, fontSize);
                }
                
                pos = this->title.strpos(L" ", 26);
                if (pos == -1) {
                    page->addGlyph(canvas, x, y + 139, this->title, L"#ff000000", fontName, fontSize);
                    page->addGlyph(canvas, x, y + 151, this->title2, L"#ff000000", fontName, fontSize);
                } else {
                    page->addGlyph(canvas, x, y + 139, this->title.substr(0, pos), L"#ff000000", fontName, fontSize);
                    tmpTitle = this->title.substr(pos+1);
                    pos = tmpTitle.strpos(L" ", 26);
                    if (pos == -1) {
                        page->addGlyph(canvas, x, y + 151, this->title.substr(pos+1), L"#ff000000", fontName, fontSize);
                        page->addGlyph(canvas, x, y + 163, this->title2, L"#ff000000", fontName, fontSize);
                    } else {
                        page->addGlyph(canvas, x, y + 151, tmpTitle.substr(0, pos), L"#ff000000", fontName, fontSize);
                        page->addGlyph(canvas, x, y + 163, tmpTitle.substr(pos+1), L"#ff000000", fontName, fontSize);
                        page->addGlyph(canvas, x, y + 175, this->title2, L"#ff000000", fontName, fontSize);
                    }
                }

                deltaX = 10.0;
                deltaY = 0;
                CString recAcc1 = this->receiverAccount.substr(0, 10);
                CString recAcc2 = this->receiverAccount.substr(10);
                page->addCollectionOfGlyphs(canvas, x, y + 4, recAcc1, L"#ff000000", fontName, fontSize, deltaX, deltaY);
                page->addCollectionOfGlyphs(canvas, x, y + 32, recAcc2, L"#ff000000", fontName, fontSize, deltaX, deltaY);
                //page->addCollectionOfGlyphs(canvas, x, y + 129, this->senderAccount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
                page->addCollectionOfGlyphs(canvas, x, y + 118, this->amount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
            }
        }
    }
    
    return result;
}


VOID CPP519ACompactTransferForm::_drawBlanketForBank(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, float topX, float topY) {
    float x, y, tmp, tmp2, height, width, tmpWidth;
    CString color = L"#A97285";
    CString thinFrameColor = L"#A97285";
    CString thickFrameColor = L"#E64E5D";
    CString fillColor = L"#FFE5EA";
    CString textFieldFillColor = L"#FFFFFF";
    
    float labelFontSize = 7;
    float mediumFontSize = 9;
    float largeFontSize = 11;
    float textMarginFromTopLine = 12;
    float lineCoordCorrection = 6;
    float contentMargin = 30;
    width = this->_blanketForBankWidth - 2*contentMargin;
    float boxUnitSize = width / 27; // because there is 27 input units per full width input box
    float labelPaddingLeft = 4;
    float labelPaddingTop = 0;
    float bottomPadding = 55;
    float strokeMiterLimit = 1.0;
    
    x = topX;
    y = topY;
    height = 26;
    // Main content background
    page->addRectangle(mainCanvas, x + contentMargin, y, this->_blanketForBankWidth - 2*contentMargin, this->_blanketForBankHeight - bottomPadding, thinFrameColor, 0, strokeMiterLimit, fillColor);
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"nazwa odbiorcy  ", width, height,
                                fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0,
                                strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"nazwa odbiorcy cd.  ", width, height,
                                fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0, strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"l.k.", 2*boxUnitSize, height,
                                fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 2,
                                strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);

    page->addLabeledTextField(mainCanvas, x + contentMargin + 2*boxUnitSize, y, L"nr rachunku odbiorcy  ",
                                width - 2*boxUnitSize, height, fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 2, strokeMiterLimit,
                                labelPaddingLeft, labelPaddingTop, 0);
    y += height + 1;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"", 7*boxUnitSize, height, fontName,
                                labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 2, strokeMiterLimit, labelPaddingLeft,
                                labelPaddingTop, 0);
    page->addTextInRectangles(mainCanvas, x + contentMargin + 7*boxUnitSize + 14, y + 11, L"WP", largeFontSize + 4, largeFontSize + 4, 3, fontName, largeFontSize, color, L"None", color, L"#ffffff", 1.2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + contentMargin + 7*boxUnitSize + 14 + 2*(largeFontSize+4+3) - 2, y + 11 + largeFontSize-2, L"*", color, fontName, largeFontSize);
    page->addLabeledTextField(mainCanvas, x + contentMargin + 7*boxUnitSize + 4*boxUnitSize, y, L"waluta",
                                3*boxUnitSize, height, fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0, strokeMiterLimit, labelPaddingLeft,
                                labelPaddingTop, 0);
    page->addLabeledTextField(mainCanvas, x + contentMargin + 7*boxUnitSize + 4*boxUnitSize + 3*boxUnitSize + boxUnitSize, y,
                                L"kwota:  ", 12*boxUnitSize, height, fontName, labelFontSize,
                                color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 2,
                                strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"nr rachunku zleceniodawcy (przelew)/kwota słownie (wpłata)", width, height,
                                fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0,
                                strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"nazwa zleceniodawcy  ", width, height, fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0, strokeMiterLimit,
                                labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"nazwa zleceniodawcy cd.  ", width, height, fontName, labelFontSize, color,
                                L"None", boxUnitSize, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0, strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += height + 3;
    page->addLabeledTextField(mainCanvas, x + contentMargin, y, L"tytułem  ", width, 2*height, fontName, labelFontSize, color,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, fillColor, 0, strokeMiterLimit, labelPaddingLeft, labelPaddingTop, 0);
    y += 2*height;
    
    
    // Main content border
    page->addRectangle(mainCanvas, x + contentMargin, topY, this->_blanketForBankWidth - 2*contentMargin, this->_blanketForBankHeight - bottomPadding, thinFrameColor, 1, strokeMiterLimit);
    
    
    // Bottom part (waterfalls)
    tmp = bottomPadding + 20;
    page->addRectangle(mainCanvas, x + contentMargin, topY + this->_blanketForBankHeight - tmp, width/2, tmp, thinFrameColor, 1, strokeMiterLimit, L"#ffffff");
    page->addSquare(mainCanvas, x + contentMargin + width/2 + 1.5*tmp, topY + this->_blanketForBankHeight - tmp, tmp, thinFrameColor, 1, strokeMiterLimit, L"#ffffff");
    page->addCircle(mainCanvas, x + contentMargin + width/2 + 1.5*tmp + tmp/2, topY + this->_blanketForBankHeight - tmp + tmp/2, tmp/2 - 10, L"#ababab", 1, COMMON_EMPTY_STRING, 1.5, 1.5);
    tmp2 = x + contentMargin + width/2 + 1.5*tmp + tmp + 1;
    page->addRectangle(mainCanvas, tmp2, y + 1, x + contentMargin + width - tmp2 - 1, 20, L"#ffffff", 0, strokeMiterLimit, L"#ffffff");
    
    page->addGlyph(mainCanvas, x + contentMargin + 3*boxUnitSize, topY + this->_blanketForBankHeight - 5, L"pieczęć data i podpis(y) zleceniodawcy", color, fontName, labelFontSize);
    page->addGlyph(mainCanvas, x + contentMargin + width/2 + 1.2*boxUnitSize, y + 70, L"Opłata", color, fontName, labelFontSize);
    
    page->addVerticalGlyph(mainCanvas, x + 3*contentMargin/4 - 3, topY + this->_blanketForBankHeight - 115, L"Polecenie przelewu / wpłata gotówkowa", color, fontName, mediumFontSize, FALSE);
    page->addVerticalGlyph(mainCanvas, x + 7*contentMargin/4 + width - 5, topY + this->_blanketForBankHeight - 140, L"Odcinek dla banku", color, fontName, mediumFontSize, FALSE);
    page->addHLine(mainCanvas, x + lineCoordCorrection, topY, this->_blanketForBankWidth - 2*lineCoordCorrection, thinFrameColor, 1, strokeMiterLimit);
    page->addHLine(mainCanvas, x + lineCoordCorrection, y, this->_blanketForBankWidth - contentMargin - 1.5*lineCoordCorrection, thinFrameColor, 1, strokeMiterLimit);
    page->addHLine(mainCanvas, x + this->_blanketForBankWidth - contentMargin + lineCoordCorrection/2, topY + this->_blanketForBankHeight - bottomPadding, contentMargin - 1.4*lineCoordCorrection, thinFrameColor, 1, strokeMiterLimit);
    
    // border frame
    //page->addRectangle(mainCanvas, topX, topY, this->_blanketForBankWidth, blanketForBankHeight, L"#00ff00", 1, COMMON_EMPTY_STRING);
}



VOID CPP519ACompactTransferForm::_drawBlanketForCustomer(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, float topX, float topY) {
    float x, y, tmp, tmp2, height, width, tmpWidth, r;
    CString color = L"#A97285";
    CString thinFrameColor = L"#A97285";
    CString thickFrameColor = L"#E64E5D";
    CString fillColor = L"#FFE5EA";
    CString textFieldFillColor = L"#FFFFFF";
    
    float labelFontSize = 7;
    float mediumFontSize = 9;
    float textMarginFromTopLine = 12;
    float lineCoordCorrection = 9;
    float contentMarginLeft = 30;
    float contentMarginRight = 5;
    width = this->_blanketForCustomerWidth - contentMarginLeft - contentMarginRight;
    float labelPaddingLeft = 4;
    float labelPaddingTop = 0;
    float strokeMiterLimit = 1.0;
    
    x = topX;
    y = topY;
    height = 29;
    //page->addRectangle(mainCanvas, x + contentMarginLeft, y, width, height, thinFrameColor, 2, textFieldFillColor);
    page->addLabeledTextField(mainCanvas, x + contentMarginLeft, y, L"nr rachunku odbiorcy", width, height, fontName, labelFontSize, thinFrameColor,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, textFieldFillColor, 2, strokeMiterLimit, (width-80)/2, 1, 0);
    
    y += height;
    //page->addRectangle(mainCanvas, x + contentMarginLeft, y, width, height, thinFrameColor, 2, textFieldFillColor);
    page->addLabeledTextField(mainCanvas, x + contentMarginLeft, y, L"nr rachunku odbiorcy c.d.", width, height, fontName, labelFontSize, thinFrameColor,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, textFieldFillColor, 2, strokeMiterLimit, (width-95)/2, 1, 0);

    y += height;
    //page->addRectangle(mainCanvas, x + contentMarginLeft, y, width, 3*height, thinFrameColor, 2, textFieldFillColor);
    page->addLabeledTextField(mainCanvas, x + contentMarginLeft, y, L"odbiorca", width, 2*height, fontName, labelFontSize, thinFrameColor,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, textFieldFillColor, 1, strokeMiterLimit, 4, 1, 0);

    y += 2*height;
    //page->addRectangle(mainCanvas, x + contentMarginLeft, y, width, height, thinFrameColor, 2, textFieldFillColor);
    page->addLabeledTextField(mainCanvas, x + contentMarginLeft, y, L"kwota", width, height, fontName, labelFontSize, thinFrameColor,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, textFieldFillColor, 2, strokeMiterLimit, 4, 1, 0);

    y += height;
    //page->addRectangle(mainCanvas, x + contentMarginLeft, y, width, height, thinFrameColor, 2, textFieldFillColor);
    page->addLabeledTextField(mainCanvas, x + contentMarginLeft, y, L"zleceniodawca", width, 2*height + 2 * (height-5), fontName, labelFontSize, thinFrameColor,
                                L"None", 0, thinFrameColor, textFieldFillColor,
                                COMMON_EMPTY_STRING, textFieldFillColor, 1, strokeMiterLimit, 4, 7*labelFontSize, 0);
    
    page->addVerticalGlyph(mainCanvas, x + (contentMarginLeft-mediumFontSize)/2 + 8, topY + this->_blanketForCustomerHeight - 115, L"POKWITOWANIE DLA ZLECENIODAWCY", color, fontName, mediumFontSize, FALSE);
    r = 35;
    page->addCircle(mainCanvas, x + contentMarginLeft+r, topY + this->_blanketForCustomerHeight - r, r, thinFrameColor, 1, COMMON_EMPTY_STRING, 3.0, 3.0);
    page->addDashedHLine(mainCanvas, x + 2*r + 1.5*r, topY + this->_blanketForCustomerHeight - labelFontSize - 3, 80, thinFrameColor, 1, strokeMiterLimit, 1, 1);
    page->addGlyph(mainCanvas, x + 2*r + 1.5*r + 30, topY + this->_blanketForCustomerHeight - 2, L"opłata", color, fontName, labelFontSize);
    page->addGlyph(mainCanvas, x + contentMarginLeft + r - 15, topY + this->_blanketForCustomerHeight - r - labelFontSize/4, L"Stempel", color, fontName, labelFontSize);
    page->addGlyph(mainCanvas, x + contentMarginLeft + r - 15, topY + this->_blanketForCustomerHeight - r - labelFontSize/4 + labelFontSize + 2, L"dzienny", color, fontName, labelFontSize);

    // border frame
    //page->addRectangle(mainCanvas, topX, topY, blanketForCustomerWidth, blanketForCustomerHeight, L"#0000ff", 1, COMMON_EMPTY_STRING);
}
