package edu.school.animals;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;

public class AnimalsFrame extends JFrame {

	/**
	 * Информатика, 8 клас, издателство Просвета, 2017
	 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
	 * 
	 * Задача: Във файлове shape1.txt, shape2.txt и eyes.
	 * txt са дадени координатите съответно на две
	 * линии, оформящи тялото (по две цели числа на ред) 
	 * и кръгчетата — очи (четири цели числа) на слона.
	 * Създайте приложение с ГПИ, което изчертава изображението.
	 * 
	 * Версия 1.
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel pnlControl;
	private JPanel pnlData;
	private JPanel pnlCanvas;
	private JLabel lblDatapreparation;
	private JTextField txtOriginalFileName;
	private JTextField txtTransformedFileName;
	private JTextField txtOpenFileName;
	private JScrollPane scrlPaneOriginalData;
	private JTextArea txaOriginalData;
	private JTextArea txaTransformedData;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnimalsFrame frame = new AnimalsFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnimalsFrame() {
		setTitle("\u0416\u0438\u0432\u043E\u0442\u043D\u0438");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 421);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[20%][20%][grow]", "[grow]"));
		
		pnlControl = new JPanel();
		pnlControl.setBackground(new Color(204, 255, 153));
		contentPane.add(pnlControl, "cell 0 0,grow");
		pnlControl.setLayout(new MigLayout("", "[grow]", "[][][][][][38.00][][][][][][][][grow,bottom]"));
		
		lblDatapreparation = new JLabel("\u041F\u043E\u0434\u0433\u043E\u0442\u043E\u0432\u043A\u0430 \u043D\u0430 \u0434\u0430\u043D\u043D\u0438");
		lblDatapreparation.setFont(new Font("Tahoma", Font.BOLD, 11));
		pnlControl.add(lblDatapreparation, "cell 0 0");
		
		JLabel lblOriginalFile = new JLabel("\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B\u0435\u043D \u0444\u0430\u0439\u043B:");
		pnlControl.add(lblOriginalFile, "cell 0 1");
		
		txtOriginalFileName = new JTextField();
		pnlControl.add(txtOriginalFileName, "cell 0 2,alignx right");
		txtOriginalFileName.setColumns(10);
		
		JLabel lblTransformedFile = new JLabel("\u0422\u0440\u0430\u043D\u0441\u0444\u043E\u0440\u043C\u0438\u0440\u0430\u043D \u0444\u0430\u0439\u043B");
		pnlControl.add(lblTransformedFile, "cell 0 3");
		
		txtTransformedFileName = new JTextField();
		pnlControl.add(txtTransformedFileName, "cell 0 4,alignx right");
		txtTransformedFileName.setColumns(10);
		
		JButton btnPrepare = new JButton("\u041F\u043E\u0434\u0433\u043E\u0442\u0432\u0438");
		btnPrepare.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int w = pnlCanvas.getWidth();
				int h = pnlCanvas.getHeight();
				
				//Относителен път до файла
				final String PATH = "resources/elephant/"; 
				String fileName = PATH + txtOriginalFileName.getText();
				//Свързване на програмата с файла 
				File dotsFile = new File (fileName);
				
				try {
					Scanner input = new Scanner(dotsFile);
					
					int x, y;
					String original = "";
					String transformed = "";
					while (input.hasNext()){
						x = input.nextInt();
						y = input.nextInt();
						original += x + " " + y + "\n";
						//трансформации
						x = x + w/2;
						y = -y + h/2;
						transformed += x + " " + y + "\n";
					}
					
					txaOriginalData.setText(original);
					txaTransformedData.setText(transformed);
					
					input.close();					
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}	
			}
		});
		pnlControl.add(btnPrepare, "flowx,cell 0 5");
		
		JButton btnSave = new JButton("\u0417\u0430\u043F\u0438\u0448\u0438");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Относителен път до файла
				final String PATH = "resources/elephant/"; 
				String fileName = PATH + txtTransformedFileName.getText();
				//Свързване на програмата с файла 
				File dotsFile = new File (fileName);
				
				try {
					Formatter output = new Formatter(dotsFile);
					
					output.format("%s", txaTransformedData.getText());
					output.close();
					
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		pnlControl.add(btnSave, "cell 0 5");
		
		JSeparator separator = new JSeparator();
		separator.setForeground(new Color(204, 255, 153));
		separator.setBackground(new Color(153, 153, 102));
		pnlControl.add(separator, "cell 0 6,grow");
		
		JLabel lblDrawing = new JLabel("\u0427\u0435\u0440\u0442\u0435\u0436");
		lblDrawing.setFont(new Font("Tahoma", Font.BOLD, 11));
		pnlControl.add(lblDrawing, "cell 0 8");
		
		JLabel lblOpenFileName = new JLabel("\u0417\u0430\u0440\u0435\u0434\u0438 \u0444\u0430\u0439\u043B:");
		pnlControl.add(lblOpenFileName, "cell 0 9");
		
		txtOpenFileName = new JTextField();
		pnlControl.add(txtOpenFileName, "cell 0 10,alignx right");
		txtOpenFileName.setColumns(10);
		
			
		JRadioButton rbtnLine = new JRadioButton("\u041B\u0438\u043D\u0438\u044F");
		rbtnLine.setSelected(true);
		rbtnLine.setBackground(new Color(204, 255, 153));
		pnlControl.add(rbtnLine, "flowx,cell 0 11");
		
		JRadioButton rbtnEye = new JRadioButton("\u041E\u043A\u043E");
		rbtnEye.setBackground(new Color(204, 255, 153));
		pnlControl.add(rbtnEye, "cell 0 11");
		
		ButtonGroup btnGrShape = new ButtonGroup();
		btnGrShape.add(rbtnLine);
		btnGrShape.add(rbtnEye);
		
		JButton btnDraw = new JButton("\u0427\u0435\u0440\u0442\u0430\u0439");
		btnDraw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Относителен път до файла
				final String PATH = "resources/elephant/"; 
				String fileName = PATH + txtOpenFileName.getText();
				//Свързване на програмата с файла 
				File dotsFile = new File (fileName);
	
//				try {
//					Scanner input = new Scanner(dotsFile);
//					
//					Graphics g = pnlCanvas.getGraphics();
//					
//					//Начална точка
//					int xPrev = input.nextInt();
//					int yPrev = input.nextInt();
//					
//					//Текуща точка
//					int xCurrent;
//					int yCurrent;
//					
//					while (input.hasNext()){
//						xCurrent = input.nextInt();
//						yCurrent = input.nextInt();
//						g.drawLine(xPrev, yPrev, xCurrent, yCurrent);
//						xPrev = xCurrent;
//						yPrev = yCurrent;						
//					}
//					
//					input.close();
//					
//				} catch (FileNotFoundException e1) {
//					e1.printStackTrace();
//				}
				
				
				if (rbtnLine.isSelected()){
					try {
						Scanner input = new Scanner(dotsFile);
						
						Graphics g = pnlCanvas.getGraphics();
						
						//Начална точка
						int xPrev = input.nextInt();
						int yPrev = input.nextInt();
						
						//Текуща точка
						int xCurrent;
						int yCurrent;
						
						while (input.hasNext()){
							xCurrent = input.nextInt();
							yCurrent = input.nextInt();
							g.drawLine(xPrev, yPrev, xCurrent, yCurrent);
							xPrev = xCurrent;
							yPrev = yCurrent;						
						}
						
						input.close();
						
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					}
				}
				else { //Очи
					try {
						Scanner input = new Scanner(dotsFile);
						
						Graphics g = pnlCanvas.getGraphics();
						
						//Първо око
						int x = input.nextInt();
						int y = input.nextInt();
						g.fillOval(x, y, 10, 10);
						
						//Второ око
						x = input.nextInt();
						y = input.nextInt();
						g.fillOval(x, y, 10, 10);
						
						input.close();
						
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					}
				}
				
			}
		});
		
		pnlControl.add(btnDraw, "flowx,cell 0 12");
				
		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setForeground(new Color(153, 0, 0));
		pnlControl.add(btnExit, "cell 0 13,alignx right");
		
		pnlData = new JPanel();
		pnlData.setBackground(new Color(204, 255, 153));
		contentPane.add(pnlData, "cell 1 0,grow");
		pnlData.setLayout(new MigLayout("", "[grow]", "[][35%][][35%]"));
		
		JLabel lblOriginalData = new JLabel("\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B\u043D\u0438 \u0434\u0430\u043D\u043D\u0438:");
		lblOriginalData.setFont(new Font("Tahoma", Font.BOLD, 11));
		pnlData.add(lblOriginalData, "cell 0 0");
		
		scrlPaneOriginalData = new JScrollPane();
		pnlData.add(scrlPaneOriginalData, "cell 0 1,grow");
		
		txaOriginalData = new JTextArea();
		scrlPaneOriginalData.setViewportView(txaOriginalData);
		
		JLabel lblTransformedData = new JLabel("\u0422\u0440\u0430\u043D\u0441\u0444\u043E\u0440\u043C\u0438\u0440\u0430\u043D\u0438 \u0434\u0430\u043D\u043D\u0438:");
		lblTransformedData.setFont(new Font("Tahoma", Font.BOLD, 11));
		pnlData.add(lblTransformedData, "flowy,cell 0 2");
		
		JScrollPane scrlPaneTransformedData = new JScrollPane();
		pnlData.add(scrlPaneTransformedData, "cell 0 3,grow");
		
		txaTransformedData = new JTextArea();
		scrlPaneTransformedData.setViewportView(txaTransformedData);
		
		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pnlCanvas.repaint();
				txaOriginalData.setText("");
				txaTransformedData.setText("");
				txtTransformedFileName.setText("");
				txtOpenFileName.setText("");
				txtOriginalFileName.setText("");
			}
		});
		pnlControl.add(btnReset, "cell 0 12");
		
		pnlCanvas = new JPanel();
		pnlCanvas.setBackground(new Color(255, 255, 255));
		contentPane.add(pnlCanvas, "cell 2 0,grow");
	}

}
