package com.uvigo.mhp.ui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import org.dvb.ui.*;
import org.havi.ui.*;
import com.uvigo.mhp.looks.*;
import com.uvigo.tMaestro.SCOCreator.tool.MainFrame;

public class HPanel extends HContainer {

    public static final int HALIGN_LEFT = 0;
    public static final int HALIGN_CENTER = 1;
    public static final int HALIGN_RIGHT = 2;
    public static final int VALIGN_TOP = 0;
    public static final int VALIGN_CENTER = 4;
    public static final int VALIGN_BOTTOM = 8;
    public static final int TIME_ALWAYS = 0;
    public static final int TIME_OK = 1;
    public static final int TIME_WRONG = 2;
    public static final int TIME_DELAYED = 4;
    public static final int TIME_LIMITED = 6;
    public static final int COLOUR_NONE=0;
    public static final int COLOUR_RED= 1;
    public static final int COLOUR_GREEN=2;
    public static final int COLOUR_YELLOW=4;
    public static final int COLOUR_BLUE=6;
    public static final int IMAGE_DONT_SHOW = 0;
    public static final int IMAGE_SINGLE = 1;
    public static final int IMAGE_TILE = 2;
    public static final int IMAGE_STRETCH = 4;
    private int imgWidth,  imgHeight;
    private String label;
    private int horizontalAlignment,  verticalAlignment;
    private HPanelLook look;
    private Image backgroundImage;
    private String backgroundImageURL;
    private int delayedTimeMode;
    private int delayedTime;
    private int colour;
    private int backgroundImageMode;
    private MediaTracker trk;
    private boolean opaque;

    public HPanel() {
        super();
        setLayout(new GridLayout(1, 1));
        backgroundImageMode = IMAGE_TILE;
        backgroundImageURL = null;
        delayedTimeMode = TIME_ALWAYS;
        colour=COLOUR_NONE;
        delayedTime=0;
        imgWidth = imgHeight = 0;

        trk = new MediaTracker(this);
        try {
            setLook(new HPanelLook1());
            repaint();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Insets getInsets() {
        if (look == null) {
            return new Insets(0, 0, 0, 0);
        } else {
            return look.getInsets(this);
        }
    }

    public Dimension getPreferredSize() {
        Insets insets = getInsets();
        Dimension dim = super.getPreferredSize();
        int pw = dim.width + insets.left + insets.right;
        int ph = dim.height + insets.top + insets.bottom;
        return new Dimension(pw, ph);
    }

    public Dimension getMinimumSize() {
        if ((imgWidth <= 0) || (imgHeight <= 0)) {
            return getPreferredSize();
        }
        Insets insets = getInsets();
        return new Dimension(imgWidth + insets.left + insets.right, imgHeight + insets.top + insets.bottom);
    }

    public HPanelLook getLook() {
        return look;
    }

    public void setLook(HPanelLook l) throws HInvalidLookException {
        HPanelLook old = look;
        if (l != old) {
            this.look = l;
            repaint();
            firePropertyChange("look", old, l);
        }
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String str) {
        String old = label;
        if (old != str) {
            label = str;
            repaint();
            firePropertyChange("label", old, str);
        }
    }

    public String getBackgroundImageURL() {
        return backgroundImageURL;
    }

    public void setBackgroundImageURL(String s) {
        String old = backgroundImageURL;

        if ((old == null) || (!(old.equals(s)))) {
            if ((s == null) || (s.equals(""))) {
                backgroundImageURL = s;
                backgroundImage = null;
                repaint();
                firePropertyChange("backgroundImageURL", old, s);
            } else {
                try {
                    //M: Direccionamiento relativo a "user.dir"
                    String oBase = new String(MainFrame.oMainFrame.sDir + "/");
                    URL url = new URL("file:" + oBase + s);
                    backgroundImage = Toolkit.getDefaultToolkit().getImage(url);
                    trk.addImage(backgroundImage, 0);
                    trk.waitForAll();
                    imgWidth = backgroundImage.getWidth(null);
                    imgHeight = backgroundImage.getHeight(null);
                    repaint();
                    backgroundImageURL = s;
                    firePropertyChange("backgroundImageURL", old, s);
                } catch (Exception e) {
                }
            }
        }
    }

    public int getBackgroundImageMode() {
        return backgroundImageMode;
    }

    public void setBackgroundImageMode(int i) {
        int old = backgroundImageMode;
        if (i != old) {
            backgroundImageMode = i;
            repaint();
            firePropertyChange("backgroundImageMode", new Integer(old), new Integer(i));
        }
    }

    public int getDelayedTimeMode() {
        return delayedTimeMode;
    }

    public void setDelayedTimeMode(int i) {

        int old = delayedTimeMode;
        if (i != old) {
            delayedTimeMode = i;
            repaint();
            firePropertyChange("delayedTimeMode", new Integer(old), new Integer(i));

        }
    }

    public int getDelayedTime() {
        return delayedTime;
    }

    public void setDelayedTime(int m) {
        int old = delayedTime;
        if (m != old) {
            delayedTime = m;
            firePropertyChange("delayedTime", new Integer(old), new Integer(m));
        }
    }
    public int getHorizontalAlignment() {
        return horizontalAlignment;
    }

    public void setHorizontalAlignment(int i) {
        int old = horizontalAlignment;
        if (i != old) {
            horizontalAlignment = i;
            repaint();
            firePropertyChange("horizontalAlignment", new Integer(old), new Integer(i));
        }
    }

    public int getVerticalAlignment() {
        return verticalAlignment;
    }

    public void setVerticalAlignment(int i) {
        int old = verticalAlignment;
        if (i != old) {
            verticalAlignment = i;
            repaint();
            firePropertyChange("verticalAlignment", new Integer(old), new Integer(i));
        }
    }

    public boolean getOpaque() {
        return opaque;
    }

    public void setOpaque(boolean b) {
        boolean old = opaque;
        if (b != old) {
            opaque = b;
            repaint();
            firePropertyChange("opaque", new Boolean(old), new Boolean(b));
        }
    }

    public void paint(Graphics g) {
        if (look != null) {
            look.showLook(g, this);
        }
        paintBackground(g);
        super.paintComponents(g);
    }

    public void update(Graphics g) {
        paint(g);
    }

    private void paintBackground(Graphics g) {
        Dimension dim = getSize();
        Insets insets = getInsets();

        int width = dim.width - insets.left - insets.right;
        int height = dim.height - insets.top - insets.bottom;

        if (g instanceof DVBGraphics) {
            DVBGraphics g2 = (DVBGraphics) g;
            DVBAlphaComposite a = DVBAlphaComposite.getInstance(DVBAlphaComposite.SRC_OVER);
            try {
                g2.setDVBComposite(a);
            } catch (Exception e) {
                System.out.println("No se puede aplicar la regla de composici�n");
            }
        }

        if (opaque) {
            g.setColor(getBackground());
            g.fillRect(insets.left, insets.top, width, height);
        }

        if ((backgroundImage == null) || (imgWidth == 0) || (imgHeight == 0)) {
            return;
        }
        switch (backgroundImageMode) {
            case IMAGE_DONT_SHOW:
                return;
            case IMAGE_SINGLE:
                int hpos = insets.left;
                int vpos = insets.top;
                if (width > imgWidth) {
                    switch (getHorizontalAlignment()) {
                        case HALIGN_RIGHT:
                            hpos += width - imgWidth;
                            break;
                        case HALIGN_CENTER:
                            hpos += (width - imgWidth) / 2;
                            break;
                        default:
                            break;
                    }
                }

                if (height > imgHeight) {
                    switch (getVerticalAlignment()) {
                        case VALIGN_BOTTOM:
                            vpos += height - imgHeight;
                            break;
                        case VALIGN_CENTER:
                            vpos += (height - imgHeight) / 2;
                            break;
                        default:
                            break;
                    }
                }

                g.drawImage(backgroundImage, hpos, vpos, null);
                return;
            case IMAGE_TILE:
                // hay que crear un mosaico de im�genes
                BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics g2 = img.getGraphics();
                int y = 0;
                while (y < height) {
                    int x = 0;
                    while (x < width) {
                        g2.drawImage(backgroundImage, x, y, null);
                        x += imgWidth;
                    }
                    y += imgHeight;
                }
                g.drawImage(img, insets.left, insets.top, null);
                img.flush();
                return;
            default: // IMAGE_STRETCH: la imagen ocupa todo el �rea permitida

                g.drawImage(backgroundImage, insets.left, insets.top, width, height, null);
                return;
        }
    }
}