package com.nighthawk.hacks.methodsDataTypes;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Menu: custom implementation
 * @author     John Mortensen
 *
 * Uses String to contain Title for an Option
 * Uses Runnable to store Class-Method to be run when Title is selected
 */

// The Menu Class has a HashMap of Menu Rows
public class Menu {
    // Format
    // Key {0, 1, 2, ...} created based on order of input menu
    // Value {MenuRow0, MenuRow1, MenuRow2,...} each corresponds to key
    // MenuRow  {<Exit,Noop>, Option1, Option2, ...}
    Map<Integer, MenuRow> menu = new HashMap<>();

    /**
     *  Constructor for Menu,
     *
     * @param  rows,  is the row data for menu.
     */
    public Menu(MenuRow[] rows) {
        int i = 0;
        for (MenuRow row : rows) {
            // Build HashMap for lookup convenience
            menu.put(i++, new MenuRow(row.getTitle(), row.getAction()));
        }
    }

    /**
     *  Get Row from Menu,
     *
     * @param  i,  HashMap key (k)
     *
     * @return  MenuRow, the selected menu
     */
    public MenuRow get(int i) {
        return menu.get(i);
    }

    /**
     *  Iterate through and print rows in HashMap
     */
    public void print() {
        for (Map.Entry<Integer, MenuRow> pair : menu.entrySet()) {
            System.out.println(pair.getKey() + " ==> " + pair.getValue().getTitle());
        }
    }

    /**
     *  To test run Driver
     */
    public static void main(String[] args) {
        Driver.main(args);
    }

}

// The MenuRow Class has title and action for individual line item in menu
class MenuRow {
    String title;       // menu item title
    Runnable action;    // menu item action, using Runnable

    /**
     *  Constructor for MenuRow,
     *
     * @param  title,  is the description of the menu item
     * @param  action, is the run-able action for the menu item
     */
    public MenuRow(String title, Runnable action) {
        this.title = title;
        this.action = action;
    }

    /**
     *  Getters
     */
    public String getTitle() {
        return this.title;
    }
    public Runnable getAction() {
        return this.action;
    }

    /**
     *  Runs the action using Runnable (.run)
     */
    public void run() {
        action.run();
    }
}

// The Main Class illustrates initializing and using Menu with Runnable action
class Driver {
    /**
     *  Menu Control Example
     */
    public static void main(String[] args) {
        // Row initialize
        MenuRow[] rows = new MenuRow[]{
            // lambda style, () -> to point to Class.Method
            new MenuRow("Exit", () -> main(null)),
            new MenuRow("Do Nothing", () -> DoNothingByValue.main(null)),
            new MenuRow("Swap if Hi-Low", () -> IntByReference.main(null)),
            new MenuRow("Matrix Reverse", () -> Matrix.main(null)),
            new MenuRow("Diverse Array", () -> Matrix.main(null)),
            new MenuRow("Random Squirrels", () -> Number.main(null))
        };

        // Menu construction
        Menu menu = new Menu(rows);

        // Run menu forever, exit condition contained in loop
        while (true) {
            System.out.println("Hacks Menu:");
            // print rows
            menu.print();

            // Scan for input
            try {
                Scanner scan = new Scanner(System.in);
                int selection = scan.nextInt();

                // menu action
                try {
                    MenuRow row = menu.get(selection);
                    // stop menu
                    if (row.getTitle().equals("Exit")) {
                        if (scan != null) 
                            scan.close();  // scanner resource requires release
                        return;
                    }
                    // run option
                    row.run();
                } catch (Exception e) {
                    System.out.printf("Invalid selection %d\n", selection);
                }

            } catch (Exception e) {
                System.out.println("Not a number");
            }
        }
    }
public class Alphabet extends Generics {
	// Class data
	public static KeyTypes key = KeyType.title;  // static initializer
	public static void setOrder(KeyTypes key) {Alphabet.key = key;}
	public enum KeyType implements KeyTypes {title, letter}
	private static final int size = 26;  // constant used in data initialization

	// Instance data
	private final char letter;
	
	/*
	 * single letter object
	 */
	public Alphabet(char letter)
	{
		this.setType("Alphabet");
		this.letter = letter;
	}

	/* 'Generics' requires getKey to help enforce KeyTypes usage */
	@Override
	protected KeyTypes getKey() { return Alphabet.key; }

	/* 'Generics' requires toString override
	 * toString provides data based off of Static Key setting
	 */
	@Override
	public String toString()
	{
		String output="";
		if (KeyType.letter.equals(this.getKey())) {
			output += this.letter;
		} else {
			output += super.getType() + ": " + this.letter;
		}
		return output;
	}

	// Test data initializer for upper case Alphabet
	public static Alphabet[] alphabetData()
	{
		Alphabet[] alphabet = new Alphabet[Alphabet.size];
		for (int i = 0; i < Alphabet.size; i++)
		{
			alphabet[i] = new Alphabet( (char)('A' + i) );
		} 	
		return alphabet;
	}
	
	/* 
	 * main to test Animal class
	 */
	public static void main(String[] args)
	{
		// Inheritance Hierarchy
		Alphabet[] objs = alphabetData();

		// print with title
		Alphabet.setOrder(KeyType.title);
		Alphabet.print(objs);

		// print letter only
		Alphabet.setOrder(KeyType.letter);
		Alphabet.print(objs);
	}
	
}
Alphabet.main(null);
/* This is wrapper class...
 Objective would be to push more functionality into this Class to enforce consistent definition
 */
public abstract class Generics {
	public final String masterType = "Generic";
	private String type;	// extender should define their data type

	// generic enumerated interface
	public interface KeyTypes {
		String name();
	}
	protected abstract KeyTypes getKey();  	// this method helps force usage of KeyTypes

	// getter
	public String getMasterType() {
		return masterType;
	}

	// getter
	public String getType() {
		return type;
	}

	// setter
	public void setType(String type) {
		this.type = type;
	}
	
	// this method is used to establish key order
	public abstract String toString();

	// static print method used by extended classes
	public static void print(Generics[] objs) {
		// print 'Object' properties
		System.out.println(objs.getClass() + " " + objs.length);

		// print 'Generics' properties
		if (objs.length > 0) {
			Generics obj = objs[0];	// Look at properties of 1st element
			System.out.println(
					obj.getMasterType() + ": " + 
					obj.getType() +
					" listed by " +
					obj.getKey());
		}

		// print "Generics: Objects'
		for(Object o : objs)	// observe that type is Opaque
			System.out.println(o);

		System.out.println();
	}
}
Queue<String> queue = new LinkedList<>();  // Queue interface uses LL implementation
queue.add("John");
queue.add("Jane");
queue.add("Bob");
// Collections has a toArray convertion
Object[] arr = queue.toArray();

// Empty queue
System.out.println("Empty Queue");
while (queue.size() > 0) // Interate while size
    System.out.println(queue.remove());

// Iterate of array
System.out.println("Iterate over Array");
for (Object a : arr) // Type is Object from convertion
    System.out.println(a);
Queue<String> queue = new LinkedList<>();  // Queue interface uses LL implementation
queue.add("John");
queue.add("Jane");
queue.add("Bob");
// Hack 1
public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();

        // Adding elements to the queue
        queue.add("seven");
        System.out.println("Enqueued data: " + "seven");
        printQueue(queue);

        queue.add("slimy");
        System.out.println("Enqueued data: " + "slimy");
        printQueue(queue);

        queue.add("snakes");
        System.out.println("Enqueued data: " + "snakes");
        printQueue(queue);

        queue.add("sallying");
        System.out.println("Enqueued data: " + "sallying");
        printQueue(queue);

        queue.add("slowly");
        System.out.println("Enqueued data: " + "slowly");
        printQueue(queue);

        queue.add("slithered");
        System.out.println("Enqueued data: " + "slithered");
        printQueue(queue);

        queue.add("southward");
        System.out.println("Enqueued data: " + "southward");
        printQueue(queue);

        // Removing elements from the queue
        String data =queue.remove();

        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);

        data = queue.remove();
        System.out.println("Dequeued data: " + data);
        printQueue(queue);
    }

    // Helper method to print the contents of the queue
    public static void printQueue(Queue<String> queue) {
        System.out.println("Words count: " + queue.size() + ", data: " + String.join(" ", queue));
        System.out.println();
    }
}

QueueExample.main(null);
//Hack 2
Queue<Integer> queue1 = new LinkedList<>();
//offer adds an element to the end of the queue and 
//returns a boolean value indicating whether the operation was successful
queue1.offer(1);
queue1.offer(4);
queue1.offer(5);
queue1.offer(8);

Queue<Integer> queue2 = new LinkedList<>();
queue2.offer(2);
queue2.offer(3);
queue2.offer(6);
queue1.offer(7);

Queue<Integer> mergedQueue = new LinkedList<>();

while (!queue1.isEmpty() && !queue2.isEmpty()) {
    if (queue1.peek() < queue2.peek()) { //Retrieve the first element from each queue using the peek() method
        //peek method returns the element at the front of the queue without removing it
        mergedQueue.offer(queue1.poll());
    } else { //Compare the two elements 
        //enqueue the smaller one to the new queue using the offer() method.
        mergedQueue.offer(queue2.poll());
    }//repeat
}

mergedQueue.addAll(queue1);
mergedQueue.addAll(queue2);

System.out.println("Merged queue: " + mergedQueue);
//Hack 3
Queue<Integer> queue = new LinkedList<>();
//Create a new empty list to keep track of the indices that have already been shuffled
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
queue.offer(5);
queue.offer(6);
queue.offer(7);
queue.offer(8);


List<Integer> shuffledIndices = new ArrayList<>();
//Iterate through the original queue and for each element
//generate a random index between 0 and the size of the queue
while (shuffledIndices.size() < queue.size()) {
    int currentIndex = 0;
    int randomIndex = (int) (Math.random() * queue.size());
    
    while (shuffledIndices.contains(randomIndex)) {
        randomIndex = (int) (Math.random() * queue.size());
    }
    //Retrieve the element at the generated index and swap it with the current element in the queue
    shuffledIndices.add(randomIndex);
    
    for (Integer element : queue) {
        if (currentIndex == randomIndex) {
            queue.offer(queue.poll());
            break;
        }
        queue.offer(queue.poll());
        currentIndex++;
    }
}

System.out.println("Shuffled queue: " + queue);
//Hack 4
Queue<Integer> queue = new LinkedList<>();
//Create a new empty stack to store the elements of the queue
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
queue.offer(5);

Stack<Integer> stack = new Stack<>();
while (!queue.isEmpty()) {
    stack.push(queue.poll());
}
//Dequeue all elements from the original queue and push them onto the stack
Queue<Integer> reversedQueue = new LinkedList<>();
while (!stack.isEmpty()) {
    reversedQueue.offer(stack.pop());
}//Create a new empty queue to store the reversed elements
// pop removes and returns the top element of the stack

System.out.println("Original queue: " + queue);
System.out.println("Reversed queue: " + reversedQueue);