package gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import data.ComboBoxTexts;
import data.Search;

public class MainGui extends JFrame {

	// Generated by Eclipse
	private static final long serialVersionUID = 1L;

	JComboBox counties, usage, water, sorting;

	private JSplitPane splitPane;

	private JTextField query, minPrice, maxPrice;

	private JButton find;

	private JLabel blank;

	private JPanel buttonPanel;

	GraphPanel graph;

	Search search;

	ComboBoxTexts comboBoxTexts;

	public MainGui() {
		comboBoxTexts = new ComboBoxTexts();
		initGUI();
	}

	JComboBox labelledComboBox(Container c, String labelName, String[] options) {
		JLabel label = new JLabel();
		label.setText(labelName);
		c.add(label);

		JComboBox comboBox = new JComboBox(options);
		c.add(comboBox);
		return comboBox;
	}

	JTextField labelledTextField(Container c, String labelName) {
		c.add(new JLabel(labelName));
		JTextField tmp = new JTextField();
		c.add(tmp);
		Dimension size = new Dimension(100, 22);
		tmp.setMinimumSize(size);
		tmp.setPreferredSize(size);
		return tmp;

	}

	private void initGUI() {
		GridLayout thisLayout = new GridLayout(1, 1);
		thisLayout.setColumns(1);
		thisLayout.setHgap(5);
		thisLayout.setVgap(5);
		getContentPane().setLayout(thisLayout);

		splitPane = new JSplitPane();
		getContentPane().add(splitPane);

		buttonPanel = new JPanel();
		GridLayout buttonPanelLayout = new GridLayout(4, 10);
		buttonPanel.setLayout(buttonPanelLayout);
		splitPane.add(buttonPanel, JSplitPane.BOTTOM);

		query = labelledTextField(buttonPanel, "Otsingusõna");
		minPrice = labelledTextField(buttonPanel, "Miinimumhind");
		maxPrice = labelledTextField(buttonPanel, "Maksimumhind");

		water = labelledComboBox(buttonPanel, "Veekogu", comboBoxTexts.getWater());
		usage = labelledComboBox(buttonPanel, "Kasutus", comboBoxTexts.getUsage());
		sorting = labelledComboBox(buttonPanel, "Sorteerimine", comboBoxTexts.getSorting());
		sorting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				sortingActionPerformed(evt);
			}
		});

		counties = labelledComboBox(buttonPanel, "Maakonnad", comboBoxTexts.getCounties());

		graph = new GraphPanel();
		splitPane.add(graph, JSplitPane.TOP);
		graph.setPreferredSize(new java.awt.Dimension(100, 100));
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setResizeWeight(1.0);
		splitPane.setDividerSize(0);

		blank = new JLabel();
		buttonPanel.add(blank);

		find = new JButton();
		buttonPanel.add(find);
		find.setText("Otsi");
		find.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				findActionPerformed(evt);
			}
		});

		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setTitle("Kinnisvara hinnad");
		this.setSize(335, 174);

	}

	String generateURL() {
		String s = "";
		String min, max;
		String error = "";
		try {
			float f = Float.parseFloat(minPrice.getText());
			min = "" + (int) f;
		} catch (NumberFormatException e) {
			min = "0";
			error = "Miinimumhind peab olema number! ";
		}
		try {
			float f = Float.parseFloat(maxPrice.getText());
			max = "" + (int) f;
		} catch (NumberFormatException e) {
			max = "";
			error += "Maksimumhind peab olema number! ";
		}
		int usg = usage.getSelectedIndex();
		if (usg != 0) {
			usg += comboBoxTexts.getUsageOffset();
		}
		int wtr = water.getSelectedIndex();
		if (wtr != 0) {
			wtr += comboBoxTexts.getWaterOffset();
		}
		s = "http://www.kv.ee/?act=search.simple&deal_type=5&county=" + counties.getSelectedIndex()
				+ "&page_size=25&price_min=" + min + "&price_max=" + max + "&keyword=" + query.getText()
				+ "&price_type=1&orderby=ch&company_id=&broker_id=&search=Otsi&land_purpose=" + usg
				+ "&detail_plan=0&area_ground_min=&area_ground_max=&feature%5B51%5D=" + wtr + "&feature%5B57%5D=0";
		return s;
	}

	private void findActionPerformed(ActionEvent evt) {
		System.out.println(generateURL());
		search = new Search(generateURL());
		redrawGraph();
	}

	private void sortingActionPerformed(ActionEvent evt) {
		redrawGraph();
	}

	private void redrawGraph() {
		if (search == null || search.getOffers() == null) {
			return;
		}
		search.sort(sorting.getSelectedIndex());
		graph.init(search.getOffers(), sorting.getSelectedIndex(), search.getAverageValue());
	}
}