package gui.panels.edit.dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.*;

import agenda.Act;
import agenda.ActTime;
import agenda.Agenda;
import agenda.Artist;
import agenda.Stage;
import javafx.scene.control.RadioButton;

/**
 * Created by gjoosen on 20/02/15.
 */
public class ActDialogPanel extends JDialog{

    private Agenda agenda;
    private JTextField name, genre, beginTime, endTime;
    private JComboBox stageComboBox;
    private JRadioButton radio1, radio2, radio3, radio4, radio5;
    private ButtonGroup radioButtons;
    
    private DefaultListModel model;
    private final DefaultListModel  artistModel = new DefaultListModel();
    private Act act;
    
    public ActDialogPanel(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("New Act");
        label.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 14));
        main.add(label);
        main.add(this.namePanel());
        //add
        main.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        main.add(this.stageChooser());
        main.add(this.genreChooser());
        //todo dates
        main.add(this.dates());
        main.add(this.popularity());
        main.add(this.artistsChooser());
        
        main.add(this.buttons());
        super.add(main);
        super.setVisible(true);
        super.pack();
    }

    public ActDialogPanel(Agenda agenda, DefaultListModel model, Act act){
        this.agenda = agenda;
        this.model = model;
        this.act = act;
        this.getContentPane().setBackground( Color.WHITE );
        
        JPanel main = new JPanel();
        main.setOpaque(false);
        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
        Label label = new Label("Edit Act");
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        main.add(label);
        main.add(this.namePanel());
        this.name.setText(act.getName());
        //add
        main.add(this.stageChooser(act));
        main.add(this.genreChooser());
        this.genre.setText(this.act.getGenre());
        //todo dates
        main.add(this.dates());

        for(Artist artist: this.act.getArtists()){
            this.artistModel.addElement(artist);
        }
        main.add(this.popularity(act.getPopularity()));
        main.add(this.artistsChooser());
        main.add(this.buttons());
        
        //set time
        this.beginTime.setText(this.act.getActTime().getBeginTimeStringField());
        this.endTime.setText(this.act.getActTime().getEndTimeStringField());
        super.add(main);
    }
    
    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.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        name.add(label);
        this.name = new JTextField();

        name.add(this.name);
        return name;
    }

    private JPanel stageChooser(){
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        this.stageComboBox = new JComboBox();
        for(Stage stage: this.agenda.getStages()){
            this.stageComboBox.addItem(stage);
        }
        
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        Label label = new Label("Stage:");
        label.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        panel.add(label);
        panel.add(this.stageComboBox);        
        
        return panel;
    }

    private JPanel stageChooser(Act act){
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        this.stageComboBox = new JComboBox();
        this.stageComboBox.addItem(act.getStage());
        for(Stage stage: this.agenda.getStages()){
            if(act.getStage() != stage){
                this.stageComboBox.addItem(stage);
            }
        }
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        Label label = new Label("Stage:");
        label.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        panel.add(label);
        panel.add(this.stageComboBox);

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

        genrePanel.add(this.genre);
        return genrePanel;
    }
    
    private JPanel dates(){
        JPanel dates = new JPanel();
        dates.setOpaque(false);
        dates.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        dates.setLayout(new BoxLayout(dates, BoxLayout.Y_AXIS));
        dates.add(this.beginTime());
        dates.add(this.endTime());
        return dates;
    }
    
    private JPanel beginTime(){
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        Label label = new Label("Begintijd");
        label.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        panel.add(label);
        this.beginTime = new JTextField("2015-02-11-12-10");
        panel.add(this.beginTime);
        return panel;
    }
    
    private JPanel endTime(){
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        Label label = new Label("Eindtijd");
        label.setBackground(Color.WHITE);
        label.setFont(new Font("Arial", Font.PLAIN, 11));
        panel.add(label);
        this.endTime = new JTextField("2015-02-11-13-20");
        panel.add(this.endTime);
        
        return panel;
    }
    
    private JPanel popularity(){
        return this.popularity(3);
    }

    private JPanel popularity(int number){
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("popularity"));

        JPanel buttons = new JPanel();
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));

        this.radio1 = new JRadioButton("1");
        this.radio2 = new JRadioButton("2");
        this.radio3 = new JRadioButton("3");
        this.radio4 = new JRadioButton("4");
        this.radio5 = new JRadioButton("5");
        
        switch(number){
            case 1:
                this.radio1.setSelected(true);
                break;
            case 2:       
                this.radio2.setSelected(true);
                break;
            case 3:
                this.radio3.setSelected(true);
                break;
            case 4:
                this.radio4.setSelected(true);
                break;
            case 5:
                this.radio5.setSelected(true);
                break;
        }

        buttons.add(this.radio1);
        buttons.add(this.radio2);
        buttons.add(this.radio3);
        buttons.add(this.radio4);
        buttons.add(this.radio5);

        this.radioButtons = new ButtonGroup();
        this.radioButtons.add(this.radio1);
        this.radioButtons.add(this.radio2);
        this.radioButtons.add(this.radio3);
        this.radioButtons.add(this.radio4);
        this.radioButtons.add(this.radio5);

        panel.add(buttons);
        return panel;
    }
    
    private JPanel artistsChooser(){
        JPanel artists = new JPanel();
        artists.setOpaque(false);
        artists.setLayout(new BoxLayout(artists, BoxLayout.X_AXIS));
        artists.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        //list
        final JList<Act> list = new JList();
        list.setCellRenderer(new ArtistCellRenderer());
        
        list.setModel(artistModel);
        
        JScrollPane scrollPane = new JScrollPane(list);
        artists.add(scrollPane);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        
        JLabel addArtists = new JLabel();
        addArtists.setIcon(new ImageIcon(getClass().getResource("/add.png")));
        addArtists.setForeground(new Color(51,51,51));
        addArtists.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				boolean chosen = false;
                
                //artists 
                ArrayList listArtists = new ArrayList<Artist>();
                //for every artist in the agenda
                for(Artist artistAgenda: agenda.getArtists()){
                    
                    System.out.println("Artist: " + artistAgenda);
                    
                    //for every artist in the model.
                    for(Object artistObject: artistModel.toArray()){
                        Artist artistModel = (Artist) artistObject;
                        
                        System.out.println("Vergelijk: " + artistModel + " - " + artistAgenda);
                        
                        if(artistModel == artistAgenda) {
                            System.out.println("True: " + artistModel + " - " + artistAgenda);
                            chosen = true;
                        }                    
                    }
                    if(!chosen){
                        System.out.println(artistAgenda);
                        listArtists.add(artistAgenda);
                    }
                    chosen  = false;
                }

                Artist[] artist = new Artist[listArtists.size()];
                
                if(listArtists.size() == 0){
                    JOptionPane.showMessageDialog(null, "No artists to choose!");
                    return;
                }
                
                for(int i = 0; i<listArtists.size(); i++){
                    artist[i] = (Artist) listArtists.get(i);
                }
                Artist chosenArtist = (Artist) JOptionPane.showInputDialog(null, "add artist", "add artist", JOptionPane.PLAIN_MESSAGE, null, artist ,null);
                if(chosenArtist != null){
                    artistModel.addElement(chosenArtist);
                }
            
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}
		});
        
        JLabel removeButton = new JLabel();
        removeButton.setIcon(new ImageIcon(getClass().getResource("/remove.png")));
        removeButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(list.getSelectedValue() != null){
                    Object object = artistModel.remove(list.getSelectedIndex());
			}}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}
			});
        buttonPanel.add(addArtists);
        buttonPanel.add(removeButton);
        artists.add(buttonPanel);
        return artists;
    }

    
    
    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;
    }
    
    private void save(){

        Artist[] artists = new Artist[this.artistModel.size()];
        for(int i = 0; i < artistModel.size(); i++){
            artists[i] = (Artist) artistModel.get(i);
        }

        if(this.name.getText().equals("")){
            JOptionPane.showMessageDialog(null, "A name is required!");
            return;
        }else if(this.genre.getText().equals("")){
            JOptionPane.showMessageDialog(null, "A genre is required!");
            return;
        }else if(this.stageComboBox.getSelectedItem() == null){
            JOptionPane.showMessageDialog(null, "A stage is required!");
            return;
        }else if(artists.length == 0){
            JOptionPane.showMessageDialog(null, "At least 1 artist is required!");
            return;
        }
        
        
        if(this.act == null){
            Act act = new Act(this.name.getText(), (Stage) this.stageComboBox.getSelectedItem(), this.genre.getText(), new ActTime(this.beginTime.getText(), this.endTime.getText()), this.getSelectedButtonNumber() ,artists);
            this.model.addElement(act);
            this.agenda.addAct(act);
        }else{
            this.act.setName(this.name.getText());
            this.act.setGenre(this.genre.getText());
            this.act.setStage((Stage) this.stageComboBox.getSelectedItem());
            this.act.setArtists(artists);
            this.act.setTime(new ActTime(this.beginTime.getText(), this.endTime.getText()));
            this.act.setPopularity(this.getSelectedButtonNumber());
            this.act.setActTime(new ActTime(this.beginTime.getText(), this.endTime.getText()));
            this.model.removeElement(this.act);
            this.model.addElement(act);
        }
        System.out.println(act);
        dispose();
    }

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

        Enumeration<AbstractButton> buttons = this.radioButtons.getElements();
        
        while(buttons.hasMoreElements()){
            AbstractButton button = buttons.nextElement();
            if(button.isSelected()){
                return Integer.valueOf(button.getText());
            }
        }
        return -1;
    }
}

class ArtistCellRenderer extends JLabel implements ListCellRenderer {

    private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);

    public ArtistCellRenderer() {
        setOpaque(true);
        setIconTextGap(12);
    }

    public Component getListCellRendererComponent(JList list, Object value,
                                                  int index, boolean isSelected, boolean cellHasFocus) {
        Artist stage = (Artist) value;
        setText(stage.getName());
        if (isSelected) {
            setBackground(HIGHLIGHT_COLOR);
            setForeground(Color.white);
        } else {
            setBackground(Color.white);
            setForeground(Color.black);
        }
        return this;
    }
}

