/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lembredio;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyVetoException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author elrafa
 */
public class CadastroInterface extends javax.swing.JInternalFrame {
    Login vlogin = new Login();
    Pessoa plogin = new Pessoa();
    Médico medico = new Médico();
    Farmaceutico farma = new Farmaceutico();
    /**
     * Creates new form CadastroInternal
     */
    public CadastroInterface() throws PropertyVetoException {
        this.vlogin.type = -1;
        initComponents();
        setVisible(true);
        RadioButtonHandler handler = new RadioButtonHandler();
        jRadioButtonPaciente.addItemListener(handler);
        jRadioButtonMedico.addItemListener(handler);
        jRadioButtonFarmaceutico.addItemListener(handler);
        
        jLabel4.setVisible(false);
        CRM.setVisible(false);
        LabelFarmacia.setVisible(false);
        NomeFarmacia.setVisible(false);   
    }
    public void cadastroInfo() throws IOException{
        if(vlogin.verificaCadastro() && plogin.verificaEmail()){
                      
        FileWriter outputfile = new FileWriter("CADASTRADOS.txt");
        PrintWriter out = new PrintWriter(outputfile);
        out.flush();
        out.close();
        }else{
            
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        EfetuarCadastro = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jRadioButtonMedico = new javax.swing.JRadioButton();
        jRadioButtonPaciente = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        loginDesejado = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        SENHA = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        CRM = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        nomePessoa = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        jRadioButtonFarmaceutico = new javax.swing.JRadioButton();
        NomeFarmacia = new javax.swing.JTextField();
        LabelFarmacia = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        setTitle("Cadastro");
        setToolTipText("");
        setMaximumSize(new java.awt.Dimension(665, 544));
        setMinimumSize(new java.awt.Dimension(665, 544));
        setPreferredSize(new java.awt.Dimension(665, 544));

        EfetuarCadastro.setText("Continuar");
        EfetuarCadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EfetuarCadastroActionPerformed(evt);
            }
        });

        jLabel6.setText("O que você é para com o aplicativo?");

        buttonGroup1.add(jRadioButtonMedico);
        jRadioButtonMedico.setText("Médico");

        buttonGroup1.add(jRadioButtonPaciente);
        jRadioButtonPaciente.setText("Paciente");

        jLabel2.setText("Digite o login desejado:");

        jLabel3.setText("Digite a senha:");

        jLabel4.setText("Digite seu CRM");

        jLabel5.setText("Digite seu nome");

        jLabel7.setText("Digite seu e-mail");

        buttonGroup1.add(jRadioButtonFarmaceutico);
        jRadioButtonFarmaceutico.setText("Farmacêutico");

        LabelFarmacia.setText("Farmácia");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(159, 159, 159)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(loginDesejado, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(SENHA, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(NomeFarmacia, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CRM, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(LabelFarmacia)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButtonPaciente)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButtonMedico)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButtonFarmaceutico))
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(nomePessoa, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(EfetuarCadastro)
                .addGap(96, 96, 96))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonPaciente)
                    .addComponent(jRadioButtonMedico)
                    .addComponent(jRadioButtonFarmaceutico))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nomePessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(loginDesejado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(SENHA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(LabelFarmacia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NomeFarmacia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CRM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EfetuarCadastro)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void EfetuarCadastroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EfetuarCadastroActionPerformed
          try {
            
                plogin.nome = nomePessoa.getText();
                plogin.email = email.getText();
                vlogin.setSenha(SENHA.getText());
                vlogin.setLogin(loginDesejado.getText());
                if(vlogin.type == 1) medico.setCRM(Integer.parseInt(CRM.getText()));
                if(vlogin.type == 2) farma.setnomeFarmacia(NomeFarmacia.getText());
                
            boolean verifica = plogin.verificaNome();
            //System.out.println(plogin.nome + "-" + plogin.email +"- "+ vlogin.login);
            if(vlogin.verificaCadastro() && plogin.verificaEmail() && vlogin.type != -1 && loginDesejado.getText() != "" && verifica){
               if(vlogin.type==2) farma.verificaFarmacia();
               swapFile("CADASTRADOS.txt"); 
               this.nomePessoa.setText("");
               this.email.setText("");
               this.loginDesejado.setText("");
               this.SENHA.setText("");
               if(vlogin.type == 1) this.CRM.setText("");
              
             JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
             setVisible(false);
             
             getParent().add(new LoginInterface());
             getParent().remove(this);
            }else if(vlogin.verificaCadastro() == false){
                JOptionPane.showMessageDialog(null, "Login já existe\nTente novamente!");
                    }
            else if(vlogin.type == -1){
                JOptionPane.showMessageDialog(null, "Escolha uma opção!!");
            }else if(!verifica){
                JOptionPane.showMessageDialog(null,"Insira caracteres válidos!!");
            }
            else
                    JOptionPane.showMessageDialog(null,  "E-mail já existe\nTente novamente!");// TODO add your handling code here:
        } catch (IOException ex) {
            Logger.getLogger(CadastroInterface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CadastroInterface.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_EfetuarCadastroActionPerformed

    public class RadioButtonHandler implements ItemListener{
        @Override
        
        public void itemStateChanged (ItemEvent evt){
                    
                      

        if(evt.getSource()== jRadioButtonPaciente){
            jLabel4.setVisible(false);
            CRM.setVisible(false);
            LabelFarmacia.setVisible(false);
            NomeFarmacia.setVisible(false);     
            vlogin.type = 0;
            
        }else if(evt.getSource()== jRadioButtonMedico){
            jLabel4.setVisible(true);
            CRM.setVisible(true);
            LabelFarmacia.setVisible(false);
            NomeFarmacia.setVisible(false);  
            vlogin.type = 1;
        }else if(evt.getSource() == jRadioButtonFarmaceutico){
            jLabel4.setVisible(false);
            CRM.setVisible(false);
            LabelFarmacia.setVisible(true);
            NomeFarmacia.setVisible(true);
            vlogin.type = 2;
            
           
        } 
        
        }
    }
    
    public void swapFile(String file1) throws FileNotFoundException, IOException{
                File file = new File(file1);
                if(!file.exists()) file.createNewFile();
                FileWriter outputfile = new FileWriter(file1, true);
                PrintWriter out = new PrintWriter(outputfile);

                    out.println(vlogin.type);
                    out.println(vlogin.login);
                    out.println(vlogin.senha);
                    out.println(plogin.nome);
                    out.println(plogin.email);
                    if(vlogin.type==1) out.println(medico.getCRM());
                    if(vlogin.type==2) out.println(farma.getnomeFarmacia());
                    

              out.close();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CRM;
    private javax.swing.JButton EfetuarCadastro;
    private javax.swing.JLabel LabelFarmacia;
    private javax.swing.JTextField NomeFarmacia;
    private javax.swing.JPasswordField SENHA;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField email;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JRadioButton jRadioButtonFarmaceutico;
    private javax.swing.JRadioButton jRadioButtonMedico;
    private javax.swing.JRadioButton jRadioButtonPaciente;
    private javax.swing.JTextField loginDesejado;
    private javax.swing.JTextField nomePessoa;
    // End of variables declaration//GEN-END:variables
}