/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gamegraphics;

import gamegraphics.misc.Option;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;


/**
 *
 * @author Ani
 */
public class OptionPanel extends JPanel{
    private JPanel rulesPanel;
    private JEditorPane rulesPane;
    private JButton okRules;

    private JPanel menuPanel;
    private JButton askRules;
    private JLabel player;
    private JLabel level;
    private ButtonGroup selectedPlayer;
    private JRadioButton selectedPlayerHuman;
    private JRadioButton selectedPlayerComputer;
    private ButtonGroup selectedLevel;
    private JRadioButton selectedLevelEasy;
    private JRadioButton selectedLevelMedium;
    private JRadioButton selectedLevelHard;
    private JButton startgame;
    private Option option = null;

    public OptionPanel() {
        init();
    }

    private void init(){
        initSettings();
        initRulesPanel();
        initMenuPanel();
        add(menuPanel);
    }

    private void initSettings(){
        setSize(800, 500);
        setBackground(new Color(30, 180, 30));
        setLayout(null);
    }

    private void initRulesPanel(){
        rulesPanel = new JPanel(null);
        rulesPanel.setBounds(0, 0, 800, 500);
        rulesPanel.setBackground(getBackground());
        initRulesPane();
        initOkRules();
    }

    private void initRulesPane(){
        URL url = getClass().getResource("/gamegraphics/docs/rules.html");

         if (url!=null){
            try {
                rulesPane = new JEditorPane(url);
                rulesPane.setEditable(false);
                rulesPane.setBackground(getBackground());
                rulesPane.setBounds(100, 0, 600, 450);
                rulesPane.setSize(600, 450);
                rulesPanel.add(rulesPane);
            } catch (IOException ex) {

            }
         }
    }

    private void initOkRules(){
        okRules = new JButton("OK");
        okRules.setBounds(360, 450, 80, 40);
        okRules.addActionListener(new OkRulesButtonClicked());
        rulesPanel.add(okRules);
    }

    private void initMenuPanel(){
        menuPanel = new JPanel(null);
        menuPanel.setBackground(getBackground());
        menuPanel.setBounds(0, 0, 800, 500);
        initAskRules();
        initSelectPlayer();
        initSelectLevel();
        initStartButton();
    }

    private void initAskRules(){
        askRules = new JButton("Szabályok");
        askRules.setBounds(300, 100, 200, 40);
        askRules.addActionListener(new AskRulesButtonClicked());
        menuPanel.add(askRules);
    }

    private void initSelectPlayer(){
        player = new JLabel("Kezdőjátékos:");
        player.setBounds(355, 150, 90, 20);

        selectedPlayerHuman = new JRadioButton("Ember");
        selectedPlayerHuman.setBounds(300, 180, 100, 20);
        selectedPlayerHuman.setSelected(true);
        selectedPlayerComputer = new JRadioButton("Számítógép");
        selectedPlayerComputer.setBounds(400, 180, 100, 20);
        selectedPlayer = new ButtonGroup();
        selectedPlayer.add(selectedPlayerHuman);
        selectedPlayer.add(selectedPlayerComputer);

        menuPanel.add(player);
        menuPanel.add(selectedPlayerHuman);
        menuPanel.add(selectedPlayerComputer);
    }

    private void initSelectLevel(){
        level = new JLabel("Nehézségi szint: ");
        level.setBounds(350, 210, 100, 20);

        selectedLevelEasy = new JRadioButton("Könnyű");
        selectedLevelEasy.setBounds(250, 240, 100, 20);
        selectedLevelEasy.setSelected(true);
        selectedLevelMedium = new JRadioButton("Közepes");
        selectedLevelMedium.setBounds(350, 240, 100, 20);
        selectedLevelHard = new JRadioButton("Nehéz");
        selectedLevelHard.setBounds(450, 240, 100, 20);
        selectedLevel = new ButtonGroup();
        selectedLevel.add(selectedLevelEasy);
        selectedLevel.add(selectedLevelMedium);
        selectedLevel.add(selectedLevelHard);

        menuPanel.add(level);
        menuPanel.add(selectedLevelEasy);
        menuPanel.add(selectedLevelMedium);
        menuPanel.add(selectedLevelHard);
    }

    private void initStartButton(){
        startgame = new JButton("START");
        startgame.setBounds(300, 280, 200, 40);
        startgame.addActionListener(new StartButtonClicked());
        menuPanel.add(startgame);
    }

    public Option getOption() {
        return option;
    }

    public void setOption(Option option) {
        this.option = option;
    }

    class StartButtonClicked implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            boolean player=Option.HUMAN;
            int level=Option.EASY;

            if (selectedPlayerComputer.isSelected()){
                player = false;
            }

            if (selectedLevelMedium.isSelected()){
                level = Option.MEDIUM;
            }

            if (selectedLevelHard.isSelected()){
                level = Option.HARD;
            }

            option = new Option(player, level);
        }
    }

    class AskRulesButtonClicked implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            remove(menuPanel);
            repaint();
            add(rulesPanel);
            repaint();
        }

    }

    class OkRulesButtonClicked implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            remove(rulesPanel);
            repaint();
            add(menuPanel);
            repaint();
        }

    }




}
