// package Cases;

// CSC 468. Project Stage 1. 
// Test File for the full test
// Alex Dekhtyar

// Note: the organization of your project may require somewhat different import statements.
//       Please, edit this test file accordingly.
//


import java.io.*;
import ElementIndex.ElementIndex;
import StructureIndex.*;
import AttributeIndex.*;
import ContentIndex.*;
import StructureIndexRecord.*;
import DiskAddress.*

import java.io.File;


import neustore.base.DBIndex;
import neustore.base.LRUBuffer;

public class FullTests {

	
	public static void main(String[] args)throws IOException {
		
                int pageSize = 4096;  // Size of a single buffer/disk page. Insert your size HERE!

		// Data file names for MiniNXBase index structures.
                // if you store data in less than four files, modify accordingly
		String Elements="Elements.dat";              // for ElementIndex
		String Structure= "Structure.dat";           // for StructureIndex
		String Content="Content.dat";                // for ContentIndex
		String Attributes="Attributes.dat";          // for AttributeIndex

		File fileElements= new File(Elements);
		File fileStructure= new File(Structure);
		File fileContent= new File(Content);
		File fileAttributes= new File(Attributes);

		
		//create a buffer with 100 slots. 
		LRUBuffer buf= new LRUBuffer (100, pageSize);
		
		// Create new structures
		ElementIndex EI = new ElementIndex(buf,Elements,1);
		StructureIndex SI = new StructureIndex(buf,Structure,1);
		ContentIndex CI = new ContentIndex(buf,Content,1);
		AttributeIndex AI = new AttributeIndex(buf,Elements,1);
		

               // Insert element names
                int i;
                String xmlElementName;
                String xmlBaseName = "xmlnode";
                for (i=1; i<= 30; i++) { 
          
                  xmlElementName = xmlBaseName +   Integer.toString(i);   // construct a new element name
                  int elementId = EI.InsertElementName(xmlElementName);   // insert it into Element Index
                  System.out.println("Inserted Element Id: "+ elementId + " --> "+ EI.getElementName(elementId));   // print message
                }

                // Insert Elements and content

               for (i=1; i <= 10000; i++) {
                    
                 // generate StructureIndexRecord
                 // these assignments are for clarity's sake
                 //  note that the actual StructureIndex Records produced will be ill-formed, i.e., they won't represent an XML document
                 int nodeId = i;                             //  nodeID
                 int elementID = i mod 30+1;                 // element Id
                 int PreOrder = i;                           // PreOrder
                 int PostOrder = 20000 - i+1;                // PostOrder
                 int Ordinal = i mod 7;                      // Ordinal
                 int Parent = i / 2;                         // Parent
                 int Layer = i  mod 50;                      // Layer
                 int isLeaf = 0;                             // Leaf node flag
                 if (Layer >= 45) {isLeaf = 1;}
                 DiskAddress ptrContent = null;        
                 
                 // If we are inserting a leaf, let's generate its content and insert it first
                 if (isLeaf) {
                   String contentString = "This is a test number " + Integer.toString(i) + "!";       // generate Content
                   ptrContent = CI.insertContent(contentString);                                      // insert, obtain address
                 }
                  // Construct StructureIndex Record 
                  StructureIndexRecord  record = new StructureIndexRecord(nodeId, elementId, PreOrder, PostOrder, Ordinal, Parent, Layer, isLeaf, ptrContent);
                  
                  // insert it into Structure Index
                  DiskAddress recordAddress = SI.insertEntry(record);       
           
                }     // for

                // now let's insert some attributes

                for (i =1; i<=10000; i++) {
                   // get the record first
                  StructureIndexRecord record = SI.getRecordById(i);
                   // depending on the value of ElementId, set attributes
                   if (record.elementId == 15) {
                      AI.insertEntry(i, "boo", Integer.toString(i*2 - 5);  
                   }
                   if (record.Layer >= 40) {
                      AI.insertEntry(i, "woohoo", Integer.toString(record.Parent);  
                   }
                   if (record.elementId >=3 && record.elementId <= 10) {
                      AI.insertEntry(i, "Id", Integer.toString(i);  
                      AI.insertEntry(i, "Hi", Integer.toString(i-1);  
                      AI.insertEntry(i, "Fi", Integer.toString(i-2);  
                      AI.insertEntry(i, "boo", Integer.toString(i-3);  

                  }
               } // for


               // Let's print some output.

               for (i=1; i<=100, i++) {
                
                  int nodeId = (i-1)*100 +1;  // set record id.
                 
                  DiskAddress recordAddress = SI.getAddress(nodeId);                    // get record address
                  StructureIndexRecord record = SI.getRecordByAddress(recordAddress);   // get record
                  int element = record.elementId;                                       // find element id
                  String elementName = EI.getElementName(element);                      // find element name
                  
                  System.out.print("Record: "+ record.toString());
                  System.out.println("Element: " + elementName);
                  if (record.isLeaf==1) {                                              // if it's a leaf, let's get the content
                     String content = CI.getContent(record.ptrContent);
                     System.out.printf("      Content:  "+ content);                   // and print it
                    
                     // also, if this is a leaf, we can get the attribute
                     String woohoo = AI.getValue(i, "woohoo");
                    System.out.printf("       Attribute woohoo = "+ woohoo);
                     
                  }              

                
               } // for


	}//End of main() method

}