import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * TNTRadical.java
 * 
 * Represents a simplified radical expression: c·√r (coefficient times square root of radicand)
 * Automatically simplifies radicals by extracting perfect square factors
 * 
 * Key Features:
 * - Stores original and simplified forms separately
 * - Automatically simplifies radicals (e.g., √300 becomes 10√3)
 * - Calculates and caches the numeric value
 * - Shows both exact (rational) and approximate (irrational) results
 * - Uses mathematical symbols: · for multiplication, √ for radical, ≈ for approximation
 * 
 * Radical Simplification Process:
 * 1. Find prime factors of the radicand
 * 2. Extract perfect square factors (pairs of same primes)
 * 3. Move extracted factors outside the radical as coefficients
 * 4. Keep the remaining primes inside the radical
 * 
 * Example Simplifications:
 * - √12 = √(4·3) = 2√3
 * - 2·√300 = 2·√(100·3) = 2·10√3 = 20√3
 * - √100 = 10 (perfect square: exact result)
 * - √2 ≈ 1.414 (irrational: approximate result)
 * 
 * Example Usage:
 *   TNTRadical rad1 = new TNTRadical(1, 12);         // √12
 *   System.out.println(rad1.toString());              // "√12 = 2√3 ≈ 3.464"
 *   
 *   TNTRadical rad2 = new TNTRadical(2, 300);         // 2√300
 *   System.out.println(rad2.toString());              // "2·√(300) = 20·√(3) ≈ 34.641"
 *   
 *   TNTRadical rad3 = new TNTRadical(1, 100);         // √100
 *   System.out.println(rad3.toString());              // "√(100) = 10" (exact)
 * 
 * Use Cases:
 * - Algebra: Simplify radical expressions step-by-step
 * - Number theory: Understand perfect square factorization
 * - Educational: Show how radicals are simplified mathematically
 * - Data structures: Store TNTRadical objects in ArrayLists
 * 
 * @author TNT (Teacher and Tutor) Team
 * @version 1.0
 */
public class TNTRadical {
    private long origCoeff;
    private long origRadicand;
    private long coeff;
    private long radicand;
    private double value;
    private DecimalFormat df = new DecimalFormat("###,###");

    /**
     * Default constructor
     * Creates TNTRadical(0, 1) = 0·√1 = 0
     */
    public TNTRadical(){
        origCoeff = 0;
        origRadicand = 1;
        coeff = 0;
        radicand = 1;
        value = 0;
    }//end TNTRadical()

    /**
     * Constructor with long coefficient and radicand
     * Automatically simplifies the radical
     * @param c the coefficient (long)
     * @param r the radicand (long, the value under the √)
     */
    public TNTRadical(long c, long r){
        origCoeff = c;
        origRadicand = r;
        coeff = c;
        radicand = r;
        value = coeff * Math.sqrt(radicand);
        simplify();
    }//end TNTRadical(long, long)

    /**
     * Constructor with int coefficient and radicand
     * Automatically simplifies the radical
     * @param c the coefficient (int)
     * @param r the radicand (int, the value under the √)
     */
    public TNTRadical(int c, int r){
        origCoeff = c;
        origRadicand = r;
        coeff = c;
        radicand = r;
        value = coeff * Math.sqrt(radicand);
        simplify();
    }//end TNTRadical(int, int)

    /**
     * Finds all prime factors of n using trial division
     * @param n the number to factor
     * @return ArrayList<Long> of all prime factors (with duplicates)
     */
    public static ArrayList<Long> factor(long n){
        ArrayList<Long> factors = new ArrayList<Long>();
        if(n == 1){
            factors.add(1L);
            return factors;
        }
        for(long i = 2; i * i <= n; i++){
            while(n % i == 0){
                factors.add(i);
                n /= i;
            }
        }
        if(n > 1){
            factors.add(n);
        }
        return factors;
    }//end factor

    /**
     * Checks if a number is prime
     * @param n the number to check
     * @return true if n is prime, false otherwise
     */
    public static boolean isPrime(long n){
        if(n < 2) return false;
        if(n == 2) return true;
        if(n % 2 == 0) return false;
        for(long i = 3; i * i <= n; i += 2){
            if(n % i == 0) return false;
        }
        return true;
    }//end isPrime

    /**
     * Consolidates prime factors into pairs (factor, count)
     * Example: [2, 2, 2, 3, 5, 5] becomes [2, 3, 3, 1, 5, 2]
     * @param factors ArrayList of prime factors (with duplicates)
     * @return ArrayList with alternating [prime, count, prime, count, ...]
     */
    public static ArrayList<Long> consolidate(ArrayList<Long> factors){
        ArrayList<Long> result = new ArrayList<Long>();
        if(factors.isEmpty()) return result;
        
        long current = factors.get(0);
        long count = 1;
        
        for(int i = 1; i < factors.size(); i++){
            if(factors.get(i) == current){
                count++;
            } else {
                result.add(current);
                result.add(count);
                current = factors.get(i);
                count = 1;
            }
        }
        result.add(current);
        result.add(count);
        return result;
    }//end consolidate

    /**
     * Simplifies the radical by extracting perfect square factors
     * Example: √300 = √(100·3) = 10√3
     * Updates coeff and radicand; recalculates value
     */
    public void simplify(){
        if(radicand == 0 || radicand == 1) return;
        
        ArrayList<Long> primeFactors = factor(radicand);
        ArrayList<Long> consolidated = consolidate(primeFactors);
        
        long newCoeff = coeff;
        long newRadicand = 1;
        
        for(int i = 0; i < consolidated.size(); i += 2){
            long prime = consolidated.get(i);
            long exponent = consolidated.get(i + 1);
            
            if(exponent % 2 == 1){
                long extracted = (exponent - 1) / 2;
                newCoeff *= (long)Math.pow(prime, extracted + 1);
                newRadicand *= prime;
            } else {
                long extracted = exponent / 2;
                newCoeff *= (long)Math.pow(prime, extracted);
            }
        }
        
        coeff = newCoeff;
        radicand = newRadicand;
        value = coeff * Math.sqrt(radicand);
    }//end simplify

    /**
     * Returns string representation showing original, simplified, and value
     * Shows exact integer results or approximate decimals for irrationals
     * Uses mathematical symbols: · (dot) for multiplication, √ for radical, ≈ for approximation
     * @return String representation with appropriate format
     */
    public String toString(){
        if(radicand == 1){
            // Perfect square case
            return df.format(origCoeff) + "·√(" + origRadicand + ") = " + df.format(coeff);
        } else if(coeff == 0){
            return "0";
        } else {
            long sqrtRad = (long)Math.sqrt(radicand);
            if(sqrtRad * sqrtRad == radicand){
                // Exact value (rational)
                long exactValue = coeff * sqrtRad;
                if(origCoeff == coeff && origRadicand == radicand){
                    return df.format(coeff) + "·√(" + radicand + ") = " + df.format(exactValue);
                } else {
                    return df.format(origCoeff) + "·√(" + origRadicand + ") = " + df.format(coeff) + "·√(" + radicand + ") = " + df.format(exactValue);
                }
            } else {
                // Irrational: show approximate decimal
                if(origCoeff == coeff && origRadicand == radicand){
                    return df.format(coeff) + "·√(" + radicand + ") ≈ " + String.format("%.3f", value);
                } else {
                    return df.format(origCoeff) + "·√(" + origRadicand + ") = " + df.format(coeff) + "·√(" + radicand + ") ≈ " + String.format("%.3f", value);
                }
            }
        }
    }//end toString

    /**
     * Gets the simplified radicand (value under the √)
     * @return long the simplified radicand
     */
    public long getRadicand(){
        return radicand;
    }//end getRadicand

    /**
     * Gets the simplified coefficient
     * @return long the simplified coefficient
     */
    public long getCoeff(){
        return coeff;
    }//end getCoeff

    /**
     * Gets the original radicand (before simplification)
     * @return long the original radicand
     */
    public long getOrigRadicand(){
        return origRadicand;
    }//end getOrigRadicand

    /**
     * Gets the original coefficient (before simplification)
     * @return long the original coefficient
     */
    public long getOrigCoeff(){
        return origCoeff;
    }//end getOrigCoeff

    /**
     * Gets the computed numeric value (coefficient × √radicand)
     * @return double the numeric value
     */
    public double getValue(){
        return value;
    }//end getValue

}//end class TNTRadical
