package de.nplusc.izc.tools.UiToolz;


import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author LH
 */
public class ExtendedJPanel extends javax.swing.JPanel
{
    //UIFile.class.getResource("rsrc/
    public Graphics2D Overlay = null;
    public BufferedImage iOVL = null;
    private boolean ctrAreaEnab=false;
    private int addModeSel;
    public static final int MODE_GUI=0;
    public static final int MODE_CMD=1;
    private boolean overridelowerGFX = false;
    public boolean firstDraw = true;
    public TGUI gui;
    private UIFileAccess guifile;
    private boolean useUIF = false;
    public ExtendedJPanel()
    {
        guifile=null;
        gui=null;
        addModeSel=0;
    }
    public ExtendedJPanel(TGUI g)
    {
        guifile=null;
        gui = g;
        addModeSel=0;
    }
    public ExtendedJPanel(int addMode)
    {
        guifile=null;
        gui=null;
        addModeSel=addMode;
    }
    public ExtendedJPanel(UIFileAccess u)
    {
        guifile=u;
        gui=null;
        addModeSel=0;
        useUIF = true;
    }
    public ExtendedJPanel(TGUI g,UIFileAccess u)
    {
        guifile=u;
        gui = g;
        addModeSel=0;
        useUIF = true;
    }
    public ExtendedJPanel(int addMode,UIFileAccess u)
    {
        guifile=u;
        gui=null;
        addModeSel=addMode;
        useUIF = true;
    }
    /*
     * @SuppressWarnings("CallToThreadDumpStack")
    public void initOVL()
    {
        initOVL(null);
    }Ü*/
    /**
     * initializes the overlay to write on with bitmap-fonts
     */
    
    @SuppressWarnings("CallToThreadDumpStack")
    public void initOVL()
    {
        if(!useUIF)
        {
            try
            {
                iOVL = ImageIO.read(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-FullTrans.png"));
                if(ctrAreaEnab)
                {
                    iOVL = ImageIO.read(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-OVL-TxA.png"));
                }
                //System.err.println("???");

                //ctne = true;
            }
            catch (IOException ex)
            {
                Tools.DebugHelperPrint("LickMe", true, "Toolkit.enableCoreDebug");
                ex.printStackTrace();
            }
            if(addModeSel==0)
            {
                gui.initTexts(/*false*/);//boolean egal da teil von Hack//voided
            }    //static acces hack//VOID
        }
        else
        {
            if(guifile.zipBased)
            {
                try
                {
                    String s = guifile.ResourceURLs.get("FullTrans");
                    iOVL = ImageIO.read(guifile.loadFileFromZip(s));
                    if(ctrAreaEnab)
                    {
                        throw new UnsupportedOperationException("not implemented yet");
                        //iOVL = ImageIO.read(UIFile.class.getResource("rsrc/GUI-File-OVL-TxA.png"));
                    }
                    //System.err.println("???");

                    //ctne = true;
                }
                catch (IOException ex)
                {
                    Tools.DebugHelperPrint("LickMe", true, "Toolkit.enableCoreDebug");
                    ex.printStackTrace();
                }
                if(addModeSel==0)
                {
                    gui.initTexts(/*false*/);//boolean egal da teil von Hack//voided
                }    //static acces hack//VOID
            }
            else
            {
                try
                {
                    iOVL = ImageIO.read(new File(guifile.ResourceURLs.get("FullTrans")));
                    if(ctrAreaEnab)
                    {
                        throw new UnsupportedOperationException("not implemented yet");
                        //iOVL = ImageIO.read(UIFile.class.getResource("rsrc/GUI-File-OVL-TxA.png"));
                    }
                    //System.err.println("???");

                    //ctne = true;
                }
                catch (IOException ex)
                {
                    Tools.DebugHelperPrint("LickMe", true, "Toolkit.enableCoreDebug");
                    ex.printStackTrace();
                }
                if(addModeSel==0)
                {
                    gui.initTexts(/*false*/);//boolean egal da teil von Hack//voided
                }    //static acces hack//VOID
            }
        }
    }
    
    /**
     * Handles the glow of the button in the cmdGUI
     * @param state Glow or not
     */
    
    public void cmdButtonGlow(boolean state)
    {
        if(!useUIF)
        {
            if(addModeSel==2)
            {
                try
                {
                    iOVL = ImageIO.read(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-CMD-IP-BTN-Blue.png"));
                if(state)
                    {
                        iOVL = ImageIO.read(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-CMD-IP-BTN-Red.png"));
                    }
                    //System.err.println("???");//Fliegenklatsche
                overridelowerGFX=true;
                //ctne = true;
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        else
        {
            if(guifile.zipBased)
            {
                if(addModeSel==2)
                {
                    try
                    {
                        iOVL = ImageIO.read(guifile.loadFileFromZip(guifile.ResourceURLs.get("ButtonNormal")));
                    if(state)
                        {
                            iOVL = ImageIO.read(guifile.loadFileFromZip(guifile.ResourceURLs.get("ButtonHovered")));
                        }
                        //System.err.println("???");//Fliegenklatsche
                    overridelowerGFX=true;
                    //ctne = true;
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                if(addModeSel==2)
                {
                    try
                    {
                        iOVL = ImageIO.read(new File(guifile.ResourceURLs.get("ButtonDefault")));
                    if(state)
                        {
                            iOVL = ImageIO.read(new File(guifile.ResourceURLs.get("ButtonHovered")));
                        }
                        //System.err.println("???");//Fliegenklatsche
                    overridelowerGFX=true;
                    //ctne = true;
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    public void highlightButton(int bid)
    {
        //TODO make different button variants for ExtendedDesktopMenue
    }
    
    /**
     * Enables/Disables the field in the middle of the MEnue
     * @param isEnabled <<
     */
    public void setCenterArea(boolean isEnabled)
    {
        ctrAreaEnab=isEnabled;
    }

    /**
     * Draws a bitmap-font-letter
     * @param font Which font @see FontEngine.FONT_***
     * @param letter Number of Letter
     * @param posX Where
     * @param posY Where
     */
    @SuppressWarnings("static-access")
    public void setLetter(int[] font,int letter,int posX,int posY)
    {
        boolean ctne = false;
        BufferedImage bI = null;
        
        if(iOVL!=null)
        {
            Overlay = (Graphics2D) iOVL.getGraphics();
            writeLetter(Overlay,font,letter,posX,posY);
           // iOVL = bI;
        }
    }
    
    @Override
    @SuppressWarnings("CallToThreadDumpStack")
    protected synchronized void paintComponent(Graphics g)
    {
        try
        {
        // GUI.class.getDeclaredMethods()[0].invoke(g, args)//war nen Test//notUSed
            ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
            //if(!(addModeSel==3))
            //{ 
            super.paintComponent(g);
            //}
            Image imgi = null;
            Graphics2D g2d = (Graphics2D) g;
            if(!useUIF)
            {
                imgi = new ImageIcon(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-mainMEnue.png")).getImage();//GUI-MasterFile.png").getImage();  
                if(addModeSel==1)
                {
                    imgi = new ImageIcon(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-CMD-IP-Main.png")).getImage();
                }
                if(addModeSel==2)
                {
                    imgi = new ImageIcon(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-File-CMD-IP-BTN-Blue.png")).getImage();
                }
                if(addModeSel==3)
                {
                    imgi = new ImageIcon(de.nplusc.izc.tools.UIToolz.rsrc.UIFile.class.getResource("rsrc/GUI-FIle_TabWritten.png")).getImage();//GUI-File-FullTrans.png").getImage();
                }
            }
            else
            {

                if(guifile.zipBased)
                {
                    if(addModeSel==1)
                    {
                        String s = guifile.ResourceURLs.get("MainWdw");
                        imgi = ImageIO.read(guifile.loadFileFromZip(s));
                    }
                    if(addModeSel==2)
                    {
                        imgi = ImageIO.read(guifile.loadFileFromZip(guifile.ResourceURLs.get("ButtonNormal")));
                    }
                    if(addModeSel==3)
                    {
                        imgi = ImageIO.read(guifile.loadFileFromZip(guifile.ResourceURLs.get("tabbar")));//GUI-File-FullTrans.png").getImage();
                    }
                }
                else
                {
                    if(addModeSel==1)
                    {
                        imgi = ImageIO.read(new File(guifile.ResourceURLs.get("MainWdw")));
                    }
                    if(addModeSel==2)
                    {
                        imgi = ImageIO.read(new File(guifile.ResourceURLs.get("ButtonNormal")));
                    }
                    if(addModeSel==3)
                    {
                        imgi = ImageIO.read(new File(guifile.ResourceURLs.get("tabbar")));
                    }
                }
            }
            if(firstDraw)
            {

                //g2d.setPaint(p);
                //g2d.fillRect(0, 0, getWidth(), getHeight());
                //imgi.
                TGUI.Background = imgi;

                g2d.drawImage(imgi, 0, 0, null);
                if(addModeSel==1)
                {
                    //((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
                    //g2d.drawImage(imgi, 0, 0, null);
                    //g2d.drawImage(imgi, 0, 0, null);
                }

                firstDraw=false;
            }
            else
            {
                //System.err.println()
                if(!overridelowerGFX)
                {
                g2d.drawImage(imgi, 0, 0, null);
                ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                }
                g2d.drawImage(iOVL, 0, 0, null);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    /**
     * paints the letter on the OVL
     * @param g Graphics-Obj
     * @param font which Font
     * @param letter which letter
     * @param posX where-X
     * @param posY  where-y
     */
    
    private void writeLetter(Graphics g,int[] font,int letter,int posX,int posY)
    {
            Image i = FontEngine.getLetter(font,letter);
            g.drawImage(i,posX,posY,null);
    }
}
