package com.mgatelabs.swftools.exploit.gui;

import com.mgatelabs.swftools.support.swf.tags.TBlock;

import javax.swing.*;
import java.awt.*;

class BlockCellRenderer extends JLabel implements ListCellRenderer {
    static Icon setBackgroundIcon;
    static Icon shapeIcon;
    static Icon spriteIcon;

    static Icon placeIcon;
    static Icon removeIcon;

    static Icon textIcon;
    static Icon imageIcon;

    public BlockCellRenderer(Class theClass) {
        setOpaque(true);
        if (setBackgroundIcon == null) {
            setBackgroundIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_setBackground.png"));
            shapeIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_shape.png"));
            spriteIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_sprite.png"));

            placeIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_place.png"));
            removeIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_remove.png"));
            imageIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_image.png"));

            textIcon = new ImageIcon(theClass.getResource("/com/mgatelabs/swftools/exploit/gui/images/block_text.png"));
        }
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());

        TBlock aBlock = (TBlock) value;

        switch (aBlock.getId()) {
            case 9:
                setIcon(setBackgroundIcon);
                break;
            case 2:
            case 22:
            case 32:
            case 46:
            case 83:
            case 84:
                setIcon(shapeIcon);
                break;

            case 6:
            case 8:
            case 21:
            case 35: // Bits 3
            case 36: // Lossless 2
                setIcon(imageIcon);
                break;

            case 39:
                setIcon(spriteIcon);
                break;

            case 4:
            case 26:
            case 70:
                setIcon(placeIcon);
                break;

            case 5:
            case 28:
                setIcon(removeIcon);
                break;

            case 10:
            case 11:
            case 33:
            case 48:
            case 75:
                setIcon(textIcon);
                break;

            default:
                setIcon(null);
                break;
        }

        setBackground(isSelected ? Color.red : Color.white);
        setForeground(isSelected ? Color.white : Color.black);

        return this;
    }
}