package com.marinilli.b2.c7.launcher;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Chapter 7 - This dialog summarize the installation options to the user
 * before continuing with the installation
 *
 * @author Mauro Marinilli
 * @version 1.0
 */
public class SummaryDialog extends InstallerDialog {
  private boolean installChoice = false;
  private final static String TITLE = "Installation Settings";
  private final static String DESCRIPTION =
      "The application will be installed on your computer "+
      "with the following settings.";

  //GUI stuff
  JButton installButton;
  JButton backButton;
  JCheckBox instJreCheckBox;
  JTextField destDirTextField;
  JCheckBox readmeCheckBox;

  /**
   *  Constructor
   */
  public SummaryDialog(CDLauncher inst) {
    super(inst, TITLE);
  }

  /**
   *  GUI initialization
   */
  void graphInit() {
    JPanel centerPanel = new JPanel();
    JPanel southPanel = new JPanel();
    JPanel panel1 = new JPanel();
    installButton = new JButton();
    backButton = new JButton();
    instJreCheckBox = new JCheckBox();
    JPanel destDirPanel = new JPanel();
    JPanel instJrePanel = new JPanel();
    JLabel destDirLabel = new JLabel();
    destDirTextField = new JTextField();
    readmeCheckBox = new JCheckBox();

    JTextArea desc = new JTextArea(DESCRIPTION);
    desc.setBorder(BorderFactory.createEmptyBorder(4,2,8,2));
    desc.setEditable(false);
    desc.setBackground(installButton.getBackground());
    desc.setLineWrap(true);
    desc.setWrapStyleWord(true);
    Box box1 = Box.createVerticalBox();
    panel1.setLayout(new BorderLayout());
    installButton.setText("install");
    installButton.setIcon(CDLauncher.installIcon);
    installButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // overwrite setting
        installer.setDestinationDir(destDirTextField.getText());
        installer.setInstallJre( instJreCheckBox.isSelected() );
        installer.setShowReadme( readmeCheckBox.isSelected() );
        installChoice = true;
        dispose();
      }
    });
    backButton.setText("back");
    backButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    southPanel.setLayout(new BorderLayout());
    southPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
    instJreCheckBox.setText("install JRE");
    destDirLabel.setText("destination directory");
    destDirTextField.setText(installer.getDestinationDir());
    destDirTextField.setColumns(22);
    readmeCheckBox.setText("show readme after installation");
    readmeCheckBox.setSelected( installer.isShowReadme() );
    instJreCheckBox.setSelected( installer.isInstallJre() );

    getContentPane().add(panel1);
    panel1.add(desc, BorderLayout.NORTH);
    panel1.add(centerPanel, BorderLayout.CENTER);
    centerPanel.add(box1, null);
    box1.add(destDirPanel, null);
    destDirPanel.add(destDirLabel, null);
    destDirPanel.add(destDirTextField, null);
    box1.add(readmeCheckBox, null);
    box1.add(instJreCheckBox, null);
    box1.add(instJrePanel, null);
    getContentPane().add(southPanel, BorderLayout.SOUTH);
    southPanel.add(backButton, BorderLayout.WEST);
    southPanel.add(installButton, BorderLayout.EAST);
    pack();
  }

  /**
   *  Whether the user accepted the installation settings
   */
  public boolean isInstallChoice(){
    return installChoice;
  }

}