package br.com.elotech;

import com.vaadin.icons.VaadinIcons;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by elotech on 12/04/17.
 */
public class ConexaoForm extends FormLayout {

    public static String urlOrigem, urlDestino, usuOrigem, usuDestino, senOrigem, senDestino;

    List<Registro> regs;

    TextField servidorOrigem   = new TextField("Servidor"),
              servidorDestino  = new TextField("Servidor"),
              basedadosOrigem  = new TextField("Base de Dados"),
              basedadosDestino = new TextField("Base de Dados"),
              usuarioOrigem    = new TextField("Usuário"),
              usuarioDestino   = new TextField("Usuário");
    PasswordField senhaOrigem  = new PasswordField("Senha"),
                  senhaDestino = new PasswordField("Senha");

    public ConexaoForm(DataGrid dataGrid) {

        servidorOrigem.setRequiredIndicatorVisible(true);
        servidorDestino.setRequiredIndicatorVisible(true);

        basedadosOrigem.setRequiredIndicatorVisible(true);
        basedadosDestino.setRequiredIndicatorVisible(true);

        usuarioOrigem.setRequiredIndicatorVisible(true);
        usuarioDestino.setRequiredIndicatorVisible(true);

        senhaOrigem.setRequiredIndicatorVisible(true);
        senhaDestino.setRequiredIndicatorVisible(true);

        Panel origemPanel = new Panel("Origem (SISCOP)");
        
        HorizontalLayout hl = new HorizontalLayout(servidorOrigem,basedadosOrigem,usuarioOrigem,senhaOrigem);
        origemPanel.setContent(hl);
        origemPanel.setIcon(VaadinIcons.ANCHOR);
        hl.setMargin(true);
        Panel destinoPanel = new Panel("Destino (WebSocialSaude)");
        HorizontalLayout hl1 = new HorizontalLayout(servidorDestino,basedadosDestino,usuarioDestino,senhaDestino);
        hl1.setMargin(true);
        destinoPanel.setContent(hl1);
        destinoPanel.setIcon(VaadinIcons.AMBULANCE);
        Button processarButton = new Button("Processar...");
        processarButton.setIcon(VaadinIcons.START_COG);
        processarButton.setWidth(100, Unit.PERCENTAGE);
        hl.setSpacing(true);
        hl1.setSpacing(true);
        usuarioOrigem.setValue("postgres");
        usuarioDestino.setValue("postgres");
        servidorOrigem.setValue("localhost");
        servidorDestino.setValue("localhost");
        servidorOrigem.focus();
        
        addComponents(origemPanel,destinoPanel,processarButton);

        setSpacing(true);
        setMargin(true);
        
        processarButton.setStyleName(ValoTheme.BUTTON_PRIMARY);

        processarButton.addClickListener(e->{
            processarButton.setEnabled(false);
            regs = null;
            Process p = new Process();
            p.addParameters("jdbc:postgresql://"+servidorOrigem.getValue()+"/"+basedadosOrigem.getValue(),
                    "jdbc:postgresql://"+servidorDestino.getValue()+"/"+basedadosDestino.getValue(),
                    usuarioOrigem.getValue(),
                    usuarioDestino.getValue(),
                    senhaOrigem.getValue(),
                    senhaDestino.getValue());
            urlOrigem = "jdbc:postgresql://"+servidorOrigem.getValue()+"/"+basedadosOrigem.getValue();
            urlDestino = "jdbc:postgresql://"+servidorDestino.getValue()+"/"+basedadosDestino.getValue();
            usuOrigem = usuarioOrigem.getValue();
            usuDestino = usuarioDestino.getValue();
            senOrigem = senhaOrigem.getValue();
            senDestino = senhaDestino.getValue();
            try {

                dataGrid.addParameters("jdbc:postgresql://"+servidorOrigem.getValue()+"/"+basedadosOrigem.getValue(),
                        "jdbc:postgresql://"+servidorDestino.getValue()+"/"+basedadosDestino.getValue(),
                        usuarioOrigem.getValue(),
                        usuarioDestino.getValue(),
                        senhaOrigem.getValue(),
                        senhaDestino.getValue());
                regs = p.processar();
                dataGrid.setItems(regs);

            } catch (SQLException e1) {
                e1.printStackTrace();
                Notification notification = new Notification("ERRO","Erro ao processar:\n"+e1.getMessage());
                notification.show(MyUI.getCurrent().getPage());
                processarButton.setEnabled(true);
            }
            processarButton.setEnabled(true);
        });

    }

    public List<Registro> getRegs() {
        return regs;
    }
}
