/*
 * Copyright (C) 2017 Josua Frank
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package dfmaster.json;

import java.util.Arrays;

/**
 * This class provides some useful Utilities for the JSON-Creating and -Parsing
 *
 * @author Josua Frank
 */
public class JSONUtils {

    /**
     * parsees a Number according to to ECMA-Standard for JSON-Numbers
     *
     * @param string The String to be parsed
     * @return The parsed Number or 0 if any errors occur.
     */
    public static Number parse(String string) {
        if (string == null || string.isEmpty()) {
            return DEFAULTVALUE;//Expecting chars to parse(no char available)
        }
        int i;
        JSONUtils number = new JSONUtils();
        number.negativeState(string.toCharArray(), 0);
        double result = 0;
        int length = number.decimal.length;
        for (i = 0; i < length; i++) {
            result += Math.pow(10, i) * ((number.decimal[length - i - 1]) - 48);
        }
        if (number.expo != null) {
            int expo = 0;
            int lengthExpo = number.expo.length;
            for (i = 0; i < lengthExpo; i++) {
                expo += Math.pow(10, i) * ((number.expo[lengthExpo - i - 1]) - 48);
            }
            if (number.expoNegative) {
                expo = -expo;
            }
            result = Math.pow(result, expo);
        }
        if (number.fraction != null) {
            double fraction = 0;
            int lengthFraction = number.fraction.length;
            for (i = 0; i < lengthFraction; i++) {
                fraction += Math.pow(10, i) * ((number.fraction[lengthFraction - i - 1]) - 48);
            }
            result = result + (fraction <= 0 ? 0 : fraction / Math.pow(10, (int) (1 + Math.log10(fraction))));
        }
        if (number.isNegative) {
            result = -result;
        }
        if ((result % 1) == 0) {
            return (int) result;
        } else {
            return result;
        }
    }

    static final byte DEFAULTVALUE = 0;

    boolean isNegative = false;//must
    char[] decimal;//must
    char[] fraction;//can
    boolean expoNegative = false;//can
    char[] expo;//can

    private boolean negativeState(char[] string, int index) {
        if (string.length <= index) {
            return false;//Expecting digit (no char available)
        }
        if (string[index] == '-') {
            isNegative = true;
            return decimalState(string, index + 1);
        }
        return decimalState(string, index);
    }

    private boolean decimalState(char[] string, int index) {
        if (string.length <= index) {
            return false;//Expecting digit (no char available)
        }
        if (string[index] == '0') {
            decimal = new char[]{'0'};
            return fractionState(string, index + 1);
        }
        if (isDigitWithourZero(string[index])) {
            int startindex = index;//inclusive
            index++;
            while (string.length > index && isDigit(string[index])) {
                index++;
            }
            decimal = Arrays.copyOfRange(string, startindex, index);
            return fractionState(string, index);
        }
        return false;//Expecting Digit (wrong char available)
    }

    private boolean fractionState(char[] string, int index) {
        if (string.length <= index) {
            return true;
        }
        if (string[index] == '.') {
            index++;
            if (string.length > index && isDigit(string[index])) {
                int startIndex = index;
                index++;
                while (string.length > index && isDigit(string[index])) {
                    index++;
                }
                fraction = Arrays.copyOfRange(string, startIndex, index);
                return exponentState(string, index);
            } else {
                return false;//Expecting digit after point
            }
        }
        return exponentState(string, index);
    }

    private boolean exponentState(char[] string, int index) {
        if (string.length <= index) {
            return true;
        }
        if (string[index] == 'e' || string[index] == 'E') {
            index++;
            if (string.length <= index) {
                return false;//Expecting -, + or digit after e
            }
            if (string[index] == '-') {
                expoNegative = true;
                index++;
            } else if (string[index] == '+') {
                index++;
            }
            if (string.length <= index) {
                return false;//Expecting digit after +/-
            }
            if (isDigit(string[index])) {
                int startIndex = index;
                index++;
                while (string.length > index && isDigit(string[index])) {
                    index++;
                }
                expo = Arrays.copyOfRange(string, startIndex, index);
                return index == string.length;
            }
            return false; //Expecting digit after e/E or +/-
        }
        return false;//Expecting end of number
    }

    private static boolean isDigit(char achar) {
        return achar == '0' || achar == '1' || achar == '2' || achar == '3' || achar == '4' || achar == '5' || achar == '6' || achar == '7' || achar == '8' || achar == '9';
    }

    private static boolean isDigitWithourZero(char achar) {
        return achar == '1' || achar == '2' || achar == '3' || achar == '4' || achar == '5' || achar == '6' || achar == '7' || achar == '8' || achar == '9';
    }

}
