/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.tools.IOtools.iZformats;

import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.baseTools.Messagers;
import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.process.ProcessStarter;

/**
 *
 * @author LH
 */
public class IZTile
{    
    
    //iZtile FileStruktur:
    //tile.iZmeta
    //>>baseWidth(Kachelanzahl) //1 kachel=10cm
    //>>BaseHeight(Kachelanzahl) //1 kachel=10cm
    //>>IsBG
    //>>pxPerCm
    //thumb.png //50px pro einheit
    //tileContent.png
    
    
    
    
    
    
    private IZpackage backendFile;
    private Image Thumbnail; //vollständiges Image wird erst auf bedarf extrahiert
    
    private String filetmppath = "";
    private String impath = "...";
    private int xsize,ysize,res;
    boolean isBackground;
    private void ensureSysreadyness()     
    {
        impath = Preferences.userRoot().get("izTile.impath", "...");
        
        if (impath.equals("..."))
        {
            Messagers.SingleLineMsg("bitte öffnen sie die Imagemagick Convert.exe", "Okay");
            Preferences.userRoot().put("izTile.impath", FileTK.getFilePath(Tools.FileChooseDlg(new JFrame(), true, false, new String[]
            {
                "exe"
            })));
            impath = Preferences.userRoot().get("izTile.impath", "...");
        }
        
        
        ProcessStarter.setGlobalSearchPath(impath);
        
    }
    public IZTile(String TilePath)
    {
        ensureSysreadyness();
        filetmppath =  Tools.processEnvVarsinLine("%temp%\\iZTiler\\SessionCache")+"\\"+hashCode()+"\\";
        new File(filetmppath).mkdirs();
        backendFile = new IZpackage(TilePath);
        String izmeta = backendFile.getFileContent("tile.izmeta");
        String[] lines = izmeta.split("\\n");
        xsize = Integer.valueOf(lines[0]);
        ysize = Integer.valueOf(lines[1]);
        res = Integer.valueOf(lines[3]);
        isBackground = lines[2].equals(true);
        try
        {
            backendFile.extractFileToDisk("thumb.png",filetmppath );

            Thumbnail = ImageIO.read(new File(filetmppath+"thumb.png"));
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
    
    public IZTile(String TilePath,String baseImagePath,int baseWidth,int baseHeight,boolean isBackGround,int pxPerCM)
    {
        ensureSysreadyness();
        xsize=baseWidth;
        ysize=baseHeight;
        res=pxPerCM;
        isBackground = isBackGround;
        try
        {
            String iZmeta = baseWidth+"\n"+baseHeight+"\n"+isBackGround+"\n"+pxPerCM;
            String tmppath = Tools.processEnvVarsinLine("%temp%\\iZTiler\\tileTMP");
            File f = new File(tmppath);
            f.mkdirs();
            FileTK.writeFile(iZmeta, tmppath+"\\tile.izmeta");
            FileTK.kopierpaste(new File(baseImagePath), new File(tmppath+"\\tileContent.png"));
            IMOperation op = new IMOperation();
            op.resize(baseWidth*50, baseHeight*50);
            op.addImage(tmppath+"\\tileContent.png");
            op.addImage(tmppath+"\\thumb.png");
            filetmppath =  Tools.processEnvVarsinLine("%temp%\\iZTiler\\SessionCache")+"\\"+hashCode()+"\\";
            new File(filetmppath).mkdirs();
            //op.
            ConvertCmd c = new  ConvertCmd();
            c.run(op, new Object[]{});
            IZpackage.wrapArchive(TilePath, tmppath);
            backendFile = new IZpackage(TilePath);
            new File(tmppath+"\\tileContent.png").delete();
            new File(tmppath+"\\tile.izmeta").delete();
            new File(tmppath+"\\thumb.png").delete();
            f.delete();
            try
            {
                backendFile.extractFileToDisk("thumb.png",filetmppath );

                Thumbnail = ImageIO.read(new File(filetmppath+"thumb.png"));
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        catch (IOException | InterruptedException | IM4JavaException ex)
        {
            ex.printStackTrace();
        }
        
    }
    public Image getThumb()
    {
        return Thumbnail;
        
    }
    
    public String extractContent()
    {
       backendFile.extractFileToDisk("tileContent.png",filetmppath );
       return filetmppath+"tileContent.png";
    }
    
    public int getPxPerCm()
    {
        return res;
    }
    
    public int getHeightInTiles()
    {
        return ysize;
    }
    
    public int getWidthInTiles()
    {
        return xsize;
    }
    public boolean isBackground()
    {
        return isBackground;
    }
    public String getStoragePath()
    {
       return backendFile.getPath();
    }
    public String getThumbPath()
    {
        return filetmppath+"thumb.png";
    }
}
