/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csa.tiletools.map;

import csa.gui.components.CSATableModel;
import csa.gui.components.CSATablePanel;
import csa.gui.components.DoubleClickAction;
import csa.tiletools.graphics.ImageComponent;
import csa.tiletools.graphics.ImageSequence;
import csa.tiletools.graphics.ImageSequenceData;
import csa.tiletools.graphics.ImageSequenceDataPool;
import csa.tiletools.tiles.Tile;
import csa.tiletools.tiles.TileAttribute;
import csa.tiletools.tiles.TileData;
import java.awt.BorderLayout;
import java.io.File;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author salchr
 */
public class TileListForm extends javax.swing.JFrame {
    
    TMap map=null;
    MapPanel mapPanel = null;
    MapEdit mapEdit = null;
    TileData mCurrentTile = null;
    Vector <TileAttribute> mAttributeData = new Vector <TileAttribute>();
    
    // event gotten row / column of double clicked tableentry
    private int mRow=-1;
    private int mCol=-1;

    /**
     * Creates new form TileList
     */
    public TileListForm() {
        initComponents();
        cSATablePanel1.setDoubleClickAction(new DoubleClickAction()
        {
            public void doIt()
            {
                JTable table = (JTable) evt.getSource();
                mRow = table.convertRowIndexToModel(table.rowAtPoint(evt.getPoint()));
                mCol = table.convertColumnIndexToModel(table.columnAtPoint(evt.getPoint()));
                mCol = cSATablePanel1.getModel().convertEnabledColToRealCol(mCol);
                tableDoubleClicked();
            }
        });
        cSATablePanel1.setXMLId("ImageAttributes2");
        cSATablePanel1.setTableStyleSwitchingEnabled(false);
        cSATablePanel1.setTablePopupEnabled(true);
    }

    /**
     * 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() {

        cSATablePanel1 = new csa.gui.components.CSATablePanel();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButtonDownTile = new javax.swing.JButton();
        jButtonDeleteTile = new javax.swing.JButton();
        jPanel15 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jTextFieldTileName = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTextFieldTileID = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jTextFieldGraphicsHeightTile = new javax.swing.JTextField();
        jTextFieldGraphicsWidthTile = new javax.swing.JTextField();
        cSATablePanel2 = new csa.gui.components.CSATablePanel();
        jButtonTileUp = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jTextField2 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();

        setTitle("Layer Tiles");
        setAlwaysOnTop(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tile Info"));

        jButton4.setText("GFX Info");
        jButton4.setEnabled(false);

        jButtonDownTile.setText("Down");
        jButtonDownTile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDownTileActionPerformed(evt);
            }
        });

        jButtonDeleteTile.setText("Delete");
        jButtonDeleteTile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteTileActionPerformed(evt);
            }
        });

        jPanel15.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel15.setPreferredSize(new java.awt.Dimension(52, 52));
        jPanel15.setLayout(new java.awt.BorderLayout());

        jLabel14.setText("Name");

        jLabel15.setText("ID ");

        jLabel19.setText("Size");

        jTextFieldGraphicsHeightTile.setToolTipText("If size is not the same as graphic, the grafix will be scaled to this size!");

        jTextFieldGraphicsWidthTile.setToolTipText("If size is not the same as graphic, the grafix will be scaled to this size!");

        jButtonTileUp.setText("Up");
        jButtonTileUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTileUpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel19))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldTileName)
                    .addComponent(jTextFieldTileID)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextFieldGraphicsWidthTile, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldGraphicsHeightTile))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton4))
            .addComponent(cSATablePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButtonTileUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonDownTile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonDeleteTile))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jTextFieldTileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jTextFieldTileID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jTextFieldGraphicsWidthTile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldGraphicsHeightTile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cSATablePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jButtonDownTile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDeleteTile)
                    .addComponent(jButtonTileUp)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 0, 0, 0));

        jTextField2.setEditable(false);

        jTextField1.setEditable(false);

        jLabel1.setText("Map:");

        jLabel2.setText("Layer:");

        jTextField3.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(3, 3, 3)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jLabel2)
                .addGap(4, 4, 4)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(cSATablePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, 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(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cSATablePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

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

    private void jButtonDeleteTileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteTileActionPerformed
        int r = cSATablePanel1.getTable().getSelectedRow();
        if (r==-1) return;
        int row = cSATablePanel1.getTable().convertRowIndexToModel(r);
        
        mapPanel.deleteTile(map.getCurrentLayerNo(), row);
        mapEdit.updateLayerSize();
        
        cSATablePanel1.reInit();

        mCurrentTile = null;
        clearAll();
        repaint();
    }//GEN-LAST:event_jButtonDeleteTileActionPerformed

    private void jButtonTileUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTileUpActionPerformed
        int r = cSATablePanel1.getTable().getSelectedRow();
        if (r==-1) return;
        if (r > map.getLayer(map.getCurrentLayerNo()).mTiles.size()-1) return;
        int row = cSATablePanel1.getTable().convertRowIndexToModel(r);
        if (row == map.getLayer(map.getCurrentLayerNo()).mTiles.size()-1) return;
        
        mapPanel.upTile(map.getCurrentLayerNo(), row);
            
        cSATablePanel1.getTable().repaint();
        ListSelectionModel selectionModel =  cSATablePanel1.getTable().getSelectionModel();
        selectionModel.setSelectionInterval(row+1, row+1);        

        repaint();
    }//GEN-LAST:event_jButtonTileUpActionPerformed

    private void jButtonDownTileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDownTileActionPerformed
        int r = cSATablePanel1.getTable().getSelectedRow();
        if (r==-1) return;
        if (r > map.getLayer(map.getCurrentLayerNo()).mTiles.size()-1) return;
        int row = cSATablePanel1.getTable().convertRowIndexToModel(r);
        if (row == 0) return;
        
        
        mapPanel.downTile(map.getCurrentLayerNo(), row);
        cSATablePanel1.getTable().repaint();

        ListSelectionModel selectionModel =  cSATablePanel1.getTable().getSelectionModel();
        selectionModel.setSelectionInterval(row-1, row-1);        


        repaint();
    }//GEN-LAST:event_jButtonDownTileActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private csa.gui.components.CSATablePanel cSATablePanel1;
    private csa.gui.components.CSATablePanel cSATablePanel2;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButtonDeleteTile;
    private javax.swing.JButton jButtonDownTile;
    private javax.swing.JButton jButtonTileUp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextFieldGraphicsHeightTile;
    private javax.swing.JTextField jTextFieldGraphicsWidthTile;
    private javax.swing.JTextField jTextFieldTileID;
    private javax.swing.JTextField jTextFieldTileName;
    // End of variables declaration//GEN-END:variables

    public void updateMap()
    {
        if (map == null)
        {
            clearAll();
            return;
        }
        // build table model,
        // active tile to none 
        CSATableModel m = CSATableModel.buildTableModel(getMapTableModel());

        cSATablePanel1.setModel(m);
        cSATablePanel1.reInit();
        jTextField1.setText(map.getData().mName);
        jTextField2.setText(map.getCurrentLayer().getData().mName);
        jTextField3.setText(""+map.getCurrentLayerNo());
    }
    
    javax.swing.table.TableModel getMapTableModel()
    {
        AbstractTableModel model = new
         AbstractTableModel() {
            @Override public String getColumnName(int col)
            {
                
                if (col==0) return "Name";
                if (col==1) return "Position";
                return "";
            }
            @Override
            public int getRowCount()
            {
                if (map == null) return 0;
                return map.getCurrentLayer().mTiles.size();
            }
            @Override
            public int getColumnCount() { return 2;}
            @Override
            public Object getValueAt(int row, int col)
            {

                if (col==0)
                {
                    return map.getCurrentLayer().mTiles.get(row).getData().mName;
                }
                if (col==1) return ""+map.getCurrentLayer().mTiles.get(row).getX()+"x"+map.getCurrentLayer().mTiles.get(row).getY();
                return "";
            }
            @Override public boolean isCellEditable(int row, int col)
                { return false; }
            @Override public Class getColumnClass(int col)
            {
                return String.class;
            }
        };
        return model;
    }

    public void setMap(TMap m, MapPanel mp, MapEdit me)
    {
        map = m;
        mapPanel = mp;
        mapEdit = me;
        updateMap();
    }

    private void tableDoubleClicked()
    {
        if (mRow <0) return;
        setCurrentTile(map.getCurrentLayer().mTiles.get(mRow).getData());
        mapPanel.highliteTile(map.getCurrentLayerNo(), mRow);
    }

    private void setCurrentTile(TileData t)
    {
        mCurrentTile = t;
        mAttributeData = TileAttribute.getVector(mCurrentTile);
        
        CSATableModel m = CSATableModel.buildTableModel(getAttributeTableModel());

        cSATablePanel2.setModel(m);

        cSATablePanel2.setXMLId("ImageAttributes2");
        cSATablePanel2.setTableStyleSwitchingEnabled(false);
        cSATablePanel2.setTablePopupEnabled(true);
        cSATablePanel2.setDoubleClickAction(new DoubleClickAction()
        {
            @Override
            public void doIt()
            {
            }
        });
        cSATablePanel2.reInit();

        jTextFieldTileName.setText(mCurrentTile.mName);
        jTextFieldTileID.setText(mCurrentTile.getID());
        jTextFieldGraphicsHeightTile.setText(""+mCurrentTile.getHeight()); 
        jTextFieldGraphicsWidthTile.setText(""+mCurrentTile.getWidth()); 
        jPanel15.removeAll();
        ImageSequenceData tileImageData = getImageSequenceDataForTile(mCurrentTile);
        ImageComponent oneTile = new ImageComponent();
        oneTile.setSequence(new ImageSequence(tileImageData));
    
        oneTile.setBounds(2,2, 52, 52);
        if (tileImageData != null) 
        {
            oneTile.setDelay(tileImageData.getDelay());
            oneTile.setScaled(true, 48, 48);
            oneTile.setVisible(true);
        }                
        jPanel15.add(oneTile, BorderLayout.CENTER);
        jPanel15.invalidate();
        jPanel15.repaint();
    }
    
    ImageSequenceData getImageSequenceDataForTile(TileData tileData)
    {
        if (tileData.getImageDataFile().trim().length()==0) return null;
        
        String imageFilename = "Graphics"+File.separator+tileData.getImageDataFile()+".xml";
        ImageSequenceDataPool imagePool = new ImageSequenceDataPool(imageFilename);
        if (imagePool == null) return null;
        String nameToSet = tileData.getImageDataName();
        return imagePool.get(nameToSet);
    }

    javax.swing.table.TableModel getAttributeTableModel()
    {
        AbstractTableModel model = new
         AbstractTableModel() {
            @Override public String getColumnName(int col)
            {
                if (col==0) return "Name";
                if (col==1) return "Type";
                if (col==2) return "Value";
                return "";
            }
            @Override
            public int getRowCount()
            {
                return mAttributeData.size();
            }
            @Override
            public int getColumnCount() { return 3;}
            @Override
            public Object getValueAt(int row, int col)
            {

                if (col==0) return mAttributeData.elementAt(row).name;
                if (col==1) return mAttributeData.elementAt(row).type;
                if (col==2) return mAttributeData.elementAt(row).sVal();
                return "";
            }
            @Override public boolean isCellEditable(int row, int col)
                { return false; }
            @Override public Class getColumnClass(int col)
            {
                return String.class;
            }
        };
        return model;
    }
    void clearAll()
    {
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextFieldTileName.setText("");
        jTextFieldTileID.setText("");
        jTextFieldGraphicsHeightTile.setText(""); 
        jTextFieldGraphicsWidthTile.setText(""); 
        mAttributeData = new Vector <TileAttribute>();
        CSATableModel m = CSATableModel.buildTableModel(getAttributeTableModel());
        cSATablePanel2.setSameModelType(m);
        jPanel15.removeAll();
    }
    public void updateTileList()
    {
        cSATablePanel1.reInit();
    }
}
