package de.nplusc.izc.tools.UiToolz;


import de.nplusc.izc.tools.IOtools.ZipFileHandler;
import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Set;
import org.yaml.snakeyaml.Yaml;

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

/**
 *
 * @author LH
 */
public class UIFileAccess
{
    public final Rectangle[] tabs = new Rectangle[4];//*//*/
    public final int[] tabBarPos = new int[4];//*//*
    public final Polygon ButtonShape;//*
    public final Font term;//*//*
    public final Color FontColor;//*//* //Color(255, 0, 0, 128);= new Color(170,220,240,200)
    public final int lineCount;//*
    public final int[] windowSize = new int[2];;//*//*
    public final int[] buttonPanel = new int[4];//*//*
    public final int[] ipLineArea = new int[4];//*//*
    public final int[] ScrollBar = new int[4];//*//*
    public final int[] lineCfg = new int[4];//*//*
    public final Point windowBasePoint; //*//*
    public final HashMap<String,String> ResourceURLs = new HashMap<>();//*
    public final boolean zipBased;
    private static Yaml parser = new Yaml();
    private ZipFileHandler zfh = null;
    //private static UIFileAccess defaultUIfile = new UIFileAccess(UIFile.class);
    
    
    
    
    public static void createFefaultUIFile(String opPath)
    {
        String s = "";
                //parser.
        String[] s2=FileTK.fileload("C:\\users\\LH\\Desktop\\jp\\ExampleUIFile.yml");
        for (int i=0;i<s2.length-1;i++)
        {
            s+=s2[i]+"\n";
        }
        s= s.trim();
        HashMap<String,HashMap<String,String>> t = new HashMap<>();
        //t.put(s, null);
        t= parser.loadAs(s, t.getClass());
        //System.err.println(parser.dump(t));
        Tools.DebugHelperPrint(parser.dump(t), true, "Toolkit.enableCoreDebug");
        // <editor-fold defaultstate="collapsed" desc="altlast">
        /*"FontColor: \n"+
         
        f 
        
        "  R: I|170\n"+
        "  G: I|220\n"+
        "  B: I|240\n"+
        "  A: I|200\n"+
        "Font:\n"+
        "  FontName: S|Lucida Console\n"+
        "  Bold: B|Yes\n"+
        "  Size: I|15\n"+
        "tabBar:\n"+
        "  TabWidth: I|200\n"+
        "  Gfx: U|rsrc/GUI-FIle_TabWritten.png\n"+
        "  BarBasePoint: 'P|0|0'\n"+
        "  BarWidth: I|1000\n"+
        "  BarHeight: I|20\n"+
        "  TabHeight: I|20\n"+
        "  TabBasePoint: P|50|0\n"+
        "MainCfg:\n"+
        "  LineWidth: I|930\n"+
        "  ScrollBarHeight: Remark|Determined by LineCount*LineSize\n"+
        "  ScrollBarWidth: I|15\n"+
        "  ScrollBarBasePoint: P|935|25\n"+
        "  ICO: U|rsrc/icon.png\n"+
        "  InputLineHeight: I|17\n"+
        "  LinesBasePoint: P|5|25\n"+
        "  InputLineBasePoint: P|54|412\n"+
        "  GFX: U|rsrc/GUI-File-CMD-IP-Main.png\n"+
        "  InputLineWidth: I|860 \n"+
        "  LineHeight: I|14\"+
        "  WindowBasePoint: P|200|120"+
        "okButton:"+
        "  BP6: P|209|7"+
        "  BP7: P|229|27"+
        "  BP8: P|168|47"+
        "  ButtonGlowGFX: U|rsrc/GUI-File-CMD-IP-BTN-Red.png"+
        "  BP9: P|12|47"+
        "  BP10: P|6|36"+
        "  BP1: P|27|16"+
        "  BP2: P|73|16"+
        "  BP3: P|85|1"+
        "  BP4: P|152|1"+
        "  ButtonDfltGFX: U|rsrc/GUI-File-CMD-IP-BTN-Blue.png"+
        "  BP5: P|155|8"+
        "  ButtonAreaHeight: I|48"+
        "  ButtonAreaWidth: I|233"+
        "\n  ButtonAreaPos: P|630|432";*/

        /*
        HashMap<String,HashMap<String,String>> uiOP = new HashMap<>();
        HashMap<String,String> Keys = new HashMap<>();
            Keys.put("R","I|170");
            Keys.put("G","I|220");
            Keys.put("B","I|240");
            Keys.put("A","I|200");
        uiOP.put("FontColor",Keys);
        Keys = new HashMap<>();
            Keys.put("FontName","S|Lucida Console");//FióntName
            Keys.put("Bold","B|Yes");
            Keys.put("Size","I|15");
        uiOP.put("Font",Keys);
        Keys = new HashMap<>();
            Keys.put("TabBasePoint","P|50|0");
            Keys.put("TabWidth","I|200");
            Keys.put("TabHeight","I|20");
            Keys.put("BarBasePoint","P|0|0");
            Keys.put("BarWidth","I|1000");
            Keys.put("BarHeight","I|20");
            Keys.put("Gfx","U|rsrc/GUI-FIle_TabWritten.png");
        uiOP.put("tabBar",Keys);
        Keys = new HashMap<>();
            Keys.put("ButtonAreaPos","P|630|432");
            Keys.put("ButtonAreaWidth","I|233");
            Keys.put("ButtonAreaHeight","I|48");
            Keys.put("BP1","P|27|16");
            Keys.put("BP2","P|73|16");
            Keys.put("BP3","P|85|1");
            Keys.put("BP4","P|152|1");
            Keys.put("BP5","P|155|8");
            Keys.put("BP6","P|209|7");
            Keys.put("BP7","P|229|27");
            Keys.put("BP8","P|168|47");
            Keys.put("BP9","P|12|47");
            Keys.put("BP10","P|6|36");
            Keys.put("ButtonGlowGFX","U|rsrc/GUI-File-CMD-IP-BTN-Red.png");
            Keys.put("ButtonDfltGFX","U|rsrc/GUI-File-CMD-IP-BTN-Blue.png");
        uiOP.put("okButton",Keys);
        Keys = new HashMap<>();
            Keys.put("LineHeight","I|14");
            Keys.put("WindowBasePoint","P|200|120");
            Keys.put("LinesBasePoint","P|5|25");
            Keys.put("LineWidth","I|930");
            Keys.put("GFX","U|rsrc/GUI-File-CMD-IP-Main.png");
            Keys.put("ICO","U|rsrc/icon.png");
            Keys.put("ScrollBarBasePoint","P|935|25");
            Keys.put("ScrollBarWidth","I|15");
            Keys.put("ScrollBarHeight","Remark|Determined by LineCount*LineSize");
            Keys.put("InputLineBasePoint","P|54|412");
            Keys.put("InputLineWidth","I|860");
            Keys.put("InputLineHeight","I|17");
        uiOP.put("MainCfg",Keys);*/
        //Iterator<Entry<String, HashMap<String, String>>> i = uiOP.entrySet().iterator();
        //System.err.println(parser.dump(uiOP));
        
            
            //ipline.setBounds(54,412,860,17);
            //js.setBounds(935,25,15,25*14);
            //linel.setBounds(5, 25+i*14, 930, 14);
            //public static final Polygon buttonPos = new Polygon(new int[]
                    //27,73,85,152,155,209,229,168,12,6},new int[]
                    //{16,16,1 ,1  ,8  ,7  ,27 ,47 ,47,36},10);
           
            //(630,432,233,48))
            //</editor-fold>
            
 
    }
    
    private static String ssv(String ip)
    {
        String ret = ip.split("\\|")[1];
        //System.err.println(ret);
        Tools.DebugHelperPrint(ret, true, "Toolkit.enableCoreDebug");
        return ret;
    }
    private static String[] stv(String ip)
    {
        String[]ret=new String[]{ip.split("\\|")[1],ip.split("\\|")[2]};
        return ret;
    }
    
    /**
     * loads uifile from specified file
     * @param base file with UIFile content
     */
    public UIFileAccess(File base)
    {
        zfh = new ZipFileHandler(base.getAbsolutePath());
        zipBased=true;
        String s = "";
        //parser.
        byte[] uif = zfh.getEntry("UIFile.yml");
        for (byte b : uif)
        {
            s+=(char)b;
        }

        HashMap<String,HashMap<String,String>> t = new HashMap<>();
        //t.put(s, null);
        t= (HashMap<String,HashMap<String,String>>)parser.load(s);
        System.err.println("NOP");
        Tools.DebugHelperPrint("NOP", true, "Toolkit.enableCoreDebug");
        HashMap<String,String> fontcfg = t.get("Font");
            int bold = Font.PLAIN;
            if("Yes".equals(ssv(fontcfg.get("Bold"))))
            {
                bold=Font.BOLD;
            }
            int size = Integer.valueOf(ssv(fontcfg.get("Size")));
            term = new Font(ssv(fontcfg.get("FontName")),bold,size);
        
        HashMap<String,String> fontclr = t.get("FontColor");
            int r,g,b,a;
            r=Integer.valueOf(ssv(fontclr.get("R")));
            g=Integer.valueOf(ssv(fontclr.get("G")));
            b=Integer.valueOf(ssv(fontclr.get("B")));
            a=Integer.valueOf(ssv(fontclr.get("A")));
            FontColor=new Color(r,g,b,a);
        HashMap<String,String> tabcfg = t.get("tabBar");
        
            ResourceURLs.put("tabbar",ssv(tabcfg.get("Gfx")));
            int barBaseX =Integer.valueOf(stv(tabcfg.get("BarBasePoint"))[0]);
            int barBaseY =Integer.valueOf(stv(tabcfg.get("BarBasePoint"))[1]);
            int barWidth =Integer.valueOf(ssv(tabcfg.get("BarWidth")));
            int barHeight=Integer.valueOf(ssv(tabcfg.get("BarHeight")));
            int tabBaseX =Integer.valueOf(stv(tabcfg.get("TabBasePoint"))[0]);
            int tabBaseY =Integer.valueOf(stv(tabcfg.get("TabBasePoint"))[1]);
            int tabWidth =Integer.valueOf(ssv(tabcfg.get("TabWidth")));
            int tabHeight=Integer.valueOf(ssv(tabcfg.get("TabHeight")));
            tabBarPos[0] = barBaseX;
            tabBarPos[1] = barBaseY;
            tabBarPos[2] = barWidth;
            tabBarPos[3] = barHeight;

            tabs[0] = new Rectangle(tabBaseX,tabBaseY,tabWidth,tabHeight);
            tabs[1] = new Rectangle(tabBaseX+tabWidth,tabBaseY,tabWidth,tabHeight);
            tabs[2] = new Rectangle(tabBaseX+tabWidth*2,tabBaseY,tabWidth,tabHeight);
            tabs[3] = new Rectangle(tabBaseX+tabWidth*3,tabBaseY,tabWidth,tabHeight);
            
        HashMap<String,String> maincfg = t.get("MainCfg");
            int winBaseX =Integer.valueOf(stv(maincfg.get("WindowBasePoint"))[0]);
            int winBaseY =Integer.valueOf(stv(maincfg.get("WindowBasePoint"))[1]);
            windowBasePoint = new Point(winBaseX,winBaseY);
            int lineBaseX  =Integer.valueOf(stv(maincfg.get("LinesBasePoint"))[0]);
            int lineBaseY  =Integer.valueOf(stv(maincfg.get("LinesBasePoint"))[1]);
            int lineWidtht = Integer.valueOf(ssv(maincfg.get("LineWidth")));
            int lineHeight = Integer.valueOf(ssv(maincfg.get("LineHeight")));
            lineCfg[0] = lineBaseX;
            lineCfg[1] = lineBaseY;
            lineCfg[2] = lineWidtht;
            lineCfg[3] = lineHeight;
            lineCount = Integer.valueOf(ssv(maincfg.get("LineCount")));
            int sbBaseX =Integer.valueOf(stv(maincfg.get("ScrollBarBasePoint"))[0]);
            int sbBaseY =Integer.valueOf(stv(maincfg.get("ScrollBarBasePoint"))[1]);
            int sbWidth = Integer.valueOf(ssv(maincfg.get("ScrollBarWidth")));
            int sbHeight = lineCount*lineCfg[3];
            ScrollBar[0] = sbBaseX;
            ScrollBar[1] = sbBaseY;
            ScrollBar[2] = sbWidth;
            ScrollBar[3] = sbHeight;
            int ipBaseX =Integer.valueOf(stv(maincfg.get("InputLineBasePoint"))[0]);
            int ipBaseY =Integer.valueOf(stv(maincfg.get("InputLineBasePoint"))[1]);
            int ipWidth = Integer.valueOf(ssv(maincfg.get("InputLineWidth")));
            int ipHeight = Integer.valueOf(ssv(maincfg.get("InputLineHeight")));
            ipLineArea[0] = ipBaseX;
            ipLineArea[1] = ipBaseY;
            ipLineArea[2] = ipWidth;
            ipLineArea[3] = ipHeight;
            ResourceURLs.put("icon",ssv(maincfg.get("ICO")));
            ResourceURLs.put("MainWdw",ssv(maincfg.get("GFX")));
            ResourceURLs.put("FullTrans",ssv(maincfg.get("FullTrans")));
            windowSize[0] = Integer.valueOf(ssv(maincfg.get("WindowSizeX")));
            windowSize[1] = Integer.valueOf(ssv(maincfg.get("WindowSizeY")));
            
        HashMap<String,String> okButt = t.get("okButton");
            ResourceURLs.put("ButtonHovered",ssv(okButt.get("ButtonGlowGFX")));
            ResourceURLs.put("ButtonNormal",ssv(okButt.get("ButtonDfltGFX")));
            int bpBaseX =Integer.valueOf(stv(okButt.get("ButtonAreaBasePoint"))[0]);
            int bpBaseY =Integer.valueOf(stv(okButt.get("ButtonAreaBasePoint"))[1]);
            int bpWidth = Integer.valueOf(ssv(okButt.get("ButtonAreaWidth")));
            int bpHeight = Integer.valueOf(ssv(okButt.get("ButtonAreaHeight")));
            buttonPanel[0] = bpBaseX;
            buttonPanel[1] = bpBaseY;
            buttonPanel[2] = bpWidth;
            buttonPanel[3] = bpHeight;
            okButt.remove("ButtonGlowGFX");
            okButt.remove("ButtonDfltGFX");
            okButt.remove("ButtonAreaBasePoint");
            okButt.remove("ButtonAreaWidth");
            okButt.remove("ButtonAreaHeight");
            Set ks = okButt.keySet();
            int[] kx = new int[ks.size()];
            int[] ky = new int[ks.size()];
            int ps = ks.size();
            String cl = "";
            for (int i = 1; i <= ks.size(); i++)
            {
                for (int j = 0; j < ks.size(); j++)
                {
                    String ct ="BP"+i; 
                    String o = (String)ks.toArray()[j];
                    if(ct.equals(o))//niemals leere ()S drinne lassen sonst ERR0R
                    {
                        cl=okButt.get(o);
                    }
                    
                }
                //System.err.println("i|"+i+"|o|"+cl);
                kx[i-1] = Integer.valueOf(stv(cl)[0]);
                ky[i-1] = Integer.valueOf(stv(cl)[1]);
            }
            ButtonShape=new Polygon(kx,ky,ps); 
    }
    
    /**
     * loads the uifile in relation to specified class
     * @param base class which is base
     */
    
    

    public InputStream loadFileFromZip(String resPath)
    {
        return new ByteArrayInputStream(zfh.getEntry(resPath));
    }
    public UIFileAccess(Class base)
    {
        zipBased=false;
        String s = "";
        //parser.
        String[] s2=FileTK.fileload(base.getResource("UIFile.yml").getPath());
        for (int i=0;i<s2.length-1;i++)
        {
            s+=s2[i]+"\n";
        }
        HashMap<String,HashMap<String,String>> t = new HashMap<>();
        //t.put(s, null);
        t= (HashMap<String,HashMap<String,String>>)parser.load(s);
        System.err.println("NOP");
        Tools.DebugHelperPrint("NOP", true, "Toolkit.enableCoreDebug");
        HashMap<String,String> fontcfg = t.get("Font");
            int bold = Font.PLAIN;
            if("Yes".equals(ssv(fontcfg.get("Bold"))))
            {
                bold=Font.BOLD;
            }
            int size = Integer.valueOf(ssv(fontcfg.get("Size")));
            term = new Font(ssv(fontcfg.get("FontName")),bold,size);
        
        HashMap<String,String> fontclr = t.get("FontColor");
            int r,g,b,a;
            r=Integer.valueOf(ssv(fontclr.get("R")));
            g=Integer.valueOf(ssv(fontclr.get("G")));
            b=Integer.valueOf(ssv(fontclr.get("B")));
            a=Integer.valueOf(ssv(fontclr.get("A")));
            FontColor=new Color(r,g,b,a);
        HashMap<String,String> tabcfg = t.get("tabBar");
        
            ResourceURLs.put("tabbar",base.getResource(ssv(tabcfg.get("Gfx"))).getPath());
            int barBaseX =Integer.valueOf(stv(tabcfg.get("BarBasePoint"))[0]);
            int barBaseY =Integer.valueOf(stv(tabcfg.get("BarBasePoint"))[1]);
            int barWidth =Integer.valueOf(ssv(tabcfg.get("BarWidth")));
            int barHeight=Integer.valueOf(ssv(tabcfg.get("BarHeight")));
            int tabBaseX =Integer.valueOf(stv(tabcfg.get("TabBasePoint"))[0]);
            int tabBaseY =Integer.valueOf(stv(tabcfg.get("TabBasePoint"))[1]);
            int tabWidth =Integer.valueOf(ssv(tabcfg.get("TabWidth")));
            int tabHeight=Integer.valueOf(ssv(tabcfg.get("TabHeight")));
            tabBarPos[0] = barBaseX;
            tabBarPos[1] = barBaseY;
            tabBarPos[2] = barWidth;
            tabBarPos[3] = barHeight;

            tabs[0] = new Rectangle(tabBaseX,tabBaseY,tabWidth,tabHeight);
            tabs[1] = new Rectangle(tabBaseX+tabWidth,tabBaseY,tabWidth,tabHeight);
            tabs[2] = new Rectangle(tabBaseX+tabWidth*2,tabBaseY,tabWidth,tabHeight);
            tabs[3] = new Rectangle(tabBaseX+tabWidth*3,tabBaseY,tabWidth,tabHeight);
            
        HashMap<String,String> maincfg = t.get("MainCfg");
            int winBaseX =Integer.valueOf(stv(maincfg.get("WindowBasePoint"))[0]);
            int winBaseY =Integer.valueOf(stv(maincfg.get("WindowBasePoint"))[1]);
            windowBasePoint = new Point(winBaseX,winBaseY);
            int lineBaseX  =Integer.valueOf(stv(maincfg.get("LinesBasePoint"))[0]);
            int lineBaseY  =Integer.valueOf(stv(maincfg.get("LinesBasePoint"))[1]);
            int lineWidtht = Integer.valueOf(ssv(maincfg.get("LineWidth")));
            int lineHeight = Integer.valueOf(ssv(maincfg.get("LineHeight")));
            lineCfg[0] = lineBaseX;
            lineCfg[1] = lineBaseY;
            lineCfg[2] = lineWidtht;
            lineCfg[3] = lineHeight;
            lineCount = Integer.valueOf(ssv(maincfg.get("LineCount")));
            int sbBaseX =Integer.valueOf(stv(maincfg.get("ScrollBarBasePoint"))[0]);
            int sbBaseY =Integer.valueOf(stv(maincfg.get("ScrollBarBasePoint"))[1]);
            int sbWidth = Integer.valueOf(ssv(maincfg.get("ScrollBarWidth")));
            int sbHeight = lineCount*lineCfg[3];
            ScrollBar[0] = sbBaseX;
            ScrollBar[1] = sbBaseY;
            ScrollBar[2] = sbWidth;
            ScrollBar[3] = sbHeight;
            int ipBaseX =Integer.valueOf(stv(maincfg.get("InputLineBasePoint"))[0]);
            int ipBaseY =Integer.valueOf(stv(maincfg.get("InputLineBasePoint"))[1]);
            int ipWidth = Integer.valueOf(ssv(maincfg.get("InputLineWidth")));
            int ipHeight = Integer.valueOf(ssv(maincfg.get("InputLineHeight")));
            ipLineArea[0] = ipBaseX;
            ipLineArea[1] = ipBaseY;
            ipLineArea[2] = ipWidth;
            ipLineArea[3] = ipHeight;
            ResourceURLs.put("icon",base.getResource(ssv(maincfg.get("ICO"))).getPath());
            ResourceURLs.put("MainWdw",base.getResource(ssv(maincfg.get("GFX"))).getPath());
            ResourceURLs.put("FullTrans",base.getResource(ssv(maincfg.get("FullTrans"))).getPath());
            windowSize[0] = Integer.valueOf(ssv(maincfg.get("WindowSizeX")));
            windowSize[1] = Integer.valueOf(ssv(maincfg.get("WindowSizeY")));
            
        HashMap<String,String> okButt = t.get("okButton");
            ResourceURLs.put("ButtonHovered",base.getResource(ssv(okButt.get("ButtonGlowGFX"))).getPath());
            ResourceURLs.put("ButtonNormal",base.getResource(ssv(okButt.get("ButtonDfltGFX"))).getPath());
            int bpBaseX =Integer.valueOf(stv(okButt.get("ButtonAreaBasePoint"))[0]);
            int bpBaseY =Integer.valueOf(stv(okButt.get("ButtonAreaBasePoint"))[1]);
            int bpWidth = Integer.valueOf(ssv(okButt.get("ButtonAreaWidth")));
            int bpHeight = Integer.valueOf(ssv(okButt.get("ButtonAreaHeight")));
            buttonPanel[0] = bpBaseX;
            buttonPanel[1] = bpBaseY;
            buttonPanel[2] = bpWidth;
            buttonPanel[3] = bpHeight;
            okButt.remove("ButtonGlowGFX");
            okButt.remove("ButtonDfltGFX");
            okButt.remove("ButtonAreaBasePoint");
            okButt.remove("ButtonAreaWidth");
            okButt.remove("ButtonAreaHeight");
            Set ks = okButt.keySet();
            int[] kx = new int[ks.size()];
            int[] ky = new int[ks.size()];
            int ps = ks.size();
            String cl = "";
            for (int i = 1; i <= ks.size(); i++)
            {
                for (int j = 0; j < ks.size(); j++)
                {
                    String ct ="BP"+i; 
                    String o = (String)ks.toArray()[j];
                    if(ct.equals(o))//niemals leere ()S drinne lassen sonst ERR0R
                    {
                        cl=okButt.get(o);
                    }
                    
                }
                System.err.println("i|"+i+"|o|"+cl);
                kx[i-1] = Integer.valueOf(stv(cl)[0]);
                ky[i-1] = Integer.valueOf(stv(cl)[1]);
            }
            ButtonShape=new Polygon(kx,ky,ps); 
        
    }
    /*
    public static void main(String args[])
    {
        //createFefaultUIFile(null);
        UIFileAccess x = new UIFileAccess(UIFile.class);
        System.err.println("NOP");
    }*/
    
}
