/*
 * @(#)NewProjectDialog.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2000-2004 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 *
 * Jörg Haeger, 10.05.2000
 */

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

/**
 * NewProjectDialog
 *
 * @version 20040703
 * @author Jörg P. M. Haeger
 */
public class NewProjectDialog extends SequenceDialog {
	protected JPanel panel0;
	protected ActionListener eventHandler;

	protected JComponent modesBox;
	protected JRadioButton
		dataButton,
		imageButton,
		audioButton,
		copyButton,
		blankButton;

	protected JComponent mediumBox;
	protected JRadioButton
		CDRButton,
		CDRWButton,
		DVDMinusRButton,
		DVDMinusRWButton,
		DVDPlusRButton,
		DVDPlusRWButton;

	protected JRadioButton
		MB650Button,
		MB700Button,
		MB800Button,
		MB4482Button;

	protected JRadioButton
		warnButton,
		overwriteButton,
		hideButton,
		addButton;

	protected JComponent blankBox;
	protected JRadioButton
		CDRWBlankButton,
		DVDMinusRWBlankButton,
		DVDPlusRWBlankButton;

	protected JRadioButton
		blankFastButton,
		blankAllButton;

	protected JComboBox speedBox;

	protected JCheckBox
		acceptKeyBox,
		rejectEmptyCDBox,
		rejectWithoutUserIDBox;

	protected JTextField acceptField;

	public NewProjectDialog(boolean canBeCancelled) {
		super(".title");
		if (!canBeCancelled)
			setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		eventHandler = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == DVDMinusRButton
						|| e.getSource() == DVDMinusRWButton
						|| e.getSource() == DVDPlusRButton
						|| e.getSource() == DVDPlusRWButton)
					MB4482Button.setSelected(true);

				if (state == 1)
					validateMediumBox();
			}
		};

		initModesBox();
		initMediumBox();
		initBlankBox();
		initBlankBox2();
		eventHandler.actionPerformed(new ActionEvent(this, 0, ""));

		panel0 = new JPanel() {
			public Dimension getPreferredSize() {
				Dimension a = mediumBox.getPreferredSize();
				Dimension b = modesBox.getPreferredSize();
				if (b.width > a.width)
					a.width = b.width;
				if (b.height > a.height)
					b.height = a.height;
				Insets insets = getInsets();
				a.width += insets.left + insets.right;
				a.height += insets.top + insets.bottom;
				return a;
			}
		};
		panel0.setLayout(new BoxLayout(panel0, BoxLayout.Y_AXIS));
		getContentPane().add(panel0, BorderLayout.CENTER);

		enterState(0);
		pack();
	}

	protected boolean apply() {
		if (dataButton.isSelected()) {
			Mode.type = Mode.data;
			Mode.makeImage = true;
		}
		else
		if (imageButton.isSelected()) {
			Mode.type = Mode.data;
			Mode.makeImage = false;
		}
		else
		if (audioButton.isSelected()) {
			Mode.setTypeAudio();
		}

		if (CDRButton.isSelected()) {
			if (MB650Button.isSelected())
				Mode.setMedium(Mode.CDR74);
			else
			if (MB700Button.isSelected())
				Mode.setMedium(Mode.CDR80);
			else
			if (MB800Button.isSelected())
				Mode.setMedium(Mode.CDR90);
		}
		else
		if (CDRWButton.isSelected()) {
			if (MB650Button.isSelected())
				Mode.setMedium(Mode.CDRW74);
			else
				Mode.setMedium(Mode.CDRW80);
		}
		else
		if (DVDMinusRButton.isSelected())
			Mode.setMedium("DVD-R");
		else
		if (DVDMinusRWButton.isSelected())
			Mode.setMedium("DVD-RW");
		else
		if (DVDPlusRButton.isSelected())
			Mode.setMedium("DVD+R");
		else
		if (DVDPlusRWButton.isSelected())
			Mode.setMedium("DVD+RW");

		Options.reset();
		Options.waitForEmptyCD = warnButton.isSelected();
		Options.blankTOC = overwriteButton.isSelected();
		Options.hideContents = hideButton.isSelected();

		if (copyButton.isSelected()) {
			setVisible(false);
			CDcreator.model.burnCopy();
		}
		else
		if (blankButton.isSelected()) {
			setVisible(false);

			if (CDcreator.writeProtection && acceptField.isEnabled())
				Options.acceptKey = acceptField.getText();
			else
				Options.acceptKey = "";
			Options.rejectEmptyCD = rejectEmptyCDBox.isSelected();
			Options.rejectWithoutUserID
				= rejectWithoutUserIDBox.isSelected();

//			CDcreator.mainWin.blank();
			if (CDRWBlankButton.isSelected())
				Mode.setMedium(Mode.CDR74);
			else
			if (DVDMinusRWBlankButton.isSelected())
				Mode.setMedium("DVD-RW");
			else
			if (DVDPlusRWBlankButton.isSelected())
				Mode.setMedium("DVD+RW");
			Options.speed = OptionsDialog.getSpeed(speedBox);
			try {
				new Blank(blankAllButton.isSelected()).exec();
			} catch (CommandError cmdErr) {};
		}

		return true;
	}

	private JRadioButton createButton(JComponent box,
			ButtonGroup group, String label) {
		label = i18n(label);

		JRadioButton button = new JRadioButton(label);
		button.addActionListener(eventHandler);
		box.add(button);
		group.add(button);
		return button;
	}

	protected JComponent createText(String label) {
		label = i18n(label);

		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);
		vbox.setAlignmentX(LEFT_ALIGNMENT);
		vbox.add(Box.createVerticalGlue());

		while (true) {
			int pos = label.indexOf("<br>");
			if (true || pos < 0) {
				vbox.add(new JLabel(label));
				break;
			}
			else
			if (pos == 0)
//				vbox.add(Box.createVerticalGlue());
				vbox.add(new JLabel(" "));
			else
				vbox.add(new JLabel(label.substring(0, pos)));
			label = label.substring(pos + 4);
		}

		vbox.add(Box.createVerticalGlue());
		return vbox;
	}

	protected void enterHelpState(int aState) {
		panel0.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel0.removeAll();

		if (aState == 0)
			panel0.add(CDcreator.getHelp("NewProjectModes"));
		else
		if (aState == 1)
			panel0.add(CDcreator.getHelp("NewProjectMedium"));
		else
		if (aState == 2)
			if (copyButton.isSelected())
				panel0.add(CDcreator.getHelp("RejectionOfCDs"));

		validate();
	}

	protected void enterState(int aState) {
		panel0.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel0.removeAll();

		if (aState == 0)
			panel0.add(modesBox);
		else
		if (aState == 1) {
			DVDMinusRButton.setEnabled(
				CDcreator.supports("DVD-R ")
				&& !audioButton.isSelected()
				&& !copyButton.isSelected());
			DVDMinusRWButton.setEnabled(
				CDcreator.supports("DVD-RW")
				&& !audioButton.isSelected()
				&& !copyButton.isSelected());
			DVDPlusRButton.setEnabled(
				CDcreator.supports("DVD+R ")
				&& !audioButton.isSelected()
				&& !copyButton.isSelected());
			DVDPlusRWButton.setEnabled(
				CDcreator.supports("DVD+RW")
				&& !audioButton.isSelected()
				&& !copyButton.isSelected());
			if (dataButton.isSelected()) {
				addButton.setEnabled(true);
				addButton.setSelected(true);
			}
			else {
				addButton.setEnabled(false);
			}
			validateMediumBox();
			if (blankButton.isSelected()) {
				DVDMinusRWBlankButton.setEnabled(
					CDcreator.supports("DVD-RW"));
				DVDPlusRWBlankButton.setEnabled(
					CDcreator.supports("DVD+RW"));
				panel0.add(blankBox);
			}
			else
				panel0.add(mediumBox);
		}
		else
		if (aState == 2)
			if (copyButton.isSelected())
				panel0.add(createText(".pressOK"));
			else
			if (blankButton.isSelected()) {
				if (CDcreator.writeProtection) {
					panel0.add(createHeader("OptionsDialog.rejectionOfCDs"));
					panel0.add(new JLabel(i18n("OptionsDialog.rejectIf")));
					JComponent[] cs = {
						rejectEmptyCDBox,
						rejectWithoutUserIDBox
					};
					MyBox waitForSubBox = OptionsDialog.createVBox(cs);
					panel0.add(OptionsDialog.createLabelItemBox(
						Box.createHorizontalStrut(20),
						waitForSubBox));
					panel0.add(Box.createVerticalGlue());
					panel0.add(new JLabel(i18n(
						"OptionsDialog.rejectWrongUserID")));
					panel0.add(OptionsDialog.createLabelItemBox(
						Box.createHorizontalStrut(20),
							OptionsDialog.createLabelItemBox(
								new JLabel(i18n("OptionsDialog.userID")),
								acceptField)));
					panel0.add(Box.createVerticalGlue());
				}
				panel0.add(createText(".blankWarning"));
			}
			else
				panel0.add(createText(".instructions"));
		else
			panel0.add(new JLabel("state " + aState));

		validate();
	}

	protected boolean hasHelp(int aState) {
		return aState == 0;
	}

	protected boolean hasMore() {
		return state < 2;
	}

	protected void initBlankBox() {
		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);

		vbox.add(createHeader(".typeLabel"));
		MyBox hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		ButtonGroup group = new ButtonGroup();
		CDRWBlankButton = createButton(hbox, group, "CD-RW");
		CDRWBlankButton.setSelected(true);
		DVDMinusRWBlankButton = createButton(hbox, group, "DVD-RW");
		DVDMinusRWBlankButton.setSelected(Mode.medium.equals("DVD-RW"));
		DVDPlusRWBlankButton = createButton(hbox, group, "DVD+RW");
		DVDPlusRWBlankButton.setSelected(Mode.medium.equals("DVD+RW"));
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".blankModeLabel"));
		group = new ButtonGroup();
		blankFastButton = createButton(vbox, group, i18n("ODfast"));
		blankFastButton.setSelected(true);
		blankAllButton = createButton(vbox, group, i18n("ODall"));

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(i18n("speed")));
		speedBox = OptionsDialog.createSpeedBox();
		speedBox.setAlignmentX(LEFT_ALIGNMENT);
		vbox.add(speedBox);

		vbox.add(Box.createVerticalGlue());
		blankBox = vbox;
	}

	protected void initBlankBox2() {
		acceptKeyBox = new JCheckBox(
			i18n("OptionsDialog.acceptKey"), true);
		acceptKeyBox.addChangeListener(
			new javax.swing.event.ChangeListener() {
				public void stateChanged(
						javax.swing.event.ChangeEvent e) {
					acceptField.setEnabled(
						acceptKeyBox.isSelected());
				}
			});
		if (Options.acceptKey.length() == 0)
			Options.acceptKey = OptionsDialog.getUserName();
		acceptField = OptionsDialog.createTextField(Options.acceptKey, 20);
		acceptField.setEnabled(acceptKeyBox.isSelected());

		// init reject components
		rejectEmptyCDBox = new JCheckBox(
			i18n("OptionsDialog.rejectEmptyCD"), Options.rejectEmptyCD);
		rejectWithoutUserIDBox = new JCheckBox(
			i18n("OptionsDialog.rejectWithoutUserID"),
			Options.rejectWithoutUserID);
	}

	protected void initMediumBox() {
		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);

		vbox.add(createHeader(".typeLabel"));
		MyBox hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		ButtonGroup group = new ButtonGroup();
		CDRButton = createButton(hbox, group, "CD-R");
		CDRButton.setSelected(Mode.medium.equals(Mode.CDR));
		CDRWButton = createButton(hbox, group, "CD-RW");
		CDRWButton.setSelected(Mode.medium.equals(Mode.CDRW));
		if (CDcreator.supports("DVD-R")) {
			DVDMinusRButton = createButton(hbox, group, i18n("DVD-R"));
			DVDMinusRButton.setSelected(Mode.medium.equals("DVD-R"));
			DVDMinusRWButton = createButton(hbox, group, i18n("DVD-RW"));
			DVDMinusRWButton.setSelected(Mode.medium.equals("DVD-RW"));
		}
		else {
			DVDMinusRButton = new JRadioButton();
			DVDMinusRWButton = new JRadioButton();
		}
		DVDPlusRButton = createButton(hbox, group, i18n("DVD+R"));
		DVDPlusRButton.setSelected(Mode.medium.equals("DVD+R"));
		DVDPlusRWButton = createButton(hbox, group, i18n("DVD+RW"));
		DVDPlusRWButton.setSelected(Mode.medium.equals("DVD+RW"));
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".mediumSize"));
		hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		group = new ButtonGroup();
		MB650Button = createButton(hbox, group, "74/650 MB");
		MB650Button.setSelected(Mode.mediumSize == 650);
		MB700Button = createButton(hbox, group, "80/700 MB");
		MB700Button.setSelected(Mode.mediumSize == 700);
		MB800Button = createButton(hbox, group, "90/800 MB");
		MB800Button.setSelected(Mode.mediumSize == 800);
		MB4482Button = createButton(hbox, group, "4482 MB");
		MB4482Button.setSelected(Mode.mediumSize == 4482);
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".ifNotEmpty"));
		MyBox vbox2 = new MyBox(BoxLayout.Y_AXIS);
		vbox2.setAlignmentX(LEFT_ALIGNMENT);
		group = new ButtonGroup();
		warnButton = createButton(vbox2, group, ".warnIfNotEmpty");
		warnButton.setSelected(Options.waitForEmptyCD);
		overwriteButton = createButton(vbox2, group, ".overwrite");
		hideButton = createButton(vbox2, group, ".hide");
		addButton = createButton(vbox2, group, ".add");
		addButton.setSelected(!Options.waitForEmptyCD);
		vbox.add(vbox2);

		vbox.add(Box.createVerticalGlue());
		mediumBox = vbox;
	}

	protected void initModesBox() {
		JPanel vbox = new JPanel();
		vbox.setLayout(new BoxLayout(vbox, BoxLayout.Y_AXIS));
		ButtonGroup group = new ButtonGroup();

//		vbox.add(Box.createHorizontalStrut(200));
		vbox.add(createHeader(".selectMode"));
		dataButton = createButton(vbox, group, ".storeFiles");
		dataButton.setSelected(true);
		imageButton = createButton(vbox, group, ".imageCD");
		audioButton = createButton(vbox, group, ".audioCD");
		audioButton.setEnabled(CDcreator.audioCDSupport);
		copyButton = createButton(vbox, group, ".copyCD");
		copyButton.setEnabled(CDcreator.copySupport);
		blankButton = createButton(vbox, group, ".blank");
		vbox.add(Box.createVerticalGlue());

		vbox.add(Box.createVerticalStrut(20));
		final JCheckBox onStartupBox = new JCheckBox(
			i18n(".onStartup"), Config.getBool(Config.onStartup));
		onStartupBox.addActionListener(
			new java.awt.event.ActionListener() {
				public void actionPerformed(
						java.awt.event.ActionEvent e) {
					Config.set(Config.onStartup, onStartupBox.isSelected());
				}
		});
		vbox.add(onStartupBox);

		JPanel hbox = new JPanel();
		hbox.setLayout(new BoxLayout(hbox, BoxLayout.X_AXIS));
		hbox.add(Box.createHorizontalStrut(60));
		hbox.add(vbox);

		modesBox = hbox;
	}

	protected boolean isDVDButtonSelected() {
		return DVDMinusRButton.isSelected()
			|| DVDMinusRWButton.isSelected()
			|| DVDPlusRButton.isSelected()
			|| DVDPlusRWButton.isSelected();
	}

	protected void validateMediumBox() {
		for (int i = 0; i < 3; i++) {
			MB650Button.setEnabled(!isDVDButtonSelected());
			MB700Button.setEnabled(!isDVDButtonSelected());
			MB800Button.setEnabled(CDcreator.overburnSupport
				&& !isDVDButtonSelected());
			MB4482Button.setEnabled(isDVDButtonSelected());

			overwriteButton.setEnabled(!CDRButton.isSelected()
				&& !DVDMinusRButton.isSelected()
				&& !DVDPlusRButton.isSelected());
			hideButton.setEnabled(dataButton.isSelected()
				&& !isDVDButtonSelected());
			addButton.setEnabled(dataButton.isSelected());

//			speedBox.setEnabled(CDRWBlankButton.isSelected());
			if (CDRWBlankButton.isSelected()) {
				if (Mode.isMediumDVD()) {
					Mode.setMedium(Mode.CDR74);
					speedBox.setModel(
						OptionsDialog.createSpeedBox().getModel());
				}
			}
			else
				if (!Mode.isMediumDVD()) {
					Mode.setMedium("DVD+RW");
					speedBox.setModel(
						OptionsDialog.createSpeedBox().getModel());
				}

			if (DVDPlusRWButton.isSelected()
					&& !DVDPlusRWButton.isEnabled())
				CDRButton.setSelected(true);

			if (MB4482Button.isSelected()
					&& !MB4482Button.isEnabled())
				MB650Button.setSelected(true);

			if (addButton.isSelected()
					&& !addButton.isEnabled())
				warnButton.setSelected(true);
		}
	}
}
