// GraphicsServer.java

// I am not testing this server for fear of errors 
// because if there are errors, I wont stop trying to debugg it untill tomorrow.
import GraphicsApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.*;


// overriding the rectangle Class. 
// copying from the Hello Project, I believe we extend the RectanglePOA class
class Rect extends RectanglePOA {
    private ORB orb;
    private GraphicalObject[] objects;

    public Rect() {
        this.objects = new GraphicalObject[0];
    }

    public void setORB(ORB orb_val) {
        this.orb = orb_val; 
    }

    // getter
    public GraphicalObject[] objects() {
        return this.objects;
    }

    // setter
    public void objects(GraphicalObject[] newObject) {
        this.objects = newObject;
    }

    // add objects
    public void addObject(GraphicalObject newObject) {
        List<GraphicalObject> temp = new ArrayList<>(Arrays.asList(this.objects));
        temp.add(newObject);
        this.objects = temp.toArray(new GraphicalObject[0]);
    }


    // remove an object
    public void removeObjectAtPosition(int index)
        throws GraphicsApp.RectanglePackage.IndexOutOfBounds {

        if (index < 0 || index >= this.objects.length) {
            throw new GraphicsApp.RectanglePackage.IndexOutOfBounds();
        }

        List<GraphicalObject> temp = new ArrayList<>(Arrays.asList(this.objects));
        temp.remove(index);
        this.objects = temp.toArray(new GraphicalObject[0]);
    }
}


public class GraphicsServer {

  public static void main(String args[]) {
    try{
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // get reference to rootpoa and activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      Rect rect = new Rect();
      rect.setORB(orb); 

      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(rect);
      Rectangle href = RectangleHelper.narrow(ref);
          
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");

      // Use NamingContextExt which is part of the Interoperable
      // Naming Service (INS) specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Graphics";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println("GraphicsServer ready and waiting ...");

      // wait for invocations from clients
      orb.run();
    } 
        
      catch (Exception e) {
        System.err.println("ERROR: " + e);
        e.printStackTrace(System.out);
      }
          
      System.out.println("GraphicsServer Exiting ...");
        
  }
}
