package com.capgemini.reports.templates;

import lombok.Getter;

/** Represents a Cell's format: stretch with overflow, color, alignment. */
@Getter
public class CellFormat {

    private static final String EMPTY_STRING = "";
    private static final String BEGIN_COLOR = "\" mode=\"Opaque\" backcolor=\"";
    private static final String FORECOLOR = " forecolor =\"";
    private static final String END_COLOR = "";
    private static final String ALIGNMENT_BEGIN = " textAlignment=\"";
    private static final String ALIGNMENT_END = "\"";
    private static final String BEGIN_BOLD = "\n  <font isBold=\"";
    private static final String END = "\"/>";
    private static final String VERTICAL_ALIGNEMENT = " verticalAlignment=\"";
    private static final String END_ALIGNEMENT = ">";

    protected String alignment;
    protected String color;
    protected String verticalAligement;

    private String isBold;
    protected String colorForecolor;

    /** Basic constructor. Color is black, Alignment is default (left). */
    public CellFormat() {
        this.alignment = EMPTY_STRING;
        this.color = EMPTY_STRING;
        this.isBold = EMPTY_STRING;
        this.verticalAligement = EMPTY_STRING;
    }

    /**
     * Specify an {@link Alignment}.
     * 
     * @param anAlignment
     * @return same object, for chaining
     */
    public CellFormat setAlignment(final Alignment anAlignment) {
        this.alignment = makeAlignmentDeclaration(anAlignment);
        return this;
    }

    /**
     * If null, the cell is transparent. Otherwise, this is a color code like '#E3E3E3' -> grey.
     * 
     * @param colorID
     * @return this same object, for chaining
     */
    public CellFormat setColor(final String colorID) {
        this.color = makeColorDeclaration(colorID);
        return this;
    }

    /**
     * Method which allow to put the value of a cell in bold.
     * 
     * @param isBold
     * @return the object CellFormat
     */
    public CellFormat setBold(final boolean isBold) {
        this.isBold = makeBoldDeclaration(isBold);
        return this;
    }

    /**
     * Make an alignment for the cell's value.
     * 
     * @param nameVerticalAlignement
     */
    public void setAlignment(final String nameVerticalAlignement) {
        this.verticalAligement = makeVerticalAlignmentDeclaration(nameVerticalAlignement);
    }

    private String makeBoldDeclaration(boolean isBold2) {
        return isBold == null ? EMPTY_STRING : BEGIN_BOLD + isBold2 + END;
    }

    private String makeColorDeclaration(final String aColor) {
        return aColor == null ? EMPTY_STRING : BEGIN_COLOR + aColor + END_COLOR;
    }

    private String makeAlignmentDeclaration(final Alignment anAlignment) {
        return anAlignment == null ? EMPTY_STRING : ALIGNMENT_BEGIN + anAlignment.toString() + ALIGNMENT_END;
    }

    private String makeVerticalAlignmentDeclaration(final String anAlignment) {
        return anAlignment == null ? EMPTY_STRING : VERTICAL_ALIGNEMENT + anAlignment;
    }

}
