/*
 * This program is a client application that resolves a reference to the trading service
 * and performs simple Trading Service Object look-ups based on the static properties describing
 * video-on-demand suppliers.
 */

#include <CosTradingC.h>

#include "Common/ServerC.h"

#include <tao/corba.h>

int
main (int argc, char **argv)
{
  try
    {
      // Initialise the ORB.
      CORBA::ORB_var orbVar = CORBA::ORB_init (argc, argv);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("INFO: Initialised ORB\n")));

      // Get a reference to the trading service.
      CORBA::Object_var objVar =
        orbVar->resolve_initial_references (ACE_TEXT ("TradingService"));
      CosTrading::Lookup_var lookupVar =
        CosTrading::Lookup::_narrow (objVar.in ());

      if (CORBA::is_nil (lookupVar.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("Error: nil reference.\n")),
                            1);
        }

      // Set up an empty policy and execute the query.
      CosTrading::PolicySeq policies;
      CosTrading::Lookup::SpecifiedProps desiredProps;
      CosTrading::OfferSeq_var offersVar;
      CosTrading::PolicyNameSeq_var policiesAppliedVar;
      CosTrading::OfferIterator_var iteratorVar;
      CosTrading::PropertyNameSeq propertySeq;

      propertySeq.length (1);
      propertySeq[0] = CORBA::string_dup (ACE_TEXT ("title"));
      desiredProps.prop_names (propertySeq);

      lookupVar->query (ACE_TEXT ("IDL:video_supplier:1.0"),
                        ACE_TEXT (""),
                        ACE_TEXT (""),
                        policies,
                        desiredProps,
                        0,
                        offersVar,
                        iteratorVar,
                        policiesAppliedVar);

      // Process the results.
      if (!CORBA::is_nil (iteratorVar.in ()))
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Trader lookup for all references\n")));

          CORBA::Boolean more;
          int count1 = 0;
          do
            {
              more = iteratorVar->next_n (10, offersVar.out ());
              if (more)
                {
                  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%i available titles.\n"), offersVar->length ()));
                  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("===================\n")));
                  for (CORBA::ULong count2 = 0; count2 < offersVar->length (); ++count2)
                    {
                      // Print the information for each offer.
                      const char *temp = 0;
                      (*offersVar)[count2].properties[0].value >>= temp;
                      CORBA::String_var titleVar (temp);
                      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Title: %s\n"), titleVar.in ()));
                    }
                }
              else
                {
                  if (count1 == 0)
                    {
                      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("INFO: No offers found\n")));
                    }
                  else
                    {
                      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("INFO: No more offers found\n")));
                    }
                }

              ++count1;
            } while (more);

          iteratorVar->destroy ();
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("WARNING: No iterator returned from query\n")));
        }

      orbVar->destroy ();
    }
  catch (const CORBA::Exception &ex)
    {
      ex._tao_print_exception (ACE_TEXT ("ERROR: Exception in TradingClient.cpp:"));
      return 1;
    }

  // Complete.
  return 0;
}
