// ALL DONE
// JUST CAN NOT REMEMBER HOW TO FIGURE OUT IF LAST TOKEN....
// SINCE NO TREATMENT HAS TAKEN PLACE ON IT!!!
//SINCE 


/*
Online Java - IDE, Code Editor, Compiler

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

// why do we need stringjoiner, stringbuilder        and tokenizer  (split words)  

import java.util.*;


class checkLine
{
    
   
     int count=0;
     int k=16;
     
   
     
     String delimiter=" ";
     StringJoiner sj = new StringJoiner(delimiter);
     
     
     int lengthLineBeforeLastWord;
     String completeLine;
     
     String totalExtraPaddingBetweenWords=delimiter;
     int extraPaddingBetweenWords=0;
     String temp1;
     StringTokenizer st1;
    
     int wordCount;
     String temp;
     StringTokenizer st;
     String line;
     int lineLength;
     StringBuilder sb;
     String lineBeforeLastWord;
     //String sentence;
     String tokentoString;
     
     
     
     
     StringBuilder sb1 = new StringBuilder();
     
        
     String sentence="";
     StringJoiner sj1;
     StringJoiner sj2;
    
    public checkLine(int wordCount, String temp, StringTokenizer st, String line, int lineLength, StringBuilder sb, String tokentoString, String lineBeforeLastWord);
    {
        this.wordCount=wordCount;
        this.temp=temp;
        this.st=st;
        this.line=line;
        this.lineLength=lineLength;
        this.sb=sb;
        this.tokentoString=tokentoString;
        this.lineBeforeLastWord=lineBeforeLastWord;
        
        if (lineLength>k)
            {
                //need remove last token....
                //can not remember how I did that last time..remember
                
                //kept copy of stringjoiner...
                
                lengthLineBeforeLastWord= lineBeforeLastWord.length();
                
                System.out.println("rolled back:" + lineBeforeLastWord);
                System.out.println("Length rolled back line: " + lengthLineBeforeLastWord);
                
                wordCount--;  //wordCount has dropped by 1 since it has reduced number of words by 1
                
                //at this point there clearly is not enough buffer to allow an additional space between the characters...
                //it has to therefore force the spaces on the left hand side...
                
                
                int buffer = k-lengthLineBeforeLastWord;
                
                System.out.println("The current buffer is: " + buffer);
                System.out.println("Number words to accomodate for: " + (wordCount-1));
                
                //The buffer (1) is less than number words (3)
                
                //note that for every 3 words, 2 spaces are required....
                if (k-lengthLineBeforeLastWord<wordCount-1)
                {
                
                
                // if extra spaces can go between words (such as two between words),
                //this is preferable than adding padding at front... as per requirements....
                //it is only possible if there is no remainder, so modulus will be taken...
                //also, it is only valid if two or more words since StringJoiner will have no effect on 
                //one word!!!
                
                //so need to keep a count somewhere of number times wordCount appears on a line  after it has
                //been truncated
                
                //This will be perfect for situations with multiple words...
                //if there is a single word, it is only eligible for padding at front...
                //so it will go into else loop....
              
                if (buffer%(wordCount-1)==0 && wordCount!=1)
                {
                    extraPaddingBetweenWords = buffer%(wordCount-1);
                
                
                
                        //***************** need to think which loop is this going in.... ****************
                        // it can be nested in one above, but not required....
                        
                  for (int j=0; j< extraPaddingBetweenWords;j++)
                  {
                      totalExtraPaddingBetweenWords = totalExtraPaddingBetweenWords + " ";
                  }
                    //the only way to get extra spaces between the existing lineBeforeLastWord
                    // is to create another StringTokenizer with same delimiter ""
                    //Then to set up another StringJoiner with new delimiter as below...
                    
                    st1=new StringTokenizer(lineBeforeLastWord, "");
                    
                    sj2 = new StringJoiner(totalExtraPaddingBetweenWords);
                    
                    while (st1.hasMoreTokens())
                    {
                        //we know that all tokens will be part of the same line...
                        
                        temp1=st1.nextToken();
                        sj2.add(temp1);
                    }
                 
                } 
                    
                
                //*************************
                
                else
                {
                
                //all spaces appended at front to fill the buffer
                
                sb1.append(lineBeforeLastWord);
                
                for (int b=0; b<buffer; b++)
                {
                    count++;
                    sb1.insert(0," ");
                    System.out.println(count + " padding added at front");
                }
                
                completeLine = sb1.toString();
                System.out.println("First line completed:" + completeLine);
                
                //at this point, it needs to wipe out all the contents in the stringjoiner
                // it also needs to keep temp back into it since this was last word that was 
                //discarded.
                sj.add(temp);
                }
                
                //reset all StringJoiners, delete all StringBuilders...
                 sj=new StringJoiner(" ");
                 sj1=new StringJoiner(" ");
                 sj2=new StringJoiner(" ");
                
                //it also has to remove contents of both StringBuilders....
                sb1.delete(0,sb1.length());
                sb.delete(0,sb.length());
                
                wordCount=0;
                
                }
                
            }
        }
    
    
    
    
    
    
    
    //public checkLine()
//    {
        
  //  }
    
}



public class Main
{
    
     String text = "The quick brown fox jumps over the lazy dog";
     int lineLength;
     int wordCount=0;
     String temp;
     StringTokenizer st = new StringTokenizer(text, delimiter);
     String lineBeforeLastWord;
     //String sentence;
     String tokentoString;
    String line;
     
    
  
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        
        
        while (st.hasMoreTokens())
        {
            wordCount++;
            
            
            temp=st.nextToken();
            tokentoString = temp.toString();
            
            StringBuilder sb = new StringBuilder(tokentoString);
            
            lineBeforeLastWord = sj.toString();
            
            sj.add(sb);
            line = sj.toString();
            lineLength=line.length();
            
            
            System.out.println("your sentence getting bigger:" + line);
            
            System.out.println("current length: " + lineLength);
            System.out.println("number words: " + wordCount);
            
            //need to think about how to allocate spaces...
            //need to keep a count of the words
            
            //first thing is if the natural line has taken it over k,
            // need to truncate a word.....
            //count length of words...
            // subtract it from k....
            //if less than (wordCount-1)
            //pads at front....    (COMPLETE...)
            
            checkLine cl = new checkLine(wordCount, temp, st, line, lineLength,sb,tokentoString, lineBeforeLastWord);
            
        }
        
        //this is a chance for it to deal with unprocessed content in line String....
        while (!st.hasMoreTokens())
        {
            checkLine cl = new checkLine(wordCount, temp, st, line, lineLength,sb,tokentoString, lineBeforeLastWord);
            //this will be repeat code unless a seperate method created for above...!!!!!
        }
        
    }
    
  
    
}
