package gui;

import gui.iconSets.DefaultIcons;
import gui.text.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.border.Border;

public class GUIFactory {
	public Font codeFont = new Font ("Lucida Console", Font.PLAIN, 14); 
	public Font textFont = new Font ("Lucida Console", Font.PLAIN, 14);
	
	public HashMap<JComponent, String> componentsWithTextBorder;
	public HashMap<JComponent, String> componentsWithTooltip;
	public HashMap<JComponent, String> combosWithTextBorder;
	public HashMap<JButton, String> buttonsWithText;	
	public GUIText guiText;
	public GUIIcons icons;
	
	public MyTabbedPane tabs;
	
	public GUIFactory() {
		componentsWithTextBorder = new HashMap<JComponent, String>();
		componentsWithTooltip = new HashMap<JComponent, String>();
		combosWithTextBorder = new HashMap<JComponent, String>();
		buttonsWithText = new HashMap<JButton, String>();
		guiText = new English();
		icons = new DefaultIcons();
	}
	
	public void makeCombos(JComboBox<String> visualBox, JComboBox<String> codeBox, JComboBox<String> guiBox) {
		int selected = visualBox.getSelectedIndex();				
		visualBox.removeAllItems();
		visualBox.addItem(guiText.translateComboItem("english"));
        visualBox.addItem(guiText.translateComboItem("slovak"));
        visualBox.setSelectedIndex(selected);
        
        selected = codeBox.getSelectedIndex();		
		codeBox.removeAllItems();
        codeBox.addItem(guiText.translateComboItem("ctype"));
        codeBox.addItem(guiText.translateComboItem("pascal"));
        codeBox.setSelectedIndex(selected);
        
        selected = guiBox.getSelectedIndex();		
		guiBox.removeAllItems();		
        guiBox.addItem(guiText.translateComboItem("slovak"));
        guiBox.addItem(guiText.translateComboItem("english"));
        guiBox.setSelectedIndex(selected);
	}
	
	
	public Border getBorderByName(String s) {
		return BorderFactory.createTitledBorder(guiText.getBorderTitle(s));
	}
	
	public Border getComboBorderByName(String s) {
		 return BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), guiText.getComboDescription(s));
	}

	public void setTabs(MyTabbedPane t) {
		tabs = t;
	}
	
	public void resetTexts() {
		for (JComponent c:componentsWithTextBorder.keySet()) c.setBorder(getBorderByName(componentsWithTextBorder.get(c)));
		for (JComponent c:componentsWithTooltip.keySet()) c.setToolTipText(guiText.getButtonDescription(componentsWithTooltip.get(c)));			
		for (JComponent c:combosWithTextBorder.keySet()) c.setBorder(getComboBorderByName(combosWithTextBorder.get(c)));			
		for (JButton c:buttonsWithText.keySet()) c.setText(guiText.getButtonDescription(buttonsWithText.get(c)));
		tabs.resetTabs(false);		
	}
		
	public Border getQuestionBorder(Color color) {
		return BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(2, 5, 0, 5, Color.WHITE), 
	    		BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, color), BorderFactory.createMatteBorder(5, 7, 5, 7, Color.WHITE)));
	}
	
	public JPanel getJPanel(LayoutManager layout, Border border, Dimension size) {
	    JPanel panel = new JPanel();
	    panel.setBackground(Color.WHITE);
	    if (layout!=null) panel.setLayout(layout);
	    if (border!=null) panel.setBorder(border);	    
	    if (size!=null) {panel.setPreferredSize(size); panel.setSize(size);}
	    return panel;
	}
	
	public JPanel getJPanel(LayoutManager layout, String border, Dimension size) {
		if (border.isEmpty()) return getJPanel(layout, BorderFactory.createEmptyBorder(), size);
		else {JPanel x = getJPanel(layout, getBorderByName(border), size); componentsWithTextBorder.put(x, border); return x;}
	}	
	
    public JScrollPane getScrollPane(JTextArea text, Dimension size, String border) {
    	JScrollPane pane = new JScrollPane(text);    
    	pane.setBackground(Color.WHITE);
    	if (size!=null) pane.setPreferredSize(size);
    	if (border.isEmpty()) pane.setBorder(BorderFactory.createEmptyBorder());
    	else { pane.setBorder(getBorderByName(border)); componentsWithTextBorder.put(pane, border);}
    	return pane;
    }
    
    public JScrollPane getScrollPane(JEditorPane text, Dimension size, String border) {
    	JScrollPane pane = new JScrollPane(text);    
    	pane.setBackground(Color.WHITE);
    	if (size!=null) pane.setPreferredSize(size);
    	if (border.isEmpty()) pane.setBorder(BorderFactory.createEmptyBorder());
    	else { pane.setBorder(getBorderByName(border)); componentsWithTextBorder.put(pane, border);}
    	return pane;
    }

    
    public JTextArea getTextArea(String font, Dimension size, boolean edit) {
    	JTextArea area = new JTextArea();
    	area.setBorder(BorderFactory.createEmptyBorder());    
    	if (font.equals("code")) area.setFont(codeFont);
    	if (font.equals("text")) area.setFont(textFont);
    	if (size!=null) area.setPreferredSize(size);
    	area.setEditable(edit);             
    	area.setBackground(Color.WHITE);    
    	return area;
    }    
    
    public JEditorPane getEditorPane(Dimension size, boolean edit) {
    	JEditorPane area = new JEditorPane();
	    area.setContentType("text/html");    
    	area.setBorder(BorderFactory.createEmptyBorder());    
    	if (size!=null) area.setPreferredSize(size);
    	area.setEditable(edit);             
    	area.setBackground(Color.WHITE);    
    	return area;
    }

    public JButton getButton(ImageIcon icon, String tool) {
    	JButton button = new JButton(icon);	   		   	
	   	button.setBorder(BorderFactory.createEmptyBorder());
	   	button.setContentAreaFilled(false);
	   	button.setToolTipText(guiText.getButtonDescription(tool));
	   	componentsWithTooltip.put(button, tool);
	   	return button;
    }

    public JButton getButton(String name) {
    	JButton button = new JButton(name);	
    	buttonsWithText.put(button, name);
	   	return button;
    }

    
    public JComboBox<String> getComboBox(Dimension size, String border) {
        JComboBox<String> box = new JComboBox<String>();
        if (size!=null) box.setPreferredSize(size);
        box.setBackground(Color.WHITE);
        if (border!=null) { box.setBorder(getComboBorderByName(border)); combosWithTextBorder.put(box, border); }
        return box;
    }

}
