package org.CS2336;

import com.googlecode.lanterna.TerminalFacade;
import com.googlecode.lanterna.gui.Action;
import com.googlecode.lanterna.gui.GUIScreen;
import com.googlecode.lanterna.gui.Window;
import com.googlecode.lanterna.gui.component.Button;
import com.googlecode.lanterna.gui.component.Label;
import com.googlecode.lanterna.gui.dialog.MessageBox;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class TreeSearch {
    BinaryTree<String> myTree = new BinaryTree<String>();
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("java.awt.headless", "true");

        GUIScreen textGUI = TerminalFacade.createGUIScreen();
        if(textGUI == null) {
            System.err.println("Couldn't allocate a terminal!");
            return;
        }
        textGUI.getScreen().startScreen();
        textGUI.setTitle("GUI Test");
        //Do GUI logic here

        MyWindow myWindow = new MyWindow();
        textGUI.showWindow(myWindow, GUIScreen.Position.CENTER);
        textGUI.getScreen().refresh();
        textGUI.getScreen().stopScreen();
    }


    //Create tree from tree
    //Create tree from tree
    public void buildFromFile(File newFile) throws FileNotFoundException {
        Scanner file = new Scanner(newFile);
        String input;
        if(file.hasNextLine()) {
            input = file.nextLine();
            String[] arrayedInput = input.split(" ");
            for (int i = 0; i < arrayedInput.length; i++){
                myTree.insert(arrayedInput[i]);
            }
        }
    }

    //Print tree to file
    public void printToFile() throws IOException {
        try{
            File preorderOut = new File("preorder.out.text");
            PrintWriter printPreorder = new PrintWriter(preorderOut);
            ArrayList<String> preorderstorage = new ArrayList(myTree.preorder());
            printPreorder.println(preorderstorage);
            printPreorder.close();
        } catch (IOException e){
            System.out.println("Exception occured " + e);
        }
        try {
            File inorderOut = new File("inorder.out.text");
            PrintWriter printInorder = new PrintWriter(inorderOut);
            ArrayList<String> inorderstorage = new ArrayList(myTree.inorder());
            printInorder.println(inorderstorage);
            printInorder.close();
        } catch (IOException e){
            System.out.println("Exception occured " + e);
        }
        try{
            File postorderOut = new File("postorder.out.text");
            PrintWriter printPostorder = new PrintWriter(postorderOut);
            ArrayList<String> postorderstorage = new ArrayList(myTree.postorder());
            printPostorder.println(postorderstorage);
            printPostorder.close();
        } catch (IOException e){
            System.out.println("Exception occured " + e);
        }
    }
}

class MyWindow extends Window {
    public MyWindow()
    {
        super("My Window!");
        addComponent(new Button("Button with no action"));
        addComponent(new Button("Button with action", new Action() {
            @Override
            public void doAction() {
                MessageBox.showMessageBox(getOwner(), "Hello", "You selected the button with an action attached to it!");
            }
        }));
        addComponent(new Button("Close", new closeWindow(this)));
    }
}

class closeWindow implements Action {
    private final Window window;

    public closeWindow(Window window) {
        this.window = window;
    }

    @Override
    public void doAction() {
        this.window.close();
    }
}
@@ -1048,17 +1048,19 @@
    M
-y
+ain
 Window m
 yWin
@@ -1055,17 +1055,19 @@
 Window m
-y
+ain
 Window =
@@ -1072,17 +1072,19 @@
  = new M
-y
+ain
 Window()
@@ -1113,17 +1113,19 @@
 Window(m
-y
+ain
 Window, 
@@ -3039,17 +3039,19 @@
 %0Aclass M
-y
+ain
 Window e
@@ -3082,17 +3082,19 @@
 ic M
-y
+ain
 Window()
 %0A   
@@ -3089,20 +3089,16 @@
 Window()
-%0A   
  %7B%0A     
@@ -3111,355 +3111,19 @@
 er(%22
-My Window!%22);%0A        addComponent(new Button(%22Button with no action%22));%0A        addComponent(new Button(%22Button with action%22, new Action() %7B%0A            @Override%0A            public void doAction() %7B%0A                MessageBox.showMessageBox(getOwner(), %22Hello%22, %22You selected the button with an action attached to it!%22);%0A            %7D%0A        %7D)
+TreeSearch%22
 );%0A 

change gui test code to have meaningful names