package cnp.ew.dialog;

import java.awt.*;

import cnp.ew.lightweight.*;
import cnp.ew.button.*;
import cnp.ew.text.*;
import cnp.ew.util.*;
import cnp.ew.layout.*;
import cnp.ew.misc.*;
import java.util.*;

public class CpMessageDialog extends CpLcDialog
{
    public static final int STYLE_OK = 0;
    public static final int STYLE_OKCANCEL = 1;
    public static final int STYLE_YESNO = 2;
    public static final int STYLE_YESNOCANCEL = 3;

    static int infoId;
    static int questionId;

    static {
         infoId = CpToolkit.registerImageName("info.gif");
         questionId = CpToolkit.registerImageName("question.gif");
    }

    CpReadOnlyTextAreaLc label;

    public CpMessageDialog(Frame parentFrame, String title, String text)
    {
        this(parentFrame, title, text, STYLE_OK);
    }

    public CpMessageDialog(Frame parentFrame, String title, String text, int style)
    {
        // TBD: Need to deal with modal windows.
	    super(parentFrame, title, true);

        Vector buttonTitles = new Vector();
        String defaultTitle, cancelTitle;

        switch (style) {
        case STYLE_OK:
            buttonTitles.addElement("OK");
            defaultTitle = "OK";
            cancelTitle = "OK";
            break;
        case STYLE_OKCANCEL:
            buttonTitles.addElement("OK");
            buttonTitles.addElement("Cancel");
            defaultTitle = "OK";
            cancelTitle = "Cancel";
            break;
        case STYLE_YESNO:
            buttonTitles.addElement("Yes");
            buttonTitles.addElement("No");

            defaultTitle = "Yes";
            cancelTitle = "No";
            break;
        case STYLE_YESNOCANCEL:
            buttonTitles.addElement("Yes");
            buttonTitles.addElement("No");
            buttonTitles.addElement("Cancel");

            defaultTitle = "Yes";
            cancelTitle = "Cancel";
            break;
        default:
            throw new IllegalArgumentException("Invalid messagebox type");
        }
        initialize(text, buttonTitles, defaultTitle, cancelTitle);
    }

    public CpMessageDialog(Frame parentFrame, String title, String text, Vector buttonTitles, String defaultTitle, String cancelTitle)
    {
	    super(parentFrame, title, true);
        initialize(text, buttonTitles, defaultTitle, cancelTitle);
    }

    public void initialize(String text, Vector buttonTitles, String defaultTitle, String cancelTitle)
    {

        CpAttachments attachments;

        setBackground(Color.lightGray);

        CpPanelLc rootLc = new CpPanelLc();
        rootLc.setBorderMargin(new Insets(15, 15, 10, 15));
        setLc(rootLc);
        CpAttachmentsLayout layout = new CpAttachmentsLayout();
        rootLc.setLayout(layout);

        CpAttachmentsLayout innerLayout = new CpAttachmentsLayout();
        CpPanelLc innerPanel = new CpPanelLc();
        innerPanel.setLayout(innerLayout);
        attachments = new CpAttachments(0, 0, -1, -1);
        layout.setAttachments(innerPanel, attachments);
        rootLc.add(innerPanel);

        CpImageLc imageLc = new CpImageLc(CpToolkit.getImage(buttonTitles.size() > 1 ? questionId : infoId));
        attachments = new CpAttachments();
        attachments.setVerticalCentering();
        attachments.setLeftAttachment(0);
        innerLayout.setAttachments(imageLc, attachments);
        innerPanel.add(imageLc);

        label = new CpReadOnlyTextAreaLc(text);
        label.setFont(CpFonts.dialogPlainTen());
        label.setShouldWrap(true);
        attachments = new CpAttachments();
        attachments.setLeftAttachment(15, imageLc);
        attachments.setVerticalCentering(imageLc);
        innerLayout.setAttachments(label, attachments);
        innerPanel.add(label);

        CpLabeledImageButtonLc previousButton = null;
        for (int i = buttonTitles.size() - 1; i >= 0; i--) {
            String buttonTitle = (String)buttonTitles.elementAt(i);
            CpLabeledImageButtonLc button = new CpLabeledImageButtonLc(buttonTitle);
            button.setIsTabStop(true);
            if (buttonTitle.equals(defaultTitle)) {
                button.setIsDefaultButton(true);
                button.setBorderMargin(18, 1);
            } else {
                button.setBorderMargin(18, 2);
            }
            if (buttonTitle.equals(cancelTitle)) {
                button.setIsCancelButton(true);
            }
            button.addObserver(this);
            attachments = new CpAttachments();
            attachments.setTopAttachment(20, innerPanel);
            if (previousButton == null) {
                attachments.setRightAttachment(CpAttachments.ATTACH_SIBLING_OPPOSITE, 0, innerPanel);
            } else {
                attachments.setRightAttachment(8, previousButton);
            }
            layout.setAttachments(button, attachments);
            rootLc.add(button);
            previousButton = button;
        }
    }


    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == CpEvent.BUTTON_CLICKED) {
            notifyObservers(CpEvent.OBJECT_CHANGED, ((CpLabeledImageButtonLc)o).getLabel());
            closeWindow();
        }
    }

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		    case Event.WINDOW_DESTROY: {
		        closeWindow();
            }
		}
		return super.handleEvent(evt);
	}
}


