package view.note;

import controller.note.Export_Notes_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.layout.border.STitledBorder;
import org.sig.fw.layout.component.*;
import org.sig.fw.layout.contener.SInternalFrame;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.layout.pane.SPanel;
import org.sig.fw.spdf.common.FileTools;
import org.sig.fw.utility.date.SDate;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class Export_Notes_View extends SInternalFrame {
	private static final long serialVersionUID = -9174573988904734580L;
	
	private Export_Notes_Controller ctrl = null;
	private SProgressBar jpb_progress;
	private SSpinner js_min_date;
	private SSpinner js_max_date;
	private STextField jtf_file;

	
	public Export_Notes_View(Export_Notes_Controller ctrl) {
		super("Export Notes");
		this.ctrl = ctrl;
		setBounds(100, 100, 415, 322);
		jp_main_content.setLayout(new MigLayout("", "[grow]", "[][][grow]"));
		
		SPanel panel = new SPanel();
		panel.setBorder(new STitledBorder("Configuration"));
		jp_main_content.add(panel, "cell 0 0,grow");
		panel.setLayout(new MigLayout("insets 0px 0px, gap 10px 5px", "[][grow]", "[][]"));
		
		SLabel label = new SLabel("Du");
		panel.add(label, "cell 0 0");
		
		js_min_date = new SSpinner(SSpinner.TYPE_LITTLE_DATE, new Date(), null, null, null);
		panel.add(js_min_date, "cell 1 0,growx");
		
		SLabel label_1 = new SLabel("Au");
		panel.add(label_1, "cell 0 1");
		
		js_max_date = new SSpinner(SSpinner.TYPE_LITTLE_DATE, new Date(), null, null, null);
		panel.add(js_max_date, "cell 1 1,growx");
		
		SPanel panel_1 = new SPanel();
		panel_1.setBorder(new STitledBorder("Enregistrement"));
		jp_main_content.add(panel_1, "cell 0 1,grow");
		panel_1.setLayout(new MigLayout("insets 0px 0px", "[][grow][]", "[]"));
		
		SLabel lblNewLabel = new SLabel("Fichier");
		panel_1.add(lblNewLabel, "cell 0 0,alignx trailing");
		
		jtf_file = new STextField();
		panel_1.add(jtf_file, "cell 1 0,growx");
		
		SButton btnNewButton = new SButton(SButton.TYPE_SHOW_EXPLORER);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String str = FileTools.pathChoose("", "xlsx");
				if(!str.isEmpty()) {
					jtf_file.setText(str);
				}
			}
		});
		panel_1.add(btnNewButton, "cell 2 0");
		
		SPanel panel_2 = new SPanel();
		panel_2.setBorder(new STitledBorder("Validation"));
		jp_main_content.add(panel_2, "cell 0 2,grow");
		panel_2.setLayout(new MigLayout("insets 0px 0px", "[grow]", "[][grow]"));
		
		jpb_progress = new SProgressBar();
		jpb_progress.setMaximum(100);
		jpb_progress.setMinimum(0);
		panel_2.add(jpb_progress, "cell 0 0,growx");
		
		SButton btnNewButton_1 = new SButton(SButton.TYPE_FORM_VALIDATE);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				validation();
			}
		});
		panel_2.add(btnNewButton_1, "cell 0 1,alignx center");
	}
	
	public void changeProgressBar(int value, String text) {
		this.jpb_progress.setValue(text, value);
	}
	
	private void validation() {
		SDate min_date = new SDate((Date)this.js_min_date.getValue());
		SDate max_date = new SDate((Date)this.js_max_date.getValue());
		String file_url = this.jtf_file.getText();
		
		if(file_url.isEmpty()) {
			SOptionPane.showErrorMessage("Export", "Veuillez reiseigner un chemin d'enregistrement !");
			return;
		}
		
		if(min_date.compareTo(max_date) > 0) {
			SOptionPane.showErrorMessage("Export", "La date 'du' doit être inférieur à la date 'au'");
			return;
		}
		ctrl.compute(min_date, max_date, file_url);
	}

}
