package com.hotmail.kalebmarc.textfighter.main;

import javax.swing.*;
import java.io.IOException;
import java.util.Scanner;

public class Ui {
    public static boolean guiEnabled = true;
    private final static Scanner IN = new Scanner(System.in);
    private Ui() {
    }

    public static boolean isDecimalNumber(String string) {
        if (string == null) {
            return false;
        }

        int length = string.length();

        if (length == 1) {
            return false;
        }

        int i = 0;

        if (string.charAt(0) == '-') {
            if (length < 3) {
                return false;
            }
            i = 1;
        }

        int numOfDot = 0;

        for (; i < length; i++) {
            char c = string.charAt(i);
            if (c == '.')
                numOfDot++;
            else if (c == '/')
                return false;
            else if (c < '.' || c > '9') {
                return false;
            }
        }

        return (numOfDot == 1);
    }

    public static boolean isNumber(String string) {
        if (string == null) return false;

        int length = string.length();

        if (length == 0) return false;

        int i = 0;

        if (string.charAt(0) == '-') {
            if (length == 1) return false;

            i = 1;
        }

        for (; i < length; i++) {
            char c = string.charAt(i);

            if (c <= '/' || c >= ':') return false;
        }
        return true;
    }

    /*
     * The whole purpose of this class is to make it easy to change from using the Console to other
     * ways to output information. For example, switching to a GUI application, instead of changing
     * every System.out.println() in the program, you can change just the methods in this class.
     *
     * Also to control whether popup should actually be a popup or not, based on user preference
     */

    public static <T> void print(T input){
        System.out.print(String.valueOf(input));
    }

    public static <T> void println(T input){
        System.out.println(String.valueOf(input));
    }

    public static void print(){
        System.out.print("");
    }

    public static void println(){
        System.out.println("");
    }
    
    /**
     * Clears screen, prints msg, then calls pause();.
     *
     * @param msg string to output
     */
    public static void msg(String msg) {//TODO use this instead throughout project
        if (msg == null || msg.equals("")) {
            cls();
            pause();
        }

        cls();
        println(msg);
        pause();
    }

    /**
     * @param msgType Ex. JOptionPane.ERROR_MESSAGE
     */
    public static void popup(String body, String title, int msgType) {
        if (guiEnabled) {
            JOptionPane.showMessageDialog(null, body, title, msgType);
        } else {
            msg(body);
        }
    }

    public static int confirmPopup(String body, String title) {
        if (guiEnabled) {
            return JOptionPane.showConfirmDialog(null, body, title, JOptionPane.YES_NO_OPTION);
        } else {
            cls();
            println(body);
            println("(Y/N)");

            //TODO Replace this next snippet when using JTools lib
            //TODO Or.. Just write this better.
            //----------------------------------------------------
            java.util.Scanner in = new java.util.Scanner(System.in);

            while (!in.hasNextLine()) {
                in.nextLine();
            }

            String valid = in.nextLine();
            valid = valid.toUpperCase();
            if (valid.isEmpty()) {
                return 1;
            }
            char input = valid.charAt(0);
            //-----------------------------------------------------

            cls();
            if (input == 'Y') return 0;
            return 1;
        }
    }

    /*
     * Clears the console by attempting to run either the 'cls' (Windows CMD) or
     * 'clear' (Other terminals) command. If this is interrupted, the terminal
     * will be brute-force cleared
     */
    public static void cls() {
        try {
            if (System.getProperty("os.name").contains("Windows"))
                new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
            else
                Runtime.getRuntime().exec("clear");
        } catch (IOException | InterruptedException exception) {
            for (int i = 1; i < 50; i++)
                println("\n");
        }
    }

    public static int getValidInt() {
        while (!IN.hasNextInt()) {
            IN.nextLine();
        }
        return IN.nextInt();
    }

    public static int getValidInt(int min, int max) {
        int value = getValidInt();

        while (min > value || value > max)
            value = getValidInt();

        return value;
    }

    public static String getValidString() {

        IN.reset();
        return IN.next();

    }

    /*
     * Stops the program until the user presses enter, then continues
     */
    public static void pause() {
        try {

            Scanner pauseScan = new Scanner(System.in);
            String temp = pauseScan.nextLine();
            println(temp);


        } catch (Exception e) {
            //Blank for a reason - Not supposed to do anything.
        }
    }
}

@@ -425,546 +425,90 @@
     
-int length = string.length();%0D%0A%0D%0A        if (length == 1) %7B%0D%0A            return false;%0D%0A        %7D%0D%0A%0D%0A        int i = 0;%0D%0A%0D%0A        if (string.charAt(0) == '-') %7B%0D%0A            if (length %3C 3) %7B%0D%0A                return false;%0D%0A            %7D%0D%0A            i = 1;%0D%0A        %7D%0D%0A%0D%0A        int numOfDot = 0;%0D%0A%0D%0A        for (; i %3C length; i++) %7B%0D%0A            char c = string.charAt(i);%0D%0A            if (c == '.')%0D%0A                numOfDot++;%0D%0A            else if (c == '/')%0D%0A                return false;%0D%0A            else if (c %3C '.' %7C%7C c %3E '9') %7B
+//Search for 1 or more digits followed by a %60.%60 and 1 or more digits after the dot
 %0D%0A  
@@ -517,88 +517,43 @@
     
- 
-       return false;%0D%0A            %7D%0D%0A        %7D%0D%0A%0D%0A        return (numOfDot == 1
+return string.matches(%22%5C%5Cd+%5C%5C.%5C%5Cd+%22
 );%0D%0A
@@ -671,338 +671,95 @@
     
-int length = string.length();%0D%0A%0D%0A        if (length == 0) return false;%0D%0A%0D%0A        int i = 0;%0D%0A%0D%0A        if (string.charAt(0) == '-') %7B%0D%0A            if (length == 1) return false;%0D%0A%0D%0A            i = 1;%0D%0A        %7D%0D%0A%0D%0A        for (; i %3C length; i++) %7B%0D%0A            char c = string.charAt(i);%0D%0A%0D%0A            if (c %3C= '/' %7C%7C c %3E= ':')
+//Search for any character that is not a digit%0D%0A        //If you find a non-digit char,
  ret
@@ -767,28 +767,16 @@
 rn false
-;%0D%0A        %7D
 %0D%0A      
@@ -784,20 +784,38 @@
  return 
-true
+!string.matches(%22%5C%5CD%22)
 ;%0D%0A    %7D
@@ -1601,20 +1601,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    /*

Simplify isNumber & isDecimalNumber using regular expressions