/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socius.telas.login;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import socius.adapter.Arquivo;
import socius.telas.util.DadosGlobais;
import socius.dispositivos.ComputadorRemoto;
import socius.modelo.Usuario;
import socius.state.EstadoComputador;
import socius.app.ClientBoot;

/**
 * FXML Controller class
 *
 * @author Ana
 */
public class LoginController implements Initializable {

    /**
     * Initializes the controller class.
     */
    private Usuario user;
    private ObservableList<ComputadorRemoto> computadores;
    private ObservableList<Arquivo> arquivos;
    private EstadoComputador estado;
    private Stage stage;
    private ClientBoot redirect;
    
    @FXML
    private Button cancelar;
    @FXML
    private Button cadastrar;
    @FXML
    private Button confirmar;
    @FXML
    private TextField usuario;
    @FXML
    private PasswordField senha;
    
    @FXML
    public void cancelar(){
        usuario.clear();
        senha.clear();
    }
    
    public void fechar(){
    }
    
    @FXML
    public void controleAcesso(ActionEvent e) throws Exception, Throwable{
            user=new Usuario();
            user.setSenha(senha.getText());
            user.setLogin(usuario.getText());
            //fecho a janela de login
            stage = (Stage) confirmar.getScene().getWindow();
            stage.close();
            //tenta conectar computador e retorna estado
            
            // arrumar não esta retornando a tela de login 
            // e abre de qualquer forma a tela principal
            redirect = new DadosGlobais(user).setupClient();
            Stage s = new Stage();
            s.setMinHeight(560);
            s.setMinWidth(780);
            redirect.start(s);
            //abre a janela de conforme a conexao
                
            
    }
    
    @FXML
    public void cadastro(ActionEvent e){
        try {
            Node source=(Node) e.getSource();
            Stage stage=(Stage) source.getScene().getWindow();
            stage.close();

            redirect=new ClientBoot();
            redirect.telaCadastro();
            redirect.start(new Stage());
            
        } catch (Exception ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    
}
