//
// invoke_ex
//
int IDL_Generator_Component_Impl::
invoke_ex (GAME::Mga::Project project,
           GAME::Mga::FCO focus,
           GAME::Mga::Collection_T <GAME::Mga::FCO> & selected,
           long flags)
{
  GAME::Mga::Readonly_Transaction t (project);

  if (this->is_interactive_ || this->output_.empty ())
  {
    // Select the output directory for the files.
    if (!GAME::Utils::get_path ("Select target output directory...", this->output_))
      return -1;
  }

  // Generate all the IDL files.
  IDL_Generator_Visitor visitor (this->output_);
  PICML::RootFolder root_folder = project.root_folder ();
  root_folder->accept (&visitor);

  if (this->is_interactive_)
    ::AfxMessageBox ("Successfully generated IDL files");

  return 0;
}
//
//invoke ex
//
int PICML_MPCComponent_Impl::
invoke_ex (GAME::Mga::Project project,
           GAME::Mga::FCO_in focus,
           GAME::Mga::Collection_T <GAME::Mga::FCO> & selected,
           long flags)
{
  GAME::Mga::Readonly_Transaction t (project);

	std::string outputPath;
	std::string message = "Please specify the Output Directory";
	if (! Utils::getPath (message, outputPath))
		return -1;

	bool valid_interpretation = 0;

  if (focus || selected.count ())
  {
    PICML::MPC_Visitor visitor (outputPath);

    for (GAME::Mga::FCO target : selected)
    {
      // dfeiock: FIX - Original code put the selected and focus into a set then interated over them.
      //                Now I am doing selected first then the focus.
      //                not sure if the automatic sorting of the set is required
      if (target->meta ()->name () == PICML::MPC::impl_type::metaname)
      {
        target->accept (&visitor);
        valid_interpretation = 1;
      }
    }

    if (focus && (focus->meta ()->name () == PICML::MPC::impl_type::metaname))
    {
      focus->accept (&visitor);
      valid_interpretation = 1;
    }
  }

	if (!valid_interpretation)
	{
		PICML::RootFolder root_folder = project.root_folder ();

		for (PICML::ComponentBuild component : root_folder->get_ComponentBuild ())
		{
			for (PICML::MPC mpc : component->get_MPCs ())
			{
				PICML::MPC_Visitor visitor (outputPath);
        mpc->accept (&visitor);
				valid_interpretation = 1;
			}
		}
	}

	if (valid_interpretation)
		AfxMessageBox ("MPC Files successfully generated");
	else
		AfxMessageBox ("Interpretation Failure: Either there is no MPC paradigm present and/or \n Selected Object(s) are not MPC definitions");

  return 0;
}
//
// invoke_ex
//
int Extension_Classes_Component_Impl::
invoke_ex (GAME::Mga::Project project,
           GAME::Mga::FCO_in focus,
           GAME::Mga::Collection_T <GAME::Mga::FCO> & selected,
           long flags)
{
  try
  {
    // Start a new transaction.
    GAME::Mga::Transaction t (project);

    // Load the project settings for next time.
    this->load_project_settings (project);

    if (this->is_interactive_)
    {
      Main_Dialog dialog (::AfxGetMainWnd ());

      // Initialize the dialog before showing it.
      dialog.opts_.output_dir_ = this->output_.c_str ();
      dialog.opts_.exclude_mwc_ = this->exclude_mwc_;

      // Show the dialog.
      if (dialog.DoModal () == IDCANCEL)
        return 0;

      // Store the selected values.
      this->output_ = dialog.opts_.output_dir_.GetBuffer ();
      this->exclude_mwc_ = dialog.opts_.exclude_mwc_;
    }

    const std::string pch_basename = "stdafx";
    GAME::Mga::Folder root = project.root_folder ();

    // First, build all the elements that we are interested in generating
    // an extension class. The builder will build each object, and any
    // dependent objects of the object.
    Object_Builder builder;
    root->accept (&builder);

    std::ostringstream ostr;
    ostr << "Generating extension classes for " << OBJECT_MANAGER->objects ().current_size () << " objects...";
    GME_CONSOLE_SERVICE->info (ostr.str ().c_str ());

    // Visit all elements in the model and gather each object that
    // should be generated as an extension class.
    std::set <GAME::Mga::Object> ext_classes;
    GAME::Mga::Extension_Classes_Visitor ecv (this->output_, root, pch_basename);

    for (const auto & entry : OBJECT_MANAGER->objects ())
      ecv.generate (entry.item ());

    // Generate workspace, project, and precompiled header files.
    if (!this->exclude_mwc_)
    {
      GAME::Mga::Mwc_File_Generator mwc_gen;
      mwc_gen.generate (this->output_, project);
    }

    GAME::Mga::Mpc_File_Generator mpc_gen;
    mpc_gen.generate (this->output_, project, pch_basename, OBJECT_MANAGER);

    GAME::Mga::Pch_File_Generator pch_gen;
    pch_gen.generate (this->output_, project, pch_basename);

    GAME::Mga::Fwd_Decl_Generator fwd_gen;
    fwd_gen.generate (this->output_, project, OBJECT_MANAGER);

    GAME::Mga::Visitor_Generator visitor_gen;
    visitor_gen.generate (this->output_, project, pch_basename, OBJECT_MANAGER);

    GAME::Mga::Init_Generator init_gen;
    init_gen.generate (this->output_, project, pch_basename);

    GAME::Mga::Impl_Factory_Generator impl_factory_gen;
    impl_factory_gen.generate (this->output_, project, pch_basename, OBJECT_MANAGER);

    GAME::Mga::Top_Level_File_Generator top_level_gen;
    top_level_gen.generate (this->output_, project, OBJECT_MANAGER);
    
    GAME::Mga::Export_File_Generator export_file_gen;
    export_file_gen.generate (this->output_, project);

    GAME::Mga::RootFolder_Generator root_folder_gen;
    root_folder_gen.generate (this->output_, project, pch_basename, OBJECT_MANAGER);

    if (this->is_interactive_)
      ::AfxMessageBox ("Successfully generated extension class files.", MB_OK | MB_ICONINFORMATION);

    // Save the project settings for next time.
    this->save_project_settings (project);
    t.commit ();
  }
  catch (const GAME::Mga::Exception & ex)
  {
    if (this->is_interactive_)
      ::AfxMessageBox (ex.message ().c_str (), MB_OK);
  }

  return 0;
}