import java.io.*;
import java.io.File;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;

public class Controls extends JPanel implements Observer, Serializable
{
  private Model model;
  private JRadioButton grid;
  private JRadioButton list;
  private class customListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == grid)
      {
        grid.setSelected(true);
        list.setSelected(false);
        model.switchLayout(true);
        //grid.setIcon(lightGridIcon);
        grid.setBorderPainted(true);
        list.setBorderPainted(false);
      }
      else
      {
        grid.setSelected(false);
        list.setSelected(true);
        model.switchLayout(false);
        //list.setIcon(lightListIcon);
        list.setBorderPainted(true);
        grid.setBorderPainted(false);
      }
    }
  }
  private class superListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == star1)
      {
        if (!(star1.isSelected()) || model.getFilter() == 1)
        {
          star1.setSelected(false);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(emptyStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(0);
        }
        else
        {
          star1.setSelected(true);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(fullStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(1);
        }
      }
      else if (e.getSource() == star2)
      {
        if (!(star2.isSelected()) || model.getFilter() == 2)
        {
          star1.setSelected(false);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(emptyStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(0);
        }
        else
        {
          star1.setSelected(true);
          star2.setSelected(true);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(fullStarIcon);
          star2.setIcon(fullStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(2);
        }
      }
      else if (e.getSource() == star3)
      {
        if (!(star3.isSelected()) || model.getFilter() == 3)
        {
          star1.setSelected(false);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(emptyStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(0);
        }
        else
        {
          star1.setSelected(true);
          star2.setSelected(true);
          star3.setSelected(true);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(fullStarIcon);
          star2.setIcon(fullStarIcon);
          star3.setIcon(fullStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(3);
        }
      }
      else if (e.getSource() == star4)
      {
        if (!(star4.isSelected()) || model.getFilter() == 4)
        {
          star1.setSelected(false);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(emptyStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(0);
        }
        else
        {
          star1.setSelected(true);
          star2.setSelected(true);
          star3.setSelected(true);
          star4.setSelected(true);
          star5.setSelected(false);
          star1.setIcon(fullStarIcon);
          star2.setIcon(fullStarIcon);
          star3.setIcon(fullStarIcon);
          star4.setIcon(fullStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(4);
        }
      }
      else
      {
        if (!(star5.isSelected()) || model.getFilter() == 5)
        {
          star1.setSelected(false);
          star2.setSelected(false);
          star3.setSelected(false);
          star4.setSelected(false);
          star5.setSelected(false);
          star1.setIcon(emptyStarIcon);
          star2.setIcon(emptyStarIcon);
          star3.setIcon(emptyStarIcon);
          star4.setIcon(emptyStarIcon);
          star5.setIcon(emptyStarIcon);
          model.changeFilter(0);
        }
        else
        {
          star1.setSelected(true);
          star2.setSelected(true);
          star3.setSelected(true);
          star4.setSelected(true);
          star5.setSelected(true);
          star1.setIcon(fullStarIcon);
          star2.setIcon(fullStarIcon);
          star3.setIcon(fullStarIcon);
          star4.setIcon(fullStarIcon);
          star5.setIcon(fullStarIcon);
          model.changeFilter(5);
        }
      }
    }
  }
  private customListener customlistener;
  private superListener superlistener;
  private ButtonGroup layouts;
  private ButtonGroup stars;
  private Image emptyStarImg;
  private Image fullStarImg;
  private Image gridImg;
  private Image listImg;
  private Image lightGridImg;
  private Image lightListImg;
  private Image folderImg;
  private ImageIcon emptyStarIcon;
  private ImageIcon fullStarIcon;
  private ImageIcon gridIcon;
  private ImageIcon listIcon;
  private ImageIcon lightGridIcon;
  private ImageIcon lightListIcon;
  private ImageIcon folderIcon;
  private JButton fileButton;
  private JRadioButton star1;
  private JRadioButton star2;
  private JRadioButton star3;
  private JRadioButton star4;
  private JRadioButton star5;
  private JFileChooser fileChooser;

  // Bob the Builder this shit
  public Controls(Model model)
  {
    this.model = model;
    this.customlistener = new customListener();
    this.superlistener = new superListener();
    this.setBackground(Color.white);
    this.setLayout(new BorderLayout(0 , 0));
    this.setPreferredSize(new Dimension(1280,75));
    this.setMinimumSize(new Dimension(320, 75));
    
    try
    {
      this.fullStarImg = ImageIO.read(new File("src/main/resources/fullStar.png"));
    } catch(IOException e) {}
    
    try
    {
      this.emptyStarImg = ImageIO.read(new File("src/main/resources/emptyStar.png"));
    } catch(IOException e) {}

    try
    {
      this.gridImg = ImageIO.read(new File("src/main/resources/grid.png"));
    } catch(IOException e) {}

    try
    {
      this.listImg = ImageIO.read(new File("src/main/resources/list.png"));
    } catch(IOException e) {}
    
    try
    {
      this.lightGridImg = ImageIO.read(new File("src/main/resources/invertedgrid.png"));
    } catch(IOException e) {}

    try
    {
      this.lightListImg = ImageIO.read(new File("src/main/resources/invertedlist.png"));
    } catch(IOException e) {}
    
    try
    {
      this.folderImg = ImageIO.read(new File("src/main/resources/folder.jpg"));
    } catch(IOException e) {}
    
    
    this.fullStarImg = fullStarImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.emptyStarImg = emptyStarImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.gridImg = gridImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.listImg = listImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.lightGridImg = lightGridImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.lightListImg = lightListImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.folderImg = folderImg.getScaledInstance(50, 50,  java.awt.Image.SCALE_SMOOTH);
    this.fullStarIcon = new ImageIcon(fullStarImg);
    this.emptyStarIcon = new ImageIcon(emptyStarImg);
    this.gridIcon = new ImageIcon(gridImg);
    this.listIcon = new ImageIcon(listImg);
    this.folderIcon = new ImageIcon(folderImg);
    
    this.grid = new JRadioButton(gridIcon, true);
    grid.addActionListener(customlistener);
    
    this.list = new JRadioButton(listIcon, false);
    list.addActionListener(customlistener);
    
    this.layouts = new ButtonGroup();
    layouts.add(grid);
    layouts.add(list);
    grid.setSelected(true);
    list.setSelected(false);
    
    this.fileChooser = new JFileChooser();
    this.fileButton = new JButton(folderIcon);
    fileButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        int retval = fileChooser.showOpenDialog(null);
        
        if (retval == JFileChooser.APPROVE_OPTION)
        {
          File file = fileChooser.getSelectedFile();
          if (file.isFile())
          {
            model.newPic(file.getAbsolutePath(), file.getName(), file.length(), file.lastModified());
          }
        }
      }
    });
    
    this.star1 = new JRadioButton(emptyStarIcon, false);
    star1.addActionListener(superlistener);
    this.star2 = new JRadioButton(emptyStarIcon, false);
    star2.addActionListener(superlistener);
    this.star3 = new JRadioButton(emptyStarIcon, false);
    star3.addActionListener(superlistener);
    this.star4 = new JRadioButton(emptyStarIcon, false);
    star4.addActionListener(superlistener);
    this.star5 = new JRadioButton(emptyStarIcon, false);
    star5.addActionListener(superlistener);
    this.stars = new ButtonGroup();
    stars.add(star1);
    stars.add(star2);
    stars.add(star3);
    stars.add(star4);
    stars.add(star5);
    star1.setSelected(false);
    star2.setSelected(false);
    star3.setSelected(false);
    star4.setSelected(false);
    star5.setSelected(false);


    JPanel tempPanel = new JPanel();
    tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS));
    tempPanel.add(grid);
    tempPanel.add(list);
    this.add(tempPanel, BorderLayout.LINE_START);
    this.add(new JLabel("Fotag!", JLabel.CENTER), BorderLayout.CENTER);
    JPanel tempPanel2 = new JPanel();
    tempPanel2.setLayout(new BoxLayout(tempPanel2, BoxLayout.X_AXIS));
    tempPanel2.add(star1);
    tempPanel2.add(star2);
    tempPanel2.add(star3);
    tempPanel2.add(star4);
    tempPanel2.add(star5);
    tempPanel2.add(fileButton);
    this.add(tempPanel2, BorderLayout.LINE_END);
    model.addObserver(this);
  }

  public void update(Object observable)
  {
    revalidate();
    repaint();
  }
}
