/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs
online.
*/

//Note I am using the term beads to refer to number of dots on each line

import java.util.*;

public class CentredHexagonalNumber
{
    static StringBuilder sb;
    static int R;
    static StringJoiner[] sj;
    static StringJoiner[] sj1;
    static int row;
    static StringJoiner sjDissector = new StringJoiner("");
    static int nthTerm;
    static int k;
    static long result=0;
    static int finalTotal=0;
    static boolean firstExecution=true;
    static String paddingAtFront="";
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        int n=999179;
        Map <Long, Long> mp = new HashMap<>();
        int H=0;
        int N=1000;
        int M;
        
        System.out.println("***CENTERED HEXAGONAL NUMBER***");
        
        for (int i=1;i<N;i++)
        {
            System.out.println("***************************************************");
            
            M=(i*2)-1;
                
            nthTerm = nthTerm+1;
            System.out.println("\nThis is the Nth term: " + (nthTerm));
            System.out.println("Middle disector (beads): " + M);
                
            R=i-1;
            
            sj = new StringJoiner[R];
            sj1 = new StringJoiner[R];
            
            
            System.out.println("Number rows above or below: " + R);
            
          
            if (R==0)
            {
                System.out.println("***Hexagon Arrangement***");
                System.out.println("O");
                
                finalTotal=1;
                    
                if (finalTotal==n && n!=0)
                {
                     System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                     System.exit(0);
                }
            }
                
            if (k!=1)
            {
                for (k=R; k>=1;k--)
                {
                    System.out.println("CURRENT RUNNING TOTAL BEADS: " + result);
                    System.out.println("Total full count: " + CentredHexagonalNumber(mp,i,M,k,R,n));
                }
            }
        }
    }
    
    public static long CentredHexagonalNumber (Map mp, int i, int M, int k,int R,int n)
    {
        if (nthTerm==66)
        {
            System.out.println("*****Code will terminate due to recursion limits*******");
            System.exit(0);
        }
        
        if (k>=1)
        {
            paddingAtFront="";
            System.out.println("BEADS in: " + (k) + " row above disector is: " + (M-k));
            
            //it gets the beads from top row (furtheest middle)
            //number of frontal padding is equal to row number (k)
            
            //goes through each row
            for (int count=0;count<R;count++)
            {
                row=count;
                sj[count]=new StringJoiner("");
                System.out.println("Value of k: " + k);
                System.out.println("Value of count: " + count);
                sb=new StringBuilder();
                
                for (int space=0; space<(M-k);space++)
                {
                    sb.append("O ");
                }
                
                for (int space=0; space<k;space++)
                {
                    paddingAtFront=paddingAtFront + "-";
                    System.out.println("TOTAL SPACE INSERTED: " + k);
                }
                
                System.out.println("R: " + R);
                System.out.println("Count: " + count);
                System.out.println("length sj: " + sj.length);
                //System.out.println("length sj1: " + sj1.length);
                System.out.println("This is front padding:" + paddingAtFront + "x");
                
                System.out.println("this is the row: " + sb);
                
                sj[row].add(paddingAtFront+sb);
                System.out.println("Stored in StringJoiner"+"["+row+"]" +  " as follows: " + sj[row]);
                row++;
                break;
            }
            
            
            //sb=new StringBuilder("");
            
            result=result + (M-k);
            //System.out.println("CURRENT RUNNING TOTAL BEADS: " + result);
            
            if (R!=1)
            {
                result = ((M-k)+(CentredHexagonalNumber (mp,i,M,(k-1),R,n)));
                
                System.out.println("number provided: " + n);
                System.out.println("Final Total: " + finalTotal);
                
                if (finalTotal>n)
                {
                    System.out.println("NOT A CENTRAL HEXAGON NUMBER: " + n);
                    System.exit(0);
                }
                
                if (finalTotal==n && n!=0)
                {
                     System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                     System.exit(0);
                }
               
                M=M+2;
                
                System.out.println("This is FinalTotal: " + finalTotal);
                
                finalTotal=0;
                
                nthTerm=nthTerm+1;
                
                System.out.println("\nThis is the Nth term: " + (nthTerm));
                
                StringBuilder sb = new StringBuilder();
                paddingAtFront="";
                sj=new StringJoiner[R];
                sj1=new StringJoiner[R];
                sjDissector = new StringJoiner("");
                row=0;
                firstExecution=true;
                R=nthTerm-1;
                
                System.out.println("Middle disector: " + M);
                
                result=0;
                
                k=R;
                
                if (k>=1)
                {   
                    if (firstExecution)
                    {
                        System.out.println("FIRST EXECUTION ONCE");
                        result = finalTotal+(CentredHexagonalNumber (mp,i,M,(k),R,n));
                        firstExecution=false;
                    }
                    else
                    {
                        result = finalTotal+(CentredHexagonalNumber (mp,i,M,(k-1),R,n));
                    }
                }
                if (k==0)
                {
                    return result;
                }
            }
            else
            {
                result = M-k;
            }
        }
        
        finalTotal  = (int)(result*2)+M;
        System.out.println("******FINAL TOTAL: " + finalTotal + "     (" + result + "x" + 2+")" + "+"+M);
        
        System.out.println("***Hexagon Arrangement***");
        
        for (int m=sj.length-1;m>=0;m--)
        {
            System.out.println(sj[m]);
        }
        
        sjDissector= new StringJoiner("");
        
        for (int t=0; t<M;t++)
        {
            sjDissector.add("O ");
        }
        
        System.out.println(sjDissector);
        
        for (int m=0; m<sj.length; m++)
        {
            System.out.println(sj[m]);
        }
        
        if (finalTotal==n && n!=0)
        {
            System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
            System.exit(0);
        }
        return finalTotal;
    }
}