package gui.panels.edit.dialogs;

import agenda.Agenda;
import agenda.Artist;

import javax.swing.*;

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

/**
 * Created by gjoosen on 19/02/15.
 */
public class ArtistDialogPanel extends JDialog{

    private JTextField name, genre;
    private Agenda agenda;

    private DefaultListModel model;
    
    private Artist artist;

    public ArtistDialogPanel(Agenda agenda, DefaultListModel model){
        this.agenda = agenda;
        this.model = model;
        this.getContentPane().setBackground( Color.WHITE );
        
        JPanel main = new JPanel();
        main.setOpaque(false);
        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
        Label label = new Label("Begintijd");
        label.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 14));
        main.add(label);
        main.add(this.namePanel());
        main.add(this.genrePanel());
        main.add(this.buttons());
        main.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        super.add(main);
        super.setVisible(true);
        super.pack();
    }
    
    public ArtistDialogPanel(Agenda agenda, DefaultListModel model, Artist artist){
        this.agenda = agenda;
        this.model = model;
        this.artist = artist;
        this.getContentPane().setBackground( Color.WHITE );
        JPanel main = new JPanel();
        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
        main.setOpaque(false);
        Label label = new Label("Edit artist");
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        main.add(label);
        main.add(this.namePanel());
        main.add(this.genrePanel());
        main.add(this.buttons());
        label.setBackground(Color.WHITE);
        this.name.setText(this.artist.getName());
        this.genre.setText(this.artist.getGenre());

        super.add(main);
    }

    private JPanel genrePanel(){
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        Label label = new Label("Genre");
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        panel.add(label);
        label.setBackground(Color.WHITE);
        this.genre = new JTextField();
        panel.add(this.genre);
        return panel;
    }

    private JPanel namePanel(){
        JPanel name = new JPanel();
        name.setOpaque(false);
        name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));
        name.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
        Label label = new Label("Name");
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        name.add(label);
        label.setBackground(Color.WHITE);
        this.name = new JTextField();
        
        name.add(this.name);
        return name;
    }

    private JPanel buttons(){
        JPanel buttons = new JPanel();
        buttons.setOpaque(false);
        JButton save = new JButton("Save");
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                save();
            }
        });
        buttons.add(save);

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cancel();
            }
        });

        buttons.add(cancel);

        return buttons;
    }

    protected void save(){
        if(this.name.getText().equals("") && this.genre.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Name and Genre can't be empty!", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }else if(this.genre.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Genre can't be empty!", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }else if(this.name.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Name can't be empty!", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        //Save artist

        if(this.artist == null){
            Artist artist = new Artist(this.name.getText(), this.genre.getText());
            this.model.addElement(artist);
            this.agenda.addArtist(artist);
        }else{
            this.model.removeElement(this.artist);
            this.artist.setName(this.name.getText());
            this.artist.setGenre(this.genre.getText());
            this.model.addElement(this.artist);
        }
        super.dispose();
    }

    private void cancel(){
        dispose();
    }
}