package gerenciamento_pagamento;

import gerenciamento_acesso.ControleAcesso;
import gerenciamento_compra.Principal;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import BD.ConnectionFactory;

public class InterfacePagamento {

	static Shell janela1;
	static Shell W_cadastro, W_login;
	static Text text1, text2, campo0, campo1, campo2, campo3, campo4;
	static List lista1;

	public void SolicitaPagamento() {
		janela1 = new Shell();
		janela1.setSize(700, 700);
		janela1.setText("Lista de debitos");
		janela1.open();

		try {
			Connection con = ConnectionFactory.getConnection();
			con.close();
		} catch (SQLException e) {
			janela1.close();
			MessageBox messageBox = new MessageBox(Principal.shell, SWT.OK
					| SWT.ICON_INFORMATION);
			messageBox.setMessage("SQL nao ativada!");
			messageBox.setText("SQL");
			messageBox.open();
			return;
		}

		Label label1 = new Label(janela1, SWT.NONE);
		label1.setText("Loge no sistema e selecione os itens "
				+ "que voce deseja gerar boleto");
		label1.setBounds(20, 30, 500, 20);

		lista1 = new List(janela1, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL
				| SWT.H_SCROLL);
		lista1.setBounds(50, 50, 500, 500);

		Button volta = new Button(janela1, SWT.NONE);
		volta.setText("Voltar");
		volta.setBounds(100, 600, 100, 20);

		Button login = new Button(janela1, SWT.NONE);
		login.setText("Login");		
		login.setBounds(300, 600, 100, 20);

		Button pagar = new Button(janela1, SWT.NONE);
		pagar.setText("gerar boleto");
		pagar.setBounds(500, 600, 100, 20);

		if (Principal.login != null) {
			Label text2 = new Label(janela1, SWT.NONE);
			text2.setText("Bem vindo " + Principal.login);
			text2.setBounds(20, 10, 100, 20);
		}

		if (Principal.getName() != null) {

			try {
				// verifica se existe primeiro
				Connection con = ConnectionFactory.getConnection();
				String cliente = Principal.getName();
				String aux = "select * from " + cliente;
				PreparedStatement stmt = con.prepareStatement(aux);
				stmt.executeQuery();

				ControlePagamento crtl_pag = new ControlePagamento();
				Fatura debito = crtl_pag.BuscaFatura();
				String[] aux1 = debito.getProduto();
				int[] aux2 = debito.getPreco();
				int[] aux3 = debito.getQtde();
				int cont = 0;
				while (aux1[cont] != null) {
					lista1.add(aux3[cont] + " " + aux1[cont]
							+ "(s) no valor de " + "" + aux2[cont]
							+ " reais cada");
					cont++;
				}

				int valor = 0;
				for (int i = 0; i < cont; i++) {
					valor = valor + (aux2[i] * aux3[i]);
				}

				Label label2 = new Label(janela1, SWT.NONE);
				label2.setText("Valor total dos debitos " + valor + " Reais");
				label2.setBounds(20, 550, 200, 20);

				con.close();
			} catch (SQLException e) {

			}

		}

		SelectionAdapter voltas = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				janela1.dispose();
			}
		};
		SelectionAdapter logs = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				LogaUsuario();

			}
		};
		SelectionAdapter boletos = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {

				if (lista1.getSelectionCount() >= 1) {
					MessageBox messageBox = new MessageBox(janela1, SWT.OK
							| SWT.CANCEL | SWT.ICON_INFORMATION);
					messageBox.setMessage("Tem certeza"
							+ " que deseja imprimir o boleto desses itens?");
					messageBox.setText("Boleto");

					if (messageBox.open() == SWT.OK) {
						MessageBox imp = new MessageBox(janela1, SWT.OK
								| SWT.ICON_INFORMATION);
						imp.setMessage("Enviado para impressora");
						imp.setText("print");
						imp.open();
					}
				}
			}
		};

		volta.addSelectionListener(voltas);
		login.addSelectionListener(logs);
		pagar.addSelectionListener(boletos);

	}

	public void LogaUsuario() {

		W_login = new Shell();
		W_login.setText("Login");
		W_login.setSize(400, 200);
		W_login.open();

		Label label1 = new Label(W_login, SWT.NONE);
		label1.setText("Login");
		label1.setBounds(50, 50, 50, 20);
		Label label2 = new Label(W_login, SWT.NONE);
		label2.setText("Senha");
		label2.setBounds(50, 80, 50, 20);

		text1 = new Text(W_login, SWT.BORDER);
		text1.setBounds(100, 50, 200, 20);
		text1.setTextLimit(8);
		text2 = new Text(W_login, SWT.BORDER);
		text2.setEchoChar('*');
		text2.setBounds(100, 80, 200, 20);
		text2.setTextLimit(5);

		Button botao1 = new Button(W_login, SWT.NONE);
		botao1.setText("Cadastra");
		botao1.setBounds(250, 130, 100, 20);
		Button botao2 = new Button(W_login, SWT.NONE);
		botao2.setText("voltar");
		botao2.setBounds(50, 130, 100, 20);
		Button botao3 = new Button(W_login, SWT.NONE);
		botao3.setText("logar");
		botao3.setBounds(150, 130, 100, 20);

		SelectionAdapter modo0 = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				W_cadastro = new Shell();
				W_cadastro.setText("Cadastro");
				W_cadastro.setSize(500, 400);
				W_cadastro.open();

				Label label0 = new Label(W_cadastro, SWT.NONE);
				label0.setText("Preencha corretamente os campos");
				label0.setBounds(20, 20, 200, 20);
				Label label001 = new Label(W_cadastro, SWT.NONE);
				label001.setText("Nome");
				label001.setBounds(100, 100, 50, 20);
				Label label1 = new Label(W_cadastro, SWT.NONE);
				label1.setText("Login");
				label1.setBounds(100, 100, 50, 20);
				Label label2 = new Label(W_cadastro, SWT.NONE);
				label2.setText("Senha");
				label2.setBounds(100, 130, 50, 20);
				Label label3 = new Label(W_cadastro, SWT.NONE);
				label3.setText("E-mail");
				label3.setBounds(100, 160, 50, 20);
				Label label4 = new Label(W_cadastro, SWT.NONE);
				label4.setText("endereco");
				label4.setBounds(100, 190, 50, 20);

				campo0 = new Text(W_cadastro, SWT.BORDER);
				campo0.setBounds(150, 100, 200, 20);
				campo0.setTextLimit(255);
				campo1 = new Text(W_cadastro, SWT.BORDER);
				campo1.setBounds(150, 100, 200, 20);
				campo1.setTextLimit(8);
				campo2 = new Text(W_cadastro, SWT.BORDER);
				campo2.setBounds(150, 130, 200, 20);
				campo2.setTextLimit(5);
				campo3 = new Text(W_cadastro, SWT.BORDER);
				campo3.setBounds(150, 160, 200, 20);
				campo3.setTextLimit(30);
				campo4 = new Text(W_cadastro, SWT.BORDER);
				campo4.setBounds(150, 190, 200, 20);
				campo4.setTextLimit(20);

				Button botao1 = new Button(W_cadastro, SWT.NONE);
				botao1.setText("voltar");
				botao1.setBounds(100, 250, 100, 20);
				Button botao2 = new Button(W_cadastro, SWT.NONE);
				botao2.setText("cadastrar");
				botao2.setBounds(200, 250, 100, 20);

				SelectionAdapter tipo0 = new SelectionAdapter() {
					public void widgetSelected(SelectionEvent arg0) {
						W_cadastro.dispose();
					}
				};
				SelectionAdapter tipo1 = new SelectionAdapter() {
					public void widgetSelected(SelectionEvent arg0) {

						String aux0 = campo0.getText();
						String aux1 = campo1.getText();
						String aux2 = campo2.getText();
						String aux3 = campo3.getText();
						String aux4 = campo4.getText();

						ControleAcesso novo = new ControleAcesso();
						try {
							if (novo.CadastraCliente(aux0, aux1, aux2, aux3,
									aux4)) {
								MessageBox messageBox = new MessageBox(
										W_cadastro, SWT.OK
												| SWT.ICON_INFORMATION);
								messageBox
										.setMessage("Cadastrado com sucesso!!!");
								messageBox.setText("Cadastro");
								messageBox.open();
								W_cadastro.dispose();

							} else {
								MessageBox messageBox = new MessageBox(
										W_cadastro, SWT.OK
												| SWT.ICON_INFORMATION);
								messageBox.setMessage("Verifique se nao "
										+ "tem campo(s)vazios ou tente outro"
										+ " login");
								messageBox.setText("Cadastro");
								messageBox.open();
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				};

				botao1.addSelectionListener(tipo0);
				botao2.addSelectionListener(tipo1);
			}
		};
		SelectionAdapter modo1 = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				W_login.dispose();
			}
		};
		SelectionAdapter modo2 = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				// verifica sql....
				ControlePagamento loga = new ControlePagamento();

				String aux1 = text1.getText();
				String aux2 = text2.getText();

				if (loga.VerificaLogin(aux1, aux2)) {
					MessageBox messageBox = new MessageBox(W_login, SWT.OK
							| SWT.ICON_INFORMATION);
					messageBox.setMessage("Logado com sucesso!!!");
					messageBox.setText("Login");
					messageBox.open();

					W_login.close();
					janela1.close();
				} else {
					MessageBox messageBox = new MessageBox(W_login, SWT.OK
							| SWT.ICON_INFORMATION);
					messageBox.setMessage("Cliente invalido!");
					messageBox.setText("Login");
					messageBox.open();
				}
			}
		};

		botao1.addSelectionListener(modo0);
		botao2.addSelectionListener(modo1);
		botao3.addSelectionListener(modo2);
	}
}
