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

import de.nplusc.izc.tools.IOtools.iZformats.IZTile;
import de.nplusc.izc.tools.UiToolz.UiTools;
import de.nplusc.izc.tools.baseTools.Tools;
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 javax.imageio.ImageIO;

/**
 *
 * @author LH
 */
public class TileImporter extends javax.swing.JFrame
{
    TileRegistry tr;
    /**
     * Creates new form TileImporter
     */
    public TileImporter(TileRegistry r)
    {
        tr=r;
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        btnSelectImage = new javax.swing.JButton();
        txfWidth = new javax.swing.JTextField();
        txfHEight = new javax.swing.JTextField();
        cxBG = new javax.swing.JCheckBox();
        viewPort = new javax.swing.JPanel();
        btnRegTile = new javax.swing.JButton();

        setTitle("TileLoader");

        btnSelectImage.setText("Bild laden");
        btnSelectImage.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnSelectImageActionPerformed(evt);
            }
        });

        txfWidth.setEditable(false);
        txfWidth.setText("Breite in kacheln");
        txfWidth.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                txfWidthMousePressed(evt);
            }
        });
        txfWidth.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                txfWidthActionPerformed(evt);
            }
        });

        txfHEight.setEditable(false);
        txfHEight.setText("Höhe in Kacheln");
        txfHEight.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                txfHEightMousePressed(evt);
            }
        });

        cxBG.setText("Hintergrund?");

        javax.swing.GroupLayout viewPortLayout = new javax.swing.GroupLayout(viewPort);
        viewPort.setLayout(viewPortLayout);
        viewPortLayout.setHorizontalGroup(
            viewPortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );
        viewPortLayout.setVerticalGroup(
            viewPortLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 456, Short.MAX_VALUE)
        );

        btnRegTile.setText("Fertig und Speichern");
        btnRegTile.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnRegTileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnRegTile)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(viewPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cxBG)
                            .addComponent(txfHEight, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txfWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSelectImage))))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(btnSelectImage)
                        .addGap(18, 18, 18)
                        .addComponent(txfWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txfHEight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cxBG))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(viewPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addComponent(btnRegTile)
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
    
    
    private String spath;
    private int pwidth=0;
    private int pheight=0;
    private void btnSelectImageActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSelectImageActionPerformed
    {//GEN-HEADEREND:event_btnSelectImageActionPerformed
        try            
        {
            spath  = Tools.FileChooseDlg(this, true, false, new String[]{"png"});
            if(spath.isEmpty())
                return;
            BufferedImage i =  ImageIO.read(new File(spath));
            pwidth =i.getWidth();
            while(i.getHeight()==-1);
            pheight = i.getHeight();
            UiTools.ScaleImageIntoFrame(spath, (Graphics2D)viewPort.getGraphics(),viewPort.getHeight(),viewPort.getWidth(),true);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnSelectImageActionPerformed

    private void btnRegTileActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnRegTileActionPerformed
    {//GEN-HEADEREND:event_btnRegTileActionPerformed
        String tpath = Tools.FileChooseDlg(this, true, false, new String[]{"iZtile"});
        int tw = Integer.valueOf(txfWidth.getText());
        int pd = 0;
        try
        {
           pd= ImageIO.read(new File(spath)).getWidth()/(tw*10);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        IZTile it = new IZTile(tpath, spath,tw,Integer.valueOf(txfHEight.getText()), cxBG.isSelected(),pd);
        tr.addTile(tpath, Tools.getInputString("Name"),true);
        this.setVisible(false);
    }//GEN-LAST:event_btnRegTileActionPerformed

    private void txfWidthMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_txfWidthMousePressed
    {//GEN-HEADEREND:event_txfWidthMousePressed
        int s = Tools.numberInputWIndow(4, new int[]{-1});
        if(s>0)
        {
            double factor = ((double)pwidth)/((double)s);
            double th = pheight/factor;
            System.err.println(s+"|"+factor+"|"+th);
            //System.err.println(th+"|"+(th%1));
            if(th==(Math.floor(th)))//glozzt ob keine nachkommastellen da
            {
            txfWidth.setText(""+s);
            txfHEight.setText(""+(int)th);
            }
        }
    }//GEN-LAST:event_txfWidthMousePressed

    private void txfHEightMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_txfHEightMousePressed
    {//GEN-HEADEREND:event_txfHEightMousePressed
        int s = Tools.numberInputWIndow(4, new int[]{-1});
        if(s>0)
        {
            double factor = ((double)pheight)/((double)s);
            double th = pwidth/factor;
            System.err.println(s+"|"+factor+"|"+th);
            //System.err.println(th+"|"+(th%1));
            if(th==(Math.floor(th)))//glozzt ob keine nachkommastellen da
            {
            txfHEight.setText(""+s);
            txfWidth.setText(""+(int)th);
            }
        }
    }//GEN-LAST:event_txfHEightMousePressed

    private void txfWidthActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txfWidthActionPerformed
    {//GEN-HEADEREND:event_txfWidthActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfWidthActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegTile;
    private javax.swing.JButton btnSelectImage;
    private javax.swing.JCheckBox cxBG;
    private javax.swing.JTextField txfHEight;
    private javax.swing.JTextField txfWidth;
    private javax.swing.JPanel viewPort;
    // End of variables declaration//GEN-END:variables
}
