package ip.hak;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InputDialog extends ClosableDialog implements ActionListener {
  Label lb1 = new Label("Source Dir");
  Label lb2 = new Label("Destination Dir");
  Label lb3 = new Label("Input Name");
  Label lb4 = new Label("Output Name");
  Label lb5 = new Label("Search String");
  Label lb6 = new Label("Replace String");
  TextField tf1 = new TextField();
  TextField tf2 = new TextField();
  Choice ch = new Choice();
  TextField tf4 = new TextField();
  TextField tf5 = new TextField();
  TextField tf6 = new TextField();
  Button okButton = new Button("OK");
  Button canButton = new Button("Cancel");
  String[] s;
  String sd, dd;
  String result[];

  public InputDialog(Frame fr, String title, boolean md, String[] st, String sdi, String ddi) {
    super(fr, title, md);
    s = st;
    sd = sdi;
    dd = ddi;
    init();
    setVisible(true);
  }

  public void init() {
    int x = 20;
    int y = 30;

    setSize(320, 250);
    setLayout(null);
    lb1.setSize(100, 20);
    lb1.setLocation(x, y);
    add(lb1);

    lb2.setSize(100, 20);
    lb2.setLocation(x, y += 30);
    add(lb2);

    lb3.setSize(100, 20);
    lb3.setLocation(x, y += 30);
    add(lb3);

    lb4.setSize(100, 20);
    lb4.setLocation(x, y += 30);
    add(lb4);

    lb5.setSize(100, 20);
    lb5.setLocation(x, y += 30);
    add(lb5);

    lb6.setSize(100, 20);
    lb6.setLocation(x, y += 30);
    add(lb6);

    tf1.setText(sd);
    tf1.setEditable(false);
    tf1.setSize(160, 20);
    tf1.setLocation(x += 120, y = 30);
    add(tf1);

    tf2.setText(dd);
    tf2.setEditable(false);
    tf2.setSize(160, 20);
    tf2.setLocation(x, y += 30);
    add(tf2);

    ch.setSize(160, 20);
    ch.setLocation(x, y += 30);
    ch.add("Just copy, No replace"); // if you select this item, all files are copyed to destination directory
    for (int i = 0; i < s.length; i++)
      ch.add(s[i]);
    add(ch);


    tf4.setSize(160, 20);
    tf4.setLocation(x, y += 30);
    add(tf4);

    tf5.setSize(160, 20);
    tf5.setLocation(x, y += 30);
    add(tf5);

    tf6.setSize(160, 20);
    tf6.setLocation(x, y += 30);
    add(tf6);

    okButton.setSize(60, 20);
    okButton.setLocation(66, y += 30);
    add(okButton);
    okButton.addActionListener(this);

    canButton.setSize(60, 20);
    canButton.setLocation(192, y);
    add(canButton);
    canButton.addActionListener(this);
  }

  public boolean checkError() {
    int ind = ch.getSelectedIndex();
    if (ind == 0) {
      result = null;
      return true;
    }

    result = new String[4];
    result[0] = ch.getSelectedItem();
    result[1] = tf4.getText();
    result[2] = tf5.getText();
    result[3] = tf6.getText();

    // i<result.length-1 : because replaceString can be null...
    for (int i = 0; i < result.length - 1; i++)
      if (result[i] == null)
        return false;

    return true;
  }

  public String[] getInput() {
    return result;
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okButton) {
      if (!checkError()) {
        ErrorDialog ed = new ErrorDialog(new Frame(), "Error!", "String should be not null");
        return;
      }
      dispose();
    }

    if (e.getSource() == canButton) {
      dispose();
    }
  }
}