package view.export;

import controller.export.Export_Factures_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.db.bddc.model.Society;
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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Export_Factures_View extends SInternalFrame 
{
	private static final long serialVersionUID = -1885300631621348729L;
	
	private Export_Factures_Controller ctrl_export_factures;
	
	private SComboBox jcb_society;
	private STextField jtf_url;
	private SProgressBar jpb_progress;
	private SButton jb_validate;
	
	public Export_Factures_View(Export_Factures_Controller ctrl_export_factures) {
		super("Export des factures");
		setResizable(false);
		setMaximizable(false);
		this.ctrl_export_factures = ctrl_export_factures;
		setBounds(100, 100, 413, 270);
		jp_main_content.setLayout(new MigLayout("insets 0px 0px", "[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", "[][grow]", "[]"));
		
		SLabel label_1 = new SLabel("Soci\u00E9t\u00E9", 0);
		panel.add(label_1, "cell 0 0,alignx left");
		
		jcb_society = new SComboBox();
		jcb_society.addItem(new Society(1, "Sondefor"));
		jcb_society.addItem(new Society(2, "SBF"));
		jcb_society.setSelectedIndex(0);
		panel.add(jcb_society, "cell 1 0,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 label = new SLabel("Fichier", 0);
		panel_1.add(label, "cell 0 0,alignx trailing");
		
		jtf_url = new STextField();
		panel_1.add(jtf_url, "cell 1 0,growx");
		
		SButton btnNewButton = new SButton(SButton.TYPE_SHOW_EXPLORER) {
			private static final long serialVersionUID = 1601324197149609171L;
			@Override
			public void openExplorer() {
				String url = FileTools.pathChoose("", "export.xlsx", "xlsx");
				jtf_url.setText(url);
			}
			
		};
		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();
		panel_2.add(jpb_progress, "cell 0 0,growx");
		
		jb_validate = new SButton(SButton.TYPE_FORM_VALIDATE);
		jb_validate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startProcess();
			}
		});
		panel_2.add(jb_validate, "cell 0 1,alignx center");

	}

	public void startProcess() {
		jpb_progress.setValue("", 0);
		jb_validate.setEnabled(false);
		String url = jtf_url.getText();
		if(!url.endsWith(".xlsx")) {
			url += ".xlsx";
		}
		ctrl_export_factures.startProcess((Society) jcb_society.getSelectedItem(), url);
	}
	
	public void setProgressValue(String text, int value) {
		jpb_progress.setValue(text, value);
	}
	
	public void stopProcess(String error) {
		if(!error.isEmpty()) {
			SOptionPane.showErrorMessage("Export", error);
			jpb_progress.setValue("", 0);
		}
		jb_validate.setEnabled(true);
	}
}
