// Copyright and License 
 
import GraphicsApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

// because we need GraphicalObject we have to do an implementation of it here. 
// reversing the HelloServer.java here
class Graph extends GraphicalObjectPOA {
    private boolean filled;
    private String name;
    private ORB orb;

    public Graph(String name, boolean filled) {
        this.name = name;
        this.filled = filled;
    }

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

    // draw
    public void draw() {
        System.out.println("Drawing graph: " + name);
    }
    
    // show info
    public void showInfo() {
        System.out.println("Graph Info -> Name: " + name + ", Filled: " + filled);
    }

    // getter for is filled
    public boolean is_filled() {
        return filled;
    }

    // setter for is filled
    public void is_filled(boolean value) {
        this.filled = value;
    }


    // name getter, not setting it.
    public String name() {
        return name;
    }
}

public class GraphicsClient
{
  static Rectangle rect;

  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();

        // reversing steps from HelloServer here as well.
        // create servant and register it with the ORB
        Graph graph = new Graph("Circle", true);
        graph.setORB(orb); 

        // get object reference from the servant
        org.omg.CORBA.Object ref = rootpoa.servant_to_reference(graph);
        GraphicalObject object = GraphicalObjectHelper.narrow(ref);


        // get the root naming context
        org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
        
        // Use NamingContextExt instead of NamingContext. This is 
        // part of the Interoperable naming Service.  
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
 
        // resolve the Object Reference in Naming
        String name = "Graphics";
        rect = RectangleHelper.narrow(ncRef.resolve_str(name));


        System.out.println("Obtained a handle on server object: " + rect);
        rect.addObject(object);
	    rect.removeObjectAtPosition(0);

        } catch (Exception e) {
          System.out.println("ERROR : " + e) ;
          e.printStackTrace(System.out);
          }
    }

}
