package org.kne.cloud.klalb.uitool;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.swing.*;

import org.kne.cloud.network.klalb.ui.UIEnv;

public class PathInput extends JDialog {
	private JTextField textField;
	private boolean checkfile=true;
	private boolean isdir=false;
	protected JFileChooser jfc;
	private Consumer<File> al;
	public Consumer<File> getAl() {
		return al;
	}

	public void setAl(Consumer<File> al) {
		this.al = al;
	}

	public PathInput(Window d){
		super(d);
		setResizable(false);
		setModal(true);
		setSize(500, 110);
		setLocationRelativeTo(d);

		setIconImage(UIEnv.getIcon());
		
		textField = new JTextField();
		textField.setPreferredSize(new Dimension(400,80));
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		getContentPane().add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		JButton btnok = new JButton(UIEnv.getRsb().getString("ok"));
		UIEnv.defbut(btnok);
		panel.add(btnok);
		btnok.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				File x=new File(textField.getText());
				if(!checkfile){
					if(isdir){
						x.mkdirs();
					}else{
						try {
							x.createNewFile();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					al.accept(x);
					setVisible(false);
					return;
				}
				if(x.exists()){
				al.accept(x);
				setVisible(false);
				}else{
					JOptionPane.showMessageDialog(PathInput.this, UIEnv.getRsb().getString("filenotexist"), UIEnv.getRsb().getString("error"), JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		JButton btnNewButton_1 = new JButton(UIEnv.getRsb().getString("browse"));
		UIEnv.defbut(btnNewButton_1);
		panel.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(jfc.showOpenDialog(PathInput.this)==jfc.APPROVE_OPTION){
					textField.setText(jfc.getSelectedFile().toString());
				}
				/*File f=choose.get();
				if(f!=null){
					textField.setText(f.toString());
				}*/
			}
		});
		JButton button = new JButton(UIEnv.getRsb().getString("cancel"));
		UIEnv.defbut(button);
		panel.add(button);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		

		jfc=new JFileChooser();
		jfc.setMultiSelectionEnabled(false);
	}


	public JTextField getTextField() {
		return textField;
	}

	public JFileChooser getJfc() {
		return jfc;
	}

	public boolean isCheckfile() {
		return checkfile;
	}
	public void setCheckfile(boolean checkfile) {
		this.checkfile = checkfile;
	}
	public boolean isIsdir() {
		return isdir;
	}
	public void setIsdir(boolean isdir) {
		this.isdir = isdir;
		if(isdir) {
			jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}else {
			jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		}
	}
}
