package macro;

import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

	static JLabel label = new JLabel("sus");
	static JFrame frame = new JFrame("Macro");

	public static void main(String[] args) throws IOException, InterruptedException {

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

		new Thread(() -> {
			// System.out.println(Text.getFromSquare(0, 0, 2560, 1440));
			List<String> intrestedPokemon = List.of("Stunky", "Muk", "Smeargle", "Stantler", "Magikarp");
			while (true) {
				if (Window.isFocused("proclient")) {
//					Input.press('a', 600);
//					Input.press('d', 600);
					String foundText = Text.getFromSquare(1200, 500, 1450, 540).trim();
					if (!contains(intrestedPokemon, foundText) && foundText.toLowerCase().contains("wild")) {
						Input.press('4');
//						Input.press('1');
					}else if(contains(intrestedPokemon, foundText)) {
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Input.press('3');
						Input.press('1');
					}else {
						Input.press('3');
					}
					System.out.println(foundText);
				}
			}
		}).run();
	}

	private static void createAndShowGUI() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		}
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent windowEvent) {
				System.exit(0);
			}
		});

		frame.getContentPane().add(label);
		Timer timer = new Timer(100, e -> {
			Point p = MouseInfo.getPointerInfo().getLocation();
			label.setText(p.x + "/" + p.y);
		});
		timer.start();

		// Display the window.
		frame.setMinimumSize(new Dimension(500, 300));
		frame.pack();
		frame.setVisible(true);
	}

	private static boolean contains(List<String> pokemonWant, String pokemonFound) {
		for (String s : pokemonWant) {
			if (pokemonFound.toLowerCase().contains(s.toLowerCase()))
				return true;
		}
		return false;
	}
}
