import javax.swing.*;
import javax.swing.text.MaskFormatter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class FirmaKayitFormu {
	
    private static JTextField firmaAdiField;
    private static JTextField adSoyadField;
    private static JFormattedTextField cepNumarasiField;
    private static JTextField emailField;
    private static JPasswordField sifreField;
    private static JPasswordField sifreTekrarField;
    private static JComboBox<String> faaliyetAlaniComboBox;

    public FirmaKayitFormu() {
    	
    	JFrame frame = new JFrame("Firma Kayit Formu | Java Blackboard [131]");
    	frame.setLayout(null);
       
        frame.add(formPaneli());
        
        frame.setSize(800, 722);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    // Text Field Formati oluşturmak için metot
    private static MaskFormatter metinFormati(String format) {
    	
        MaskFormatter formatDuzeni = null;
        
        try {
        	
        	formatDuzeni = new MaskFormatter(format);
            
        } catch (java.text.ParseException e) {
        	
            System.err.println("Hatalı formatlama: " + e.getMessage());
            
        }
        
        return formatDuzeni;
    }
    
    public static JPanel formPaneli() {
    	
    	// Logo dosyasının yolu
        String logoYolu = "src/images/firmaLogo.png";

        // ImageIcon oluşturup logo boyutunu ayarlayalim
        ImageIcon logoIcon = new ImageIcon(logoYolu);
        Image logoImage = logoIcon.getImage();
        Image boyutLogo100x100 = logoImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
        ImageIcon scaledIcon = new ImageIcon(boyutLogo100x100);

        // JLabel oluşturup logoyu ekleyelim.
        JLabel logoLabel = new JLabel();
        logoLabel.setIcon(scaledIcon);
        logoLabel.setBounds(50, 25, 100, 100);
        
        JLabel markaLabel = new JLabel();
        markaLabel.setText("FİRMA A.Ş.");
        markaLabel.setFont(new Font("Helvetica", Font.BOLD, 80));
        markaLabel.setBounds(175, 10, 700, 150);
    	
    	JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(new Color(210, 233, 244));
        panel.setBounds(50, 50, 700, 600);

        Font labelFont = new Font("Helvetica", Font.PLAIN, 17);
        Font labelField = new Font("Helvetica", Font.BOLD, 23);
        
        JLabel firmaAdiLabel = new JLabel("Firma Adı:");
        firmaAdiLabel.setFont(labelFont);
        firmaAdiLabel.setBounds(50, 150, 150, 30);
        
        firmaAdiField = new JTextField();
        firmaAdiField.setFont(labelField);
        firmaAdiField.setBounds(250, 150, 400, 50);
        
        JLabel adSoyadLabel = new JLabel("Yetkili Ad Soyad:");
        adSoyadLabel.setFont(labelFont);
        adSoyadLabel.setBounds(50, 200, 150, 30);
        
        adSoyadField = new JTextField();
        adSoyadField.setFont(labelField);
        adSoyadField.setBounds(250, 200, 400, 50);
        
        JLabel cepNumarasiLabel = new JLabel("Cep Numarası:");
        cepNumarasiLabel.setFont(labelFont);
        cepNumarasiLabel.setBounds(50, 250, 150, 30);
        
        cepNumarasiField = new JFormattedTextField(metinFormati(" 0(###) ### ## ##"));
        cepNumarasiField.setFont(labelField);
        cepNumarasiField.setBounds(250, 250, 400, 50);
        
        JLabel emailLabel = new JLabel("E-Mail:");
        emailLabel.setFont(labelFont);
        emailLabel.setBounds(50, 300, 150, 30);
        
        emailField = new JTextField();
        emailField.setFont(labelField);
        emailField.setBounds(250, 300, 400, 50);
        
        JLabel sifreLabel = new JLabel("Şifre:");
        sifreLabel.setFont(labelFont);
        sifreLabel.setBounds(50, 350, 150, 30);
        
        sifreField = new JPasswordField();
        sifreField.setFont(labelField);
        sifreField.setBounds(250, 350, 400, 50);
        
        JLabel sifreTekrarLabel = new JLabel("Şifre Tekrar:");
        sifreTekrarLabel.setFont(labelFont);
        sifreTekrarLabel.setBounds(50, 400, 150, 30);
        
        sifreTekrarField = new JPasswordField();
        sifreTekrarField.setFont(labelField);
        sifreTekrarField.setBounds(250, 400, 400, 50);
        
        JLabel faaliyetAlaniLabel = new JLabel("Ana Faaliyet Alanı:");
        faaliyetAlaniLabel.setFont(labelFont);
        faaliyetAlaniLabel.setBounds(50, 450, 150, 30);
        
        String[] faaliyetAlanlari = { 	"-Seçiniz-", "Gıda", "Turizm", "Otomotiv", "Seyahat", 
					        			"Ulaşım", "Eğitim", "Lojistik", "Tekstil", "Teknoloji", 
					        			"Iletişim", "Haberleşme", "Geri Dönüşüm", "Ilaç", "Eğlence", 
					        			"Finans", "Spor", "Medya", "Yazilim" 
					        		};
					        
        Arrays.sort(faaliyetAlanlari); // Diziyi alfabetik sıraya göre sırala

        faaliyetAlaniComboBox = new JComboBox<>(faaliyetAlanlari);
        faaliyetAlaniComboBox.setFont(labelField);
        faaliyetAlaniComboBox.setBounds(250, 450, 400, 50);
        

        panel.add(logoLabel);
        panel.add(markaLabel);
        
        panel.add(firmaAdiLabel);
        panel.add(firmaAdiField);
        panel.add(adSoyadLabel);
        panel.add(adSoyadField);
        panel.add(cepNumarasiLabel);
        panel.add(cepNumarasiField);
        panel.add(emailLabel);
        panel.add(emailField);
        panel.add(sifreLabel);
        panel.add(sifreField);
        panel.add(sifreTekrarLabel);
        panel.add(sifreTekrarField);
        panel.add(faaliyetAlaniLabel);
        panel.add(faaliyetAlaniComboBox);

        JButton kaydetButon = new JButton("Kaydet");
        kaydetButon.setBounds(250, 500, 200, 50);
        
        kaydetButon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
            	// Verileri aliyoruz.
                String firmaAdi = firmaAdiField.getText();
                String adSoyad = adSoyadField.getText();
                String cepNumarasi = cepNumarasiField.getText();
                String email = emailField.getText();
                char[] sifre = sifreField.getPassword();
                char[] sifreTekrar = sifreTekrarField.getPassword();
                String faaliyetAlani = faaliyetAlaniComboBox.getSelectedItem().toString();

                // Verileri Örnek olarak consol'a yazdıralım:
                System.out.println("Firma Adı: " + firmaAdi);
                System.out.println("Ad Soyad: " + adSoyad);
                System.out.println("Cep Numarası: " + cepNumarasi);
                System.out.println("E-Mail: " + email);
                System.out.println("Şifre: " + new String(sifre)); 	// Şifre char dizisi olarak tutulduğu için 
                													// string'e çeviriyoruz
                System.out.println("Şifre Tekrar: " + new String(sifreTekrar));
                System.out.println("Ana Faaliyet Alanı: " + faaliyetAlani);
            }
        });

        panel.add(kaydetButon);
        
        return panel;

    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
        
        	new FirmaKayitFormu();
            
        });
    }
}
