/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2011 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstdblankets
 * Version: 1.00
 * First release date:   2011-02-24
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstdblankets library
 * 
 * mzstdblankets library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstdblankets library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#include "mzBSBDWGreenTransferForm.h"
#include "mzStringList.h"
#include "mzstdcurrency.h"


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

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();
}


CBSBDWGreenTransferForm::~CBSBDWGreenTransferForm() {
}

VOID CBSBDWGreenTransferForm::draw(CCanvas* canvas, CString fontName, float pageWidth, float pageHeight, float margin, float blanketSpacing, float thickLine, float thinLine) {
    if (canvas) {
        LONG space;
        
        if (!canvas->isPrintCanvas()) {
            if (canvas->getDC()) {
                canvas->endPaint();
            }
            canvas->setSize(pageWidth, pageHeight);
            this->_topMargin = margin;
            this->_leftMargin = margin;
            space = blanketSpacing;
            this->_thinLine = (LONG)thinLine;
            this->_thickLine = (LONG)thickLine;
            this->_tbTextMargin = 4;
            this->_lrTextMargin = 4;
        } else {
            LONG lx = GetDeviceCaps(canvas->getDC(), LOGPIXELSX);
            LONG ly = GetDeviceCaps(canvas->getDC(), LOGPIXELSY);
            this->_topMargin = ly/25.4 * margin;
            this->_leftMargin = lx/25.4 * margin;
            space = ly/25.4 * blanketSpacing;
            this->_thinLine = thinLine * ly/25.4;
            this->_thickLine = thickLine * ly/25.4;
            this->_tbTextMargin = MulDiv(10, ly, 72)/3;
            this->_lrTextMargin = MulDiv(10, lx, 72)/3;
        }
        
        this->_blanketWidth = (pageWidth-2*this->_leftMargin)*0.75;
        this->_blanketHeight = (pageHeight-2*this->_topMargin-2*space)/3;
        
        float unitsCount = 9;
        LONG unitHeight = this->_blanketHeight/unitsCount;
        this->_segmentHeights[0] = unitHeight;
        this->_segmentHeights[1] = unitHeight;
        this->_segmentHeights[2] = ((unitsCount-3.5)/2)*unitHeight - unitHeight/2;
        this->_segmentHeights[3] = 1.5*unitHeight;
        this->_segmentHeights[4] = ((unitsCount-3.5)/2)*unitHeight + unitHeight/2;
        LONG sum = this->_segmentHeights[0] + this->_segmentHeights[1] + this->_segmentHeights[2] + this->_segmentHeights[3] + this->_segmentHeights[4];
        if (sum != this->_blanketHeight) {
            if (sum > this->_blanketHeight) {
                this->_segmentHeights[4] -= sum - this->_blanketHeight;
            } else {
                this->_segmentHeights[4] += this->_blanketHeight - sum;
            }
        }
        this->_amountByWordsLabelText = L"ZŁ ............ gr ........";
        this->_amountByWordsLabelText_part1_zl = L"ZŁ ";
        this->_amountByWordsLabelText_part2_dots = L"............ ";
        this->_amountByWordsLabelText_part3_gr = L"gr ";
        this->_borderColor = RGB(0x00, 0x8e, 0x4d); // #008E4D

        canvas->beginPaint();

        CString color = L"#008E4D";

        float x = (pageWidth - this->_blanketWidth) / 2;
        float y = this->_topMargin;
        
        // BLANKET #1
        this->_drawBlanket(canvas, fontName, L"I", L"Dowód wpłaty", x, y);
        // Fill in data on blanket 1
        this->_drawTextContent(canvas, fontName, x, y);

        // BLANKET #2
        y += this->_blanketHeight + space;
        canvas->drawHLine(this->_borderColor, x/2, y-space/2, this->_blanketWidth + x, 1, PS_DASH);
        this->_drawBlanket(canvas, fontName, L"II", L"Pokwitowanie", x, y);
        this->_drawTextContent(canvas, fontName, x, y);

        // BLANKET #3
        y += this->_blanketHeight + space;
        canvas->drawHLine(this->_borderColor, x/2, y-space/2, this->_blanketWidth + x, 1, PS_DASH);
        this->_drawBlanket(canvas, fontName, L"III", L"Zawiadomienio o księgowaniu", x, y);
        this->_drawTextContent(canvas, fontName, x, y);
    }
}

VOID CBSBDWGreenTransferForm::_drawBlanket(CCanvas* canvas, CString fontName, WCHAR* number, WCHAR* title, float topX, float topY) {
    float x, y, tmp, tmp2, height;
    float largeFontSize = 11;
    float bigFontSize = 9;
    float smallFontSize = 8;
    float tinyFontSize = 6;
    SIZE size, size1, size2;

    COLORREF blanketFontColor = RGB(0x00, 0x8e, 0x4d); // #008E4D
    LONG boldWeight = 600;
    LONG normalWeight = 300;
    
    CFont* largeFont = new CFont(canvas->getDC(), blanketFontColor, fontName, largeFontSize, 0, boldWeight, FALSE, FALSE, FALSE);
    if (!largeFont) {
        return;
    }
    CFont* bigFont = new CFont(canvas->getDC(), blanketFontColor, fontName, bigFontSize, 0, boldWeight, FALSE, FALSE, FALSE);
    if (!bigFont) {
        delete largeFont;
        return;
    }
    CFont* smallFont = new CFont(canvas->getDC(), blanketFontColor, fontName, smallFontSize, 0, normalWeight, FALSE, FALSE, FALSE);
    if (!smallFont) {
        delete largeFont;
        delete bigFont;
        return;
    }
    CFont* tinyFont = new CFont(canvas->getDC(), blanketFontColor, fontName, tinyFontSize, 0, normalWeight, FALSE, FALSE, FALSE);
    if (!tinyFont) {
        delete largeFont;
        delete bigFont;
        delete smallFont;
        return;
    }
    
    // Necessary because of details of drawing shapes of more than 1 border size
    // (Actually correction is not proper for all sizes just for size=2)
    LONG pixelCorrection = this->_thickLine/2;
    // If printing - correction must be recalculated else
    if (canvas->isPrintCanvas()) {
        pixelCorrection /= 2;
    }
    
    // -----------------------------------------------------------------------------------------------
    // SEGMENT #1
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY;
    height = this->_segmentHeights[0];
    canvas->drawVLine(this->_borderColor, x+height, y, height, this->_thickLine);
    size = largeFont->measureString(number, canvas->getDC());
    canvas->drawText(largeFont, number, x+height-size.cx-this->_lrTextMargin, y+(height-size.cy)/2, TRUE, 0, -1);
    canvas->drawText(bigFont, L"BANKOWY DOWÓD WPŁATY", x+height+this->_lrTextMargin, y+2, TRUE, 0, -1);
    size = bigFont->measureString(title, canvas->getDC());
    canvas->drawText(smallFont, title, x+height+this->_lrTextMargin, y+height-size.cy-this->_tbTextMargin/2, TRUE, 0, -1);
    size = smallFont->measureString(this->_amountByWordsLabelText, canvas->getDC());
    canvas->drawText(smallFont, this->_amountByWordsLabelText, x+this->_blanketWidth-size.cx-this->_lrTextMargin, y+height-size.cy-2*this->_thinLine, TRUE, 0, -1);
    canvas->drawHLine(this->_borderColor, x+this->_blanketWidth-size.cx-this->_lrTextMargin-2, y+height-3*this->_thinLine, size.cx+this->_lrTextMargin+2, this->_thinLine);
    // -----------------------------------------------------------------------------------------------

    // -----------------------------------------------------------------------------------------------
    // SEGMENT #2
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + this->_segmentHeights[0];
    height = this->_segmentHeights[1];
    canvas->drawRectangle(this->_borderColor, x, y, this->_blanketWidth, height, this->_thickLine);
    size = smallFont->measureString(L"Słownie", canvas->getDC());
    canvas->drawText(smallFont, L"Słownie", x+this->_lrTextMargin, y+this->_tbTextMargin, TRUE, 0, -1);
    tmp = size.cy;
    size = smallFont->measureString(L"złotych", canvas->getDC());
    canvas->drawText(smallFont, L"złotych", x+this->_lrTextMargin, y+tmp+this->_tbTextMargin, TRUE, 0, -1);
    // -----------------------------------------------------------------------------------------------

    // -----------------------------------------------------------------------------------------------
    // SEGMENT #3
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1] - pixelCorrection;
    height = this->_segmentHeights[2];
    canvas->drawRectangle(this->_borderColor, x, y, this->_blanketWidth, height, this->_thickLine);
    canvas->drawVLine(this->_borderColor, x+this->_blanketWidth/2, y, height-this->_thinLine, this->_thickLine);
    canvas->drawHLine(this->_borderColor, x+this->_blanketWidth/2, y + height/2, this->_blanketWidth/2, this->_thickLine);
    canvas->drawText(smallFont, L"Nazwa i adres odbiorcy wpłaty", x+this->_lrTextMargin, y+this->_tbTextMargin, TRUE, 0, -1);
    canvas->drawText(smallFont, L"Na dobro rachunku numer", x+this->_blanketWidth/2+this->_lrTextMargin, y+this->_tbTextMargin, TRUE, 0, -1);
    canvas->drawText(smallFont, L"w", x+this->_blanketWidth/2+this->_lrTextMargin, y+height/2+this->_tbTextMargin, TRUE, 0, -1);
    // -----------------------------------------------------------------------------------------------

    // -----------------------------------------------------------------------------------------------
    // SEGMENT #4
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1] + this->_segmentHeights[2] - 2*pixelCorrection;
    height = this->_segmentHeights[3];
    canvas->drawRectangle(this->_borderColor, x, y, this->_blanketWidth, height, this->_thickLine);
    tmp = x + this->_blanketWidth/2 + 2*(this->_blanketWidth/2)/3;
    tmp2 = this->_blanketWidth - (tmp - x);
    canvas->drawVLine(this->_borderColor, tmp, y, height-this->_thinLine, this->_thickLine);
    canvas->drawText(smallFont, L"Tytuł wpłaty", x+this->_lrTextMargin, y+this->_tbTextMargin, TRUE, 0, -1);
    size = smallFont->measureString(L"Symbol", canvas->getDC());
    canvas->drawText(smallFont, L"Symbol", tmp+(tmp2-size.cx)/2, y+this->_tbTextMargin, TRUE, 0, -1);
    size = smallFont->measureString(L"planu kas", canvas->getDC());
    canvas->drawText(smallFont, L"planu kas.", tmp+(tmp2-size.cx)/2, y+size.cy+this->_tbTextMargin, TRUE, 0, -1);
    // -----------------------------------------------------------------------------------------------

    // -----------------------------------------------------------------------------------------------
    // SEGMENT #5
    // -----------------------------------------------------------------------------------------------
    x = topX;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1] + this->_segmentHeights[2] + this->_segmentHeights[3] - 3*pixelCorrection;
    height = this->_segmentHeights[4];
    //canvas->drawRectangle(this->_borderColor, x, y, this->_blanketWidth, height, 2);
    tmp = height/4;
    tmp2 = height - tmp;
    
    canvas->drawSquare(this->_borderColor, x, y+tmp2+pixelCorrection, tmp, this->_thickLine);
    canvas->drawTinyText(tinyFont, L"PISAĆ BEZ KALKI", x, y+3*height/4-this->_tbTextMargin, TRUE, 0, -1, -3.14/2);
    canvas->drawRectangle(this->_borderColor, x+tmp-pixelCorrection, y, 2*this->_blanketWidth/3, height, this->_thickLine);
    canvas->drawText(smallFont, L"Nazwa (nazwisko i imię), adres wpłacającego", x+height/4+this->_lrTextMargin, y+this->_tbTextMargin, TRUE, 0, -1);
    tmp = x + height/4 + 2*this->_blanketWidth/3;
    tmp += (this->_blanketWidth - height/4 - 2*this->_blanketWidth/3)/2;
    size = smallFont->measureString(L"Stempel banku", canvas->getDC());
    canvas->drawText(smallFont, L"Stempel banku", tmp-size.cx/2, y+height-size.cy, TRUE, 0, -1);
    size = tinyFont->measureString(L"Portfel nr .........", canvas->getDC());
    canvas->drawTinyText(tinyFont, L"Portfel nr .........", x+this->_blanketWidth-size.cy-this->_lrTextMargin/2, y+height-this->_tbTextMargin/2, TRUE, 0, -1, -3.14/2);
    LONG r = height/3;
    canvas->drawCircle(this->_borderColor, tmp, y+height/2-size.cy/2, r, this->_thinLine);
    // -----------------------------------------------------------------------------------------------
    //canvas->drawRectangle(RGB(0xff, 0x00, 0x00), topX, topY, this->_blanketWidth, this->_blanketHeight, 1);
    
    // Border of segment #1
    //canvas->drawRectangle(RGB(0xff, 0x00, 0x00), topX, topY, this->_blanketWidth, this->_segmentHeights[0], 1);
    // Border of segment #2
    //canvas->drawRectangle(RGB(0x00, 0xff, 0x00), topX, topY+this->_segmentHeights[0], this->_blanketWidth, this->_segmentHeights[1], 1);
    // Border of segment #3
    //canvas->drawRectangle(RGB(0x00, 0x00, 0xff), topX, topY+this->_segmentHeights[0]+this->_segmentHeights[1], this->_blanketWidth, this->_segmentHeights[2], 1);
    // Border of segment #4
    //canvas->drawRectangle(RGB(0xff, 0x00, 0xff), topX, topY+this->_segmentHeights[0]+this->_segmentHeights[1]+this->_segmentHeights[2], this->_blanketWidth, this->_segmentHeights[3], 1);
    // Border of segment #5
    //canvas->drawRectangle(RGB(0x00, 0xff, 0xff), topX, topY+this->_segmentHeights[0]+this->_segmentHeights[1]+this->_segmentHeights[2]+this->_segmentHeights[3], this->_blanketWidth, this->_segmentHeights[4], 1);

    delete largeFont;
    delete bigFont;
    delete smallFont;
    delete tinyFont;
}

VOID WINAPI CBSBDWGreenTransferForm::_drawTextContent(CCanvas* canvas, CString fontName, float topX, float topY) {
    float x, y, deltaX, deltaY;
    LONG fontSize = 8;
    CString zl;
    CString gr;
    INT pos;
    LONG normalWeight = 300;
    COLORREF black = RGB(0x00, 0x00, 0x00);
    LONG verticalTextSpacing = MulDiv(10, GetDeviceCaps(canvas->getDC(), LOGPIXELSY), 72)/4;
    SIZE size;
    
    CFont* font = new CFont(canvas->getDC(), black, fontName, fontSize, 0, normalWeight, FALSE, FALSE, FALSE);
    if (!font) {
        return;
    }
    CStringList* lstReceiver = this->receiver2.explode(L'\n');
    CStringList* lstSender = this->sender2.explode(L'\n');

    // DRAWTEXT:            RECEIVER
    x = topX + this->_lrTextMargin;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1];
    size = font->measureString(this->receiver, canvas->getDC());
    y += size.cy + 1.5*this->_tbTextMargin; // add height of text: L"Nazwa i adres odbiorcy wpłaty"
    canvas->drawText(font, this->receiver, x, y, TRUE, 0, -1);
    y += size.cy + verticalTextSpacing;
    if (lstReceiver && lstReceiver->size() > 0) {
        for (ULONG i = 0; i < lstReceiver->size(); i++) {
            canvas->drawText(font, lstReceiver->getAt(i), x, y, TRUE, 0, -1);
            y += size.cy + verticalTextSpacing;
        }
    } else {
        canvas->drawText(font, this->receiver2, x, y, TRUE, 0, -1);
    }
    
    
    // DRAWTEXT:            SENDER
    x = topX + this->_segmentHeights[4]/4 + this->_lrTextMargin;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1] + this->_segmentHeights[2] + this->_segmentHeights[3];
    size = font->measureString(this->sender, canvas->getDC());
    y += size.cy + 1.5*this->_tbTextMargin; // add height of text: L"Nazwa i adres wpłacającego" and margin
    canvas->drawText(font, this->sender, x, y, TRUE, 0, -1);
    y += size.cy + verticalTextSpacing;
    if (lstSender && lstSender->size() > 0) {
        for (ULONG i = 0; i < lstSender->size(); i++) {
            canvas->drawText(font, lstSender->getAt(i), x, y, TRUE, 0, -1);
            y += size.cy + verticalTextSpacing;
        }
    } else {
        canvas->drawText(font, this->sender2, x, y, TRUE, 0, -1);
    }
    
    
    // DRAWTEXT:            TITLE
    x = topX + this->_lrTextMargin;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1] + this->_segmentHeights[2];
    size = font->measureString(this->title, canvas->getDC());
    y += size.cy + this->_tbTextMargin; // add height of text: L"Tytuł wpłaty"
    canvas->drawText(font, this->title, x, y, TRUE, 0, -1);
    y += size.cy + verticalTextSpacing;
    canvas->drawText(font, this->title2, x, y, TRUE, 0, -1);
    
    
    // DRAWTEXT:            AMOUNT_BY_WORDS
    x = topX + this->_lrTextMargin;
    y = topY + this->_segmentHeights[0];
    size = font->measureString(L"Słownie", canvas->getDC());
    x += size.cx + 2*this->_lrTextMargin;
    y += (this->_segmentHeights[1]-size.cy)/2;
    canvas->drawText(font, this->amountByWords, x, y, TRUE, 0, -1);
    
    
    // DRAWTEXT:            RECEIVER_ACCOUNT & BANK
    x = topX + this->_blanketWidth/2 + this->_lrTextMargin;
    y = topY + this->_segmentHeights[0] + this->_segmentHeights[1];
    size = font->measureString(L"Na dobro rachunku nr", canvas->getDC());
    y += size.cy + 1.5*this->_tbTextMargin;
    CString account = FormatBankAccount(this->receiverAccount);
    canvas->drawText(font, account, x, y, TRUE, 0, -1);
    y += this->_segmentHeights[2]/2;
    canvas->drawText(font, this->receiverBank, x, y, TRUE, 0, -1);
    
    
    // DRAWTEXT:            AMOUNT
    pos = this->amount.strpos(L",");
    if (pos < 0) {
        pos = this->amount.strpos(L".");
        if (pos < 0) {
            zl = this->amount;
            gr = L"0";
        }
    }
    if (pos >= 0 && zl == COMMON_EMPTY_STRING && gr == COMMON_EMPTY_STRING) {
        zl = this->amount.substr(0, pos);
        gr = this->amount.substr(pos+1);
    }
    x = topX + this->_blanketWidth;
    y = topY + this->_segmentHeights[0];
    size = font->measureString(this->_amountByWordsLabelText, canvas->getDC());
    x -= size.cx;
    x -= this->_lrTextMargin;
    size = font->measureString(this->_amountByWordsLabelText_part1_zl, canvas->getDC());
    x += size.cx;
    size = font->measureString(zl, canvas->getDC());
    y -= size.cy;
    y -= this->_tbTextMargin/2;
    canvas->drawText(font, zl, x, y, TRUE, 0, -1);
    size = font->measureString(this->_amountByWordsLabelText_part2_dots, canvas->getDC());
    x += size.cx;
    size = font->measureString(this->_amountByWordsLabelText_part3_gr, canvas->getDC());
    x += size.cx;
    canvas->drawText(font, gr, x, y, TRUE, 0, -1);

    if (lstSender) {
        delete lstSender;
    }
    if (lstReceiver) {
        delete lstReceiver;
    }
    delete font;
}
