// package Cases;

// CSC 468. Project Stage 1. 
// Test File for the ElementIndex structure.
// 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 DiskAddress.*
import neustore.base.DiskAddress;
import java.io.File;


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

public class ElementIndexTest {

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

		//all the pages will be stored under this file name
		String filename1="TESTFILE";
		File file= new File(filename1);
		if(file.delete())
		{
			 System.out.println("File already exists: deleted");
			 
		}
		else
		{
			System.out.println("File not found");
		}
		
		//create a buffer with 5 slots. 
		LRUBuffer buf= new LRUBuffer (5, pageSize);
		
		// Create new ElementIndex structure
		ElementIndex EI = new ElementIndex(buf,filename1,1);
		
		
		//insert "<root>"
		int id1=EI.insertElementName("root");
		
		//print Id of <root>
		if(id1>=0)
		{
		System.out.println("<root> id: "+id1);
		}
		else
		{
			System.out.println("The 'root' element has not been inserted");
		}
		
		
		//Find Id of <root> via API call
		
		int id2= EI.getElementId("root");
		System.out.println("getElementID(<root>): "+id2);
		
		//this code will check if the id1 and id2 are equal, if they are then insertion and retreival are running good
		if(id1==id2)
		{
			System.out.println("Ids match: PASS");
		}
		else
		{
			System.out.println("Ids don't match: FAIL");
		}
		
	   
		//The follwing code will try to get an element name given an id
		String ename=EI.getElementName(id1);  //this statement should return "root"
		
		System.out.println("getElementName("+id1+"): "+ename);
		
		if(ename!=null &&ename.equals("root") )
		{
			System.out.println("getElementName: PASS");
			
		}
		else
		{
			System.out.println("getElementName: FAIL");
		}
		
	
                // Create some disk addresses, insert some records into ElementIndex

                DiskAddress address1 = DiskAddress.createAddressByRN(5, 10);
                DiskAddress address2 = DiskAddress.createAddressByRN(7, 15);

                System.out.println("Addresses created:");
                System.out.println(address1.toString());
                System.out.println(address2.toString());
                 
                // Insert two element entries, print status.
                int status = EI.insertElementEntry("root", address1);
                System.out.println("insertElementEntry(<root>,"+address1.toString()+"): "+ status);
                
                status = EI.insertElementEntry("root", address2);
                System.out.println("insertElementEntry(<root>,"+address2.toString()+"): "+ status);

		
		//Find where the element entries are stored
		DiskAddress da=EI.getAddress("root");
		
		if(da!=null)
		{
                   System.out.println("getAddress(<root>): "+ da.toString());

		}
		else 
                 {
                   System.out.println("getAddress(<root>): FAIL");
				
		
		//Test insertion of entries by Id
		DiskAddress address3= DiskAddress.createAddressByRN(3,2); 
		status=EI.insertElementEntryById(id2,address3);
    	        System.out.println("insertElementEntryById("+id2+"  "+address3.toString()+"): "+ status);


 	       //Follwing method would tell us if the insertion of element entries went as planned
		int nEntries = EI.getNumberEntries(id2);
		
                System.out.println("getNumberEntries("+id2+"): "+ nEntries);
		
                DiskAddress address4;
		for (int k=1; k<=nEntries;k++) {
                    address4 = EI.getElementEntry(id2, k);
                    System.out.println("getElementEntry("+id2+", "+k+"): "+ address4.toString()); 
                }
		
		
		
		//Delete entries
                status = EI.deleteElementEntry("root",address4);
                System.out.println("deleteElementEntry(<root>,"+address4.toString()+"): "+status);
                System.out.println("getNumberEntries("+id2+"): "+ EI.getNumberEntries(id2));

                address4 = EI.getElementEntry(id2, 2);
		int k=EI.deleteElementEntryById(id2,address4);
                System.out.println("deleteElementEntryById("+id2+","+address4.toString()+"): "+status);
                System.out.println("getNumberEntries("+id2+"): "+ EI.getNumberEntries(id2));
		
				
//		EI.close();//remove the comments if the file needs to be closed before exiting
		

	}//End of main() method

}