/* 
 * Copyright 2015 Torridity.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tor.tribes.ui.windows;

import de.tor.tribes.types.FightReport;
import de.tor.tribes.ui.panels.ReportTableTab;
import de.tor.tribes.util.html.FightReportHTMLToolTipGenerator;
import java.awt.Point;

/**
 *
 * @author Torridity
 */
public class ReportShowDialog extends javax.swing.JDialog {

    private Point dndDelta = null;

    /**
     * Creates new form ReportShowDialog
     */
    public ReportShowDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jCloseAllButton.setEnabled(false);
        jCloseLayerButton.setEnabled(false);
    }
    private ReportTableTab parentTab = null;
    private int layer = -1;

    public void setParentTab(ReportTableTab pTab, int pLayer) {
        parentTab = pTab;
        layer = pLayer;
        jCloseAllButton.setEnabled(true);
        jCloseLayerButton.setEnabled(true);
    }

    public void setupAndShow(FightReport pReport) {
        String layerIdentifier = "";
        if (layer != -1) {
            layerIdentifier = " [Ebene " + (layer + 1) + "]";
        }
        jTitle.setText(pReport.getSourceVillage() + " -> " + pReport.getTargetVillage() + layerIdentifier);
        jLabel1.setText(FightReportHTMLToolTipGenerator.buildToolTip(pReport));
        pack();
        setVisible(true);
    }

    public int getLayer() {
        return layer;
    }

    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jCloseAllButton = new javax.swing.JButton();
        jCloseLayerButton = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireClickEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jLabel1, gridBagConstraints);

        jCloseAllButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/ui/close_all.png"))); // NOI18N
        jCloseAllButton.setToolTipText("Alle geöffneten Berichte schließen");
        jCloseAllButton.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jCloseAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireCloseAllEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCloseAllButton, gridBagConstraints);

        jCloseLayerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/ui/close_layer.png"))); // NOI18N
        jCloseLayerButton.setToolTipText("Alle geöffneten Berichte in der Ebene dieses Berichtes schließen");
        jCloseLayerButton.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jCloseLayerButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireCloseLayerEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCloseLayerButton, gridBagConstraints);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/ui/close_frame.png"))); // NOI18N
        jButton3.setToolTipText("Diesen Bericht schließen");
        jButton3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireCloseThisEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jButton3, gridBagConstraints);

        jTitle.setBackground(new java.awt.Color(204, 204, 204));
        jTitle.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTitle.setOpaque(true);
        jTitle.setPreferredSize(new java.awt.Dimension(100, 29));
        jTitle.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                fireTitlePressedEvent(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                fireTitleReleasedEvent(evt);
            }
        });
        jTitle.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                fireTitleDraggedEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jTitle, gridBagConstraints);

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

    private void fireClickEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireClickEvent
        if (evt.getClickCount() == 2) {
            parentTab.closeReportLayer(layer);
        }
    }//GEN-LAST:event_fireClickEvent

    private void fireTitleDraggedEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireTitleDraggedEvent
        Point pos = evt.getLocationOnScreen();
        if (pos == null) {
            return;
        }
        pos.translate(-dndDelta.x, -dndDelta.y);
        setLocation(pos);
    }//GEN-LAST:event_fireTitleDraggedEvent

    private void fireTitlePressedEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireTitlePressedEvent
        dndDelta = evt.getPoint();
    }//GEN-LAST:event_fireTitlePressedEvent

    private void fireTitleReleasedEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireTitleReleasedEvent
        dndDelta = null;
    }//GEN-LAST:event_fireTitleReleasedEvent

    private void fireCloseThisEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireCloseThisEvent
        if (parentTab != null) {
            parentTab.closeReportDialog(this);
        } else {
            dispose();
        }
    }//GEN-LAST:event_fireCloseThisEvent

    private void fireCloseLayerEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireCloseLayerEvent
        if (!jCloseLayerButton.isEnabled()) {
            return;
        }
        if (parentTab != null) {
            parentTab.closeReportLayer(layer);
        } else {
            dispose();
        }
    }//GEN-LAST:event_fireCloseLayerEvent

    private void fireCloseAllEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireCloseAllEvent
        if (!jCloseAllButton.isEnabled()) {
            return;
        }
        if (parentTab != null) {
            parentTab.closeAllReportDialogs();
        } else {
            dispose();
        }
    }//GEN-LAST:event_fireCloseAllEvent
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jCloseAllButton;
    private javax.swing.JButton jCloseLayerButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jTitle;
    // End of variables declaration//GEN-END:variables
}