package cicaprojekt;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

public class ApplicationFrame implements Runnable
{
    private JFrame jframe;

    private JPanel mapselectorpanel = null;
    private Display gamepanel;

    private JList<File> filelist;

    private KeyListener keyHandler = new Control.KeyHandler();

    private class ListMouseHandler extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            jframe.remove(mapselectorpanel);
            File dungeonSource = ((JList<File>) e.getComponent()).getSelectedValue();
            try {
                setGameParams(dungeonSource);
                gamepanel.drawVisuals();
            } catch (IOException e1) {
                Control.ioErrorMessage();
            }

            jframe.add(gamepanel);
            jframe.revalidate();
        }
    }

    public void init(){
        jframe = new JFrame();
        jframe.setFocusable(true);
        jframe.setMinimumSize(new Dimension(640, 395));
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        initMapselector();

        jframe.add(mapselectorpanel);
    }

    public void backToMapSelection() {
        jframe.remove(gamepanel);
        jframe.removeKeyListener(keyHandler);
        initMapselector();
        jframe.add(mapselectorpanel);
        jframe.revalidate();
    }

    @Override
    public void run() {
        jframe.setVisible(true);
    }

    private void initMapselector() {
        if(mapselectorpanel != null)
            jframe.remove(mapselectorpanel);
        mapselectorpanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                try {
                    g.drawImage(ImageIO.read(new File("Menu.png")), 0, 0, null);
                }
                catch (IOException ioe) {
                    Control.ioErrorMessage();
                }
            }
        };

        DefaultListModel<File> dungeonslist = new DefaultListModel<>();
        File dir = new File(System.getProperty("user.dir"));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("N/A", "dungeon");

        for (File f : dir.listFiles()) {
            if (filter.accept(f) && f.isFile()){
                dungeonslist.addElement(f);
            }
        }

        filelist = new JList<>(dungeonslist);
        filelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        filelist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        filelist.addMouseListener(new ListMouseHandler());
        filelist.setMaximumSize(new Dimension(400, 80));

        mapselectorpanel.setLayout(new BorderLayout());
        mapselectorpanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getX() > 229 && e.getY() > 186 && e.getX() < 433 && e.getY() < 219) {
                    //JOptionPane.showConfirmDialog(mapselectorpanel, "Ingyen sör");
                    mapselectorpanel.add(filelist, BorderLayout.SOUTH);
                    mapselectorpanel.updateUI();
                }

            }
        });

    }
    
    private void setGameParams(File dungeonFile) throws IOException {
        gamepanel = new Display();
        jframe.addKeyListener(keyHandler);
        Game.instance.setDisplay(gamepanel);
        Game.instance.setDungeon(new Dungeon());
        Game.instance.startGame(dungeonFile);
    }
}
