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


VOID CBSBDWGreenTransferForm::_commonInit(CString title, CString title2) {
    this->setRealClassName(L"CBSBDWGreenTransferForm");
}

CBSBDWGreenTransferForm::CBSBDWGreenTransferForm(CString receiver, CString receiver2, CString receiverAccount, CString amount,
                     CString senderAccount, CString sender, CString sender2,
                     CString title, CString title2)
    : CTransferForm(TRANSFER_TYPE_BSBDW_GREEN, receiver, receiver2, receiverAccount, amount, senderAccount, sender, sender2, title, title2)
{
    this->_commonInit(title, title2);
    this->_mainFormWidth = 0.0;
    this->_mainFormHeight = 0.0;
    this->_mainFormLeftTopX = 0.0;
    this->_mainFormLeftTopY = 0.0;
    this->_gapDash = 2.1;
}


CBSBDWGreenTransferForm::CBSBDWGreenTransferForm(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_BSBDW_GREEN, receiver, receiver2, receiverAccount, receiverBank, amount, senderAccount, sender, sender2,
                    title, title2, kasPlanSymbol, amountByWords)
{
    this->_commonInit(title, title2);
    this->_mainFormWidth = 0.0;
    this->_mainFormHeight = 0.0;
    this->_mainFormLeftTopX = 0.0;
    this->_mainFormLeftTopY = 0.0;
    this->_gapDash = 2.1;
}

CBSBDWGreenTransferForm::~CBSBDWGreenTransferForm() {
}

CXmlNode* CBSBDWGreenTransferForm::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->_mainFormWidth = 534;
            this->_mainFormHeight = 320;
            this->_mainFormLeftTopX = (pageWidth - this->_mainFormWidth) / 2;
            this->_mainFormLeftTopY = 57 + paddingTop;
            
            //dataBuffer = GetPP519aThumbnailData(&size);
            //xpsDoc->addPageThumbnail(1, dataBuffer, size);
            //HeapFree(heap, 0, dataBuffer);
            if (page) {
                CString color = L"#008E4D";
                
                // CANVAS for form data (empty, without data)
                mainCanvas = page->addCanvas();
                result = mainCanvas;
                page->addItem(mainCanvas);
            
                INT space = 25;
                float x, y = this->_mainFormLeftTopY;
                this->_drawBlanket(page, mainCanvas, fontName, L"I", L"Dowód wpłaty", this->_mainFormLeftTopX, y);
                y += this->_mainFormHeight + space;
                
                page->addDashedHLine(mainCanvas, 10, y - space/2, pageWidth - 20, color, 1, 1, 2, 2);
                
                this->_drawBlanket(page, mainCanvas, fontName, L"II", L"Pokwitowanie", this->_mainFormLeftTopX, y);
                y += this->_mainFormHeight + space;
                
                page->addDashedHLine(mainCanvas, 10, y - space/2, pageWidth - 20, color, 1, 1, 2, 2);
                
                this->_drawBlanket(page, mainCanvas, fontName, L"III", L"Zawiadomienio o księgowaniu", this->_mainFormLeftTopX, y);
                
                
                
                
                // Now we fill in data on blanket 1
                
                canvas = page->addCanvas();
                page->addItem(canvas);
                x = 140;
                y = 10;
                this->_drawTextContent(page, canvas, fontName, x, y,
                        this->receiver, this->receiver2, this->receiverAccount, this->receiverBank,
                        this->sender, this->sender2, this->title, this->title2,
                        this->amount, this->amountByWords);
                
                
                
                
                // Now we fill in data on blanket 2
                canvas = page->addCanvas();
                page->addItem(canvas);
                x = 140;
                y = 355;
                this->_drawTextContent(page, canvas, fontName, x, y,
                        this->receiver, this->receiver2, this->receiverAccount, this->receiverBank,
                        this->sender, this->sender2, this->title, this->title2,
                        this->amount, this->amountByWords);
                
                // Now we fill in data on blanket 3
                canvas = page->addCanvas();
                page->addItem(canvas);
                x = 140;
                y = 700;
                this->_drawTextContent(page, canvas, fontName, x, y,
                        this->receiver, this->receiver2, this->receiverAccount, this->receiverBank,
                        this->sender, this->sender2, this->title, this->title2,
                        this->amount, this->amountByWords);
            }
        }
    }
    
    return result;
}

VOID CBSBDWGreenTransferForm::_drawBlanket(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, WCHAR* number, WCHAR* title, float topX, float topY) {
    float x, y, tmp, height;
    CString color = L"#008E4D";
    float bigFontSize = 12;
    float smallFontSize = 10;
    float textMarginFromTopLine = 12;
    float lineCoordCorrection = 0;
    float strokeMiterLimit = 1.0;
    
    //dataBuffer = GetPP519aFormData(&size);
    //CHAR *subxml = (CHAR*)dataBuffer;
    //canvas->addSubxmlTextChild(subxml);
    //HeapFree(heap, 0, dataBuffer);
    //page->addRectangle(mainCanvas, x, y, mainFormWidth, mainFormHeight, L"#0000ff", 2, L"#ff0000");

    float segmentPercents[5] = { 0.0968, 0.0968, 0.3125, 0.1500, 0.3437 };
    float segmentHeights[5] = { segmentPercents[0] * this->_mainFormHeight,
                                segmentPercents[1] * this->_mainFormHeight,
                                segmentPercents[2] * this->_mainFormHeight,
                                segmentPercents[3] * this->_mainFormHeight,
                                segmentPercents[4] * this->_mainFormHeight };
    // -----------------------------------------------------------------------------------------------
    // Now we are drawing first line-segment of blanket
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY;
    height = segmentHeights[0];
    page->addLine(mainCanvas, x + 28, y, x + 28, y + height, color, 2, strokeMiterLimit);
    page->addHLine(mainCanvas, x + 432, y + 28, 101, color, 2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + 16 - (wcslen(number)-1)*7, y + 20, number, color, fontName, 1.5 * bigFontSize, 0.0, L"BoldSimulation");
    page->addGlyph(mainCanvas, x + 32, y + textMarginFromTopLine, L"BANKOWY DOWÓD WPŁATY", color, fontName, bigFontSize, 0.0, L"BoldSimulation");
    page->addGlyph(mainCanvas, x + 32, y + height - 4, title, color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, x + 414, y + 26, L"ZŁ ................ gr ........", color, fontName, smallFontSize);
    // -----------------------------------------------------------------------------------------------
    
    // -----------------------------------------------------------------------------------------------
    // Now we are drawing second line-segment of blanket
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + segmentHeights[0];
    height = segmentHeights[1];
    page->addRectangle(mainCanvas, x, y, this->_mainFormWidth, height, color, 2, strokeMiterLimit);
    tmp = (height - 2*smallFontSize - 4) / 2 + textMarginFromTopLine/2 + 2;
    page->addGlyph(mainCanvas, x + 4, y + tmp, L"Słownie", color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, x + 4, y + tmp + smallFontSize + 4, L"złotych", color, fontName, smallFontSize);
    // -----------------------------------------------------------------------------------------------
    
    // -----------------------------------------------------------------------------------------------
    // Now we are drawing third line-segment of blanket
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + segmentHeights[0] + segmentHeights[1];
    height = segmentHeights[2];
    page->addRectangle(mainCanvas, x, y, this->_mainFormWidth, height, color, 2, strokeMiterLimit);
    page->addVLine(mainCanvas, x + this->_mainFormWidth/2, y + lineCoordCorrection, height - 2 * lineCoordCorrection, color, 2, strokeMiterLimit);
    page->addHLine(mainCanvas, x + this->_mainFormWidth/2 + lineCoordCorrection, y + height/2, this->_mainFormWidth/2 - 2*lineCoordCorrection, color, 2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + 4, y + textMarginFromTopLine, L"Nazwa i adres odbiorcy wpłaty", color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, x + this->_mainFormWidth/2 + 4, y + textMarginFromTopLine, L"Na dobro rachunku numer", color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, x + this->_mainFormWidth/2 + 4, y + height/2 + textMarginFromTopLine, L"w", color, fontName, smallFontSize);
    // -----------------------------------------------------------------------------------------------
    
    // -----------------------------------------------------------------------------------------------
    // Now we are drawing fourth line-segment of blanket
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + segmentHeights[0] + segmentHeights[1] + segmentHeights[2];
    height = segmentHeights[3];
    page->addRectangle(mainCanvas, x, y, this->_mainFormWidth, height, color, 2, strokeMiterLimit);
    tmp = x + height/4 + 2*this->_mainFormWidth/3;
    tmp += (this->_mainFormWidth - (height/4 + 2*this->_mainFormWidth/3)) / 2;
    page->addVLine(mainCanvas, tmp, y + lineCoordCorrection, height - 2*lineCoordCorrection, color, 2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + 4, y + textMarginFromTopLine, L"Tytuł wpłaty", color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, tmp + 24, y + textMarginFromTopLine, L"Symbol", color, fontName, smallFontSize);
    page->addGlyph(mainCanvas, tmp + 17, y + smallFontSize + textMarginFromTopLine, L"planu kas.", color, fontName, smallFontSize);
    // -----------------------------------------------------------------------------------------------
    
    // -----------------------------------------------------------------------------------------------
    // Now we are drawing fifth line-segment of blanket
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + segmentHeights[0] + segmentHeights[1] + segmentHeights[2] + segmentHeights[3];
    height = segmentHeights[4];
    page->addSquare(mainCanvas, x, y + 3*height/4, height/4, color, 2, strokeMiterLimit);
    page->addLabel(mainCanvas, x, y + 3*height/4 - 8, L"PISAĆ BEZ KALKI", 69, height/9, fontName, 7, color, L"None", color, L"", 1, strokeMiterLimit, 3, 2, -1.57);
    page->addRectangle(mainCanvas, x + height/4, y, 2*this->_mainFormWidth/3, height, color, 2, strokeMiterLimit);
    page->addGlyph(mainCanvas, x + height/4 + 4, y + textMarginFromTopLine, L"Nazwa (nazwisko i imię), adres wpłacającego", color, fontName, smallFontSize);
    tmp = x + height/4 + 2*this->_mainFormWidth/3;
    tmp += (this->_mainFormWidth - (height/4 + 2*this->_mainFormWidth/3)) / 2;
    tmp -= 75 / 2; // 30 = assumed length of "Stempel banku" text
    page->addGlyph(mainCanvas, tmp, y + height - 2, L"Stempel banku", color, fontName, smallFontSize);
    page->addVerticalGlyph(mainCanvas, this->_mainFormLeftTopX + this->_mainFormWidth, y + height, L"Portfel nr .........", color, fontName, smallFontSize, FALSE);
    page->addCircle(mainCanvas, tmp + 75/2, y + height/2 - smallFontSize/2, height/2 - smallFontSize/2 - 4, color, 1, COMMON_EMPTY_STRING, this->_gapDash);
    // -----------------------------------------------------------------------------------------------
}

VOID WINAPI CBSBDWGreenTransferForm::_drawTextContent(CXpsPage* page, CXmlNode* mainCanvas, CString fontName, float topX, float topY,
                            CString receiver, CString receiver2, CString receiverAccount, CString receiverBank,
                            CString sender, CString sender2,
                            CString title, CString title2,
                            CString amount, CString amountByWords) {
    // Now we fill in data on blanket 3
    CStringList* lstReceiver = receiver2.explode(L'\n');
    CStringList* lstSender = sender2.explode(L'\n');
    CXmlNode* canvas;
    float x, y, deltaX, deltaY;
    float fontSize = 10.0;
    float lineHeight = 1.3 * fontSize;
    CString zl;
    CString gr;
    INT pos;
    
    x = topX;
    y = topY;
    page->addGlyph(mainCanvas, x, y + 140, receiver, L"#ff000000", fontName, fontSize);
    if (lstReceiver && lstReceiver->size() == 2) {
        page->addGlyph(mainCanvas, x, y + 140 + lineHeight, lstReceiver->getAt(0), L"#ff000000", fontName, fontSize);
        page->addGlyph(mainCanvas, x, y + 140 + 2*lineHeight, lstReceiver->getAt(1), L"#ff000000", fontName, fontSize);
    } else {
        page->addGlyph(mainCanvas, x, y + lineHeight, receiver2, L"#ff000000", fontName, fontSize);
    }
    page->addGlyph(mainCanvas, x + 30, y + 288, sender, L"#ff000000", fontName, fontSize);
    if (lstSender && lstSender->size() == 2) {
        page->addGlyph(mainCanvas, x + 30, y + 288 + lineHeight, lstSender->getAt(0), L"#ff000000", fontName, fontSize);
        page->addGlyph(mainCanvas, x + 30, y + 288 + 2*lineHeight, lstSender->getAt(1), L"#ff000000", fontName, fontSize);
    } else {
        page->addGlyph(mainCanvas, x + 30, y + 288 + lineHeight, sender2, L"#ff000000", fontName, fontSize);
    }
    page->addGlyph(mainCanvas, x, y + 236, title, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x, y + 236 + lineHeight, title2, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x + 40, y + 100, amountByWords, L"#ff000000", fontName, fontSize);
    //page->addGlyph(mainCanvas, x + 460, y + 245, kasPlanSymbol, L"#ff000000", fontName, fontSize);

    deltaX = 7.9;
    deltaY = 0;
    page->addCollectionOfGlyphs(mainCanvas, x + 260, y + 140, receiverAccount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
    page->addGlyph(mainCanvas, x + 260, y + 190, receiverBank, L"#ff000000", fontName, fontSize);
    //page->addCollectionOfGlyphs(mainCanvas, x, y + 117, this->senderAccount, L"#ff000000", fontName, fontSize, deltaX, deltaY);
    pos = this->amount.strpos(L",");
    if (pos < 0) {
        zl = this->amount;
        gr = L"00";
    } else {
        zl = this->amount.substr(0, pos);
        gr = this->amount.substr(pos+1);
        if (gr.length() == 1) {
            gr += L"0";
        }
    }
    page->addGlyph(mainCanvas, x + 425, y + 70, zl, L"#ff000000", fontName, fontSize);
    page->addGlyph(mainCanvas, x + 500, y + 70, gr, L"#ff000000", fontName, fontSize);
    
    if (lstSender) {
        delete lstSender;
    }
    if (lstReceiver) {
        delete lstReceiver;
    }
}
