package cnp.ew.charts;

import cnp.ew.displayer.*;
import java.awt.*;
import cnp.ew.lightweight.*;
import cnp.ew.util.*;

public class CpCategoryAxisDisplayer extends CpAbstractAxisDisplayer
{
    CpChartDataModel model;
    boolean drawTicks=true;
    static final int tickLength=4;
    static final int textTopMargin = 10;
    static final int labelGap = 10;
    CpGeneralStringDisplayer labelDisplayer, categoryDisplayer;

    public CpCategoryAxisDisplayer()
    {
        super();
        categoryDisplayer = new CpGeneralStringDisplayer();
        categoryDisplayer.setHorizontalAlignment(CpAlignable.ALIGN_CENTER);
        categoryDisplayer.setShouldWrap(true);

        labelDisplayer = new CpGeneralStringDisplayer();
        labelDisplayer.setHorizontalAlignment(CpAlignable.ALIGN_CENTER);
        labelDisplayer.setFont(new Font("Dialog", Font.BOLD, 12));
        labelDisplayer.setShouldWrap(true);
    }

    public void paintIn(CpLightweightComponent lc, Graphics g, int x, int y, int w, int h)
    {

        int totalItems = model.getCategoryCount();
        int itemWidth = w/totalItems;
        FontMetrics metrics = lc.getFontMetrics();
        int fontAscent = metrics.getAscent();


        g.setColor(Color.black);
        g.setFont(font);
        int startX = x;
        int maxCategoryHeight = 0;
        int preferredHeight;
        for (int i=0; i < totalItems; i++) {

            String string = model.getCategoryForIndex(i);
          //  System.out.println("setting text to [" + string + "]" + " itemWidth: " + itemWidth);
            categoryDisplayer.setText(string);
            preferredHeight = categoryDisplayer.wrappedPreferredHeightFor(lc, itemWidth);
           // System.out.println("preferredHeight: " + preferredHeight);
            categoryDisplayer.paintIn(lc, g, startX, y + textTopMargin, itemWidth, preferredHeight);
            maxCategoryHeight = Math.max(maxCategoryHeight, preferredHeight);
            /*
            g.drawString(
                string,
                startX + (itemWidth - metrics.stringWidth(string))/2,
                y + fontAscent + textTopMargin);
*/
            if (drawTicks) {
                g.drawLine(startX, y, startX, y + tickLength);
            }

            startX += itemWidth;
        }
        if (drawTicks) {
            startX = x + w - 1;
            g.drawLine(startX, y, startX, y + tickLength);
        }

        labelDisplayer.setText(model.getCategoriesLabel());
        int labelTop = maxCategoryHeight /* metrics.getHeight() */  + textTopMargin + labelGap;
        labelDisplayer.paintIn(lc, g, x, y + labelTop, w, h - labelTop);
    }



    public void setChartDataModel(CpChartDataModel newModel)
    {
        model = newModel;
    }

    public Dimension preferredSize(CpLightweightComponent lc)
    {
        int totalItems = model.getCategoryCount();
        int categoryHeight = 0;
        for (int i=0; i < totalItems; i++) {
            categoryDisplayer.setText(model.getCategoryForIndex(i));
            categoryHeight = Math.max(categoryHeight, categoryDisplayer.preferredSize(lc).height);
        }

        FontMetrics metrics = lc.getFontMetrics(font);
        // TBD: This should be taking the other dimension into account. this  requires some thought...
        labelDisplayer.setWrapWidth(1000);
        labelDisplayer.setText(model.getCategoriesLabel());
        return new Dimension(20, categoryHeight + metrics.getHeight() + textTopMargin + /* labelDisplayer.preferredSize(lc).height */ + labelGap);
    }
}

