package jguard.client.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jguard.client.Config;

public class SettingsPanel extends JPanel {
    
    private JTextField serverText = new JTextField();
    private JTextField portText = new JTextField();
    private JTextField userText = new JTextField();
    private JPasswordField  passwordText = new JPasswordField();
    private JCheckBox autoConnect = new JCheckBox("Automaticky přihlasit při spuštění");
    private JCheckBox runMinimalized = new JCheckBox("Spouštět minimalizovaně");
    private JCheckBox enableDebug = new JCheckBox("Debug režim");
    private JCheckBox enableSounds = new JCheckBox("Povolit zvuky");
    private JSlider volumeSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);

    public SettingsPanel() {
        
        try {
            serverText.setText(new Config().get("server"));
            portText.setText(new Config().get("port"));
            userText.setText(new Config().get("user"));
            passwordText.setText(new Config().get("password"));
            autoConnect.setSelected((new Config().get("autoconnect").equals("1")? true : false));
            runMinimalized.setSelected((new Config().get("start_minimized").equals("1")? true : false));
            enableDebug.setSelected((new Config().get("debug").equals("1")? true : false));
            enableSounds.setSelected((new Config().get("sound").equals("1")? true : false));
            volumeSlider.setValue(Integer.valueOf(new Config().get("volume")));
        } catch(Exception e){
            System.err.println("Nepodařilo se nahrat nastaveni");
        }
        
        this.setLayout(null);

        JPanel p1 = loginSettings();
        p1.setBounds(20, 10, 400, 170);
        this.add(p1);
        
        JPanel p2 = settings();
        p2.setBounds(20, getNextY(p1), 400, 150);
        this.add(p2);
        
        JPanel p3 = soundSettings();
        p3.setBounds(20, getNextY(p2), 400, 100);
        this.add(p3);
        
        
        JButton b = new JButton("Uložit");
        b.setBounds(20, getNextY(p3), 100, 30);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Properties prop = new Properties();
                prop.setProperty("server", serverText.getText());
                prop.setProperty("port", portText.getText());
                prop.setProperty("user", userText.getText());
                prop.setProperty("password", new String(passwordText.getPassword()));
                prop.setProperty("autoconnect", (autoConnect.isSelected() ? "1" : "0"));
                prop.setProperty("start_minimized", (runMinimalized.isSelected() ? "1" : "0"));
                prop.setProperty("debug", (enableDebug.isSelected() ? "1" : "0"));
                prop.setProperty("sound", (enableSounds.isSelected() ? "1" : "0"));
                prop.setProperty("volume", String.valueOf(volumeSlider.getValue()));
                
                new Config().store(prop);
            }
        });
        this.add(b);
        
    }
    
    private int getNextY(JPanel p){
        return p.getY() + p.getHeight() + 30;
    }
    
    private JPanel loginSettings(){
        JPanel panel = new JPanel(null);
        
        JLabel nadpisLabel = new JLabel("Server");
        nadpisLabel.setBounds(0, 0, 80, 25);
        nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); 
        panel.add(nadpisLabel);
        
        JLabel serverLabel = new JLabel("Adresa");
        serverLabel.setBounds(0, 40, 80, 25);
        panel.add(serverLabel);

        serverText.setBounds(110, 40, 160, 25);
        panel.add(serverText);

        JLabel portLabel = new JLabel("Port");
        portLabel.setBounds(0, 70, 80, 25);
        panel.add(portLabel);

        portText.setBounds(110, 70, 160, 25);
        panel.add(portText);
        
        JLabel userLabel = new JLabel("Uživatel");
        userLabel.setBounds(0, 100, 80, 25);
        panel.add(userLabel);

        userText.setBounds(110, 100, 160, 25);
        panel.add(userText);
        
        JLabel passwordLabel = new JLabel("Heslo");
        passwordLabel.setBounds(0, 130, 80, 25);
        panel.add(passwordLabel);

        passwordText.setBounds(110, 130, 160, 25);
        panel.add(passwordText);
        
        return panel;
    }
    
    private JPanel settings(){
        JPanel panel = new JPanel(null);
        
        JLabel nadpisLabel = new JLabel("Obecné nastavení");
        nadpisLabel.setBounds(0, 0, 400, 25);
        nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); 
        panel.add(nadpisLabel);
        
        autoConnect.setBounds(0, 40, 400, 25);
        panel.add(autoConnect);
        
        runMinimalized.setBounds(0, 70, 400, 25);
        panel.add(runMinimalized);
        
        enableDebug.setBounds(0, 100, 400, 25);
        panel.add(enableDebug);
        
        return panel;
    } 
    
    private JPanel soundSettings(){
        JPanel panel = new JPanel(null);
        
        JLabel nadpisLabel = new JLabel("Zvuky");
        nadpisLabel.setBounds(0, 0, 400, 25);
        nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); 
        panel.add(nadpisLabel);
        
        enableSounds.setBounds(0, 40, 400, 25);
        panel.add(enableSounds);
        
        JLabel hlasLabel = new JLabel("Hlasitost");
        hlasLabel.setBounds(0, 70, 80, 25);
        panel.add(hlasLabel);
        
        JLabel sliderValue = new JLabel(volumeSlider.getValue() + " %");
        sliderValue.setBounds(240, 70, 50, 25);
        panel.add(sliderValue);
        
        volumeSlider.setBounds(110, 70, 110, 25);
        volumeSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                sliderValue.setText(volumeSlider.getValue() + " %");
            }
        });
        panel.add(volumeSlider);
        
        return panel;
    }    
        

}
