with Ada.Containers.Indefinite_Vectors;
with Ada.Text_IO;
with Parse_Args;

package body Aquarius.Options is

   use Parse_Args;

   AP : Argument_Parser;

   package String_Vectors is
     new Ada.Containers.Indefinite_Vectors (Positive, String);

   Source_File_Vector : String_Vectors.Vector;

   Aqua_Trace_Option   : constant String := "aqua trace";
   Code_Trigger_Option : constant String := "code trigger";
   Start_Class_Option  : constant String := "start class";
   Self_Test_Option    : constant String := "self test";
   Help_Option         : constant String := "help";

   Show_Full_Path_Option : constant String := "show full path";
   Report_Files_Option   : constant String := "report files";

   Tagatha_Trace_P_Code_Option       : constant String := "tagatha p-code";
   Tagatha_Trace_Transfers_Option    : constant String := "tagatha transfers";
   Tagatha_Trace_Improvements_Option : constant String :=
                                         "tagatha improvements";

   ----------------
   -- Aqua_Trace --
   ----------------

   function Aqua_Trace return Boolean is
   begin
      return AP.Boolean_Value (Aqua_Trace_Option);
   end Aqua_Trace;

   ------------------
   -- Code_Trigger --
   ------------------

   function Code_Trigger return Boolean is
   begin
      return AP.Boolean_Value (Code_Trigger_Option);
   end Code_Trigger;

   ----------
   -- Load --
   ----------

   function Load return Boolean is
   begin

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Aqua_Trace_Option,
         Long_Option   => "aqua-trace",
         Usage         => "Enable trace for Aqua execution");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Code_Trigger_Option,
         Long_Option   => "code-trigger",
         Usage         => "Run the code trigger on source files");

      AP.Add_Option
        (O             => Make_String_Option (""),
         Name          => Start_Class_Option,
         Long_Option   => "start-class",
         Usage         => "Create and run the Aqua class found in this path");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Self_Test_Option,
         Long_Option   => "self-test",
         Usage         => "Run unit tests");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Help_Option,
         Long_Option   => "help",
         Usage         => "Show help");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Show_Full_Path_Option,
         Long_Option   => "show-full-path",
         Usage         => "use full path when reporting file names");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Report_Files_Option,
         Long_Option   => "report-files",
         Usage         => "report all filesystem activity");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Tagatha_Trace_Improvements_Option,
         Long_Option   => "tagatha-trace-improvements",
         Usage         => "Log code improvements applied by Tagatha");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Tagatha_Trace_P_Code_Option,
         Long_Option   => "tagatha-trace-p-code",
         Usage         => "Log code p-code generated by Tagatha");

      AP.Add_Option
        (O             => Make_Boolean_Option (False),
         Name          => Tagatha_Trace_Transfers_Option,
         Long_Option   => "tagatha-trace-transfers",
         Usage         => "Log code transfers generated by Tagatha");

      AP.Allow_Tail_Arguments ("files ...");

      AP.Parse_Command_Line;

      if not AP.Parse_Success then
         Ada.Text_IO.Put_Line
           (Ada.Text_IO.Standard_Error,
            AP.Parse_Message);
         return False;
      end if;

      if AP.Boolean_Value (Help_Option) then
         AP.Usage;
         return False;
      end if;

      for File_Name of AP.Tail loop
         Source_File_Vector.Append (File_Name);
      end loop;

      return True;

   end Load;

   ------------------
   -- Report_Files --
   ------------------

   function Report_Files return Boolean is
   begin
      return AP.Boolean_Value (Report_Files_Option);
   end Report_Files;

   ---------------
   -- Self_Test --
   ---------------

   function Self_Test return Boolean is
   begin
      return AP.Boolean_Value (Self_Test_Option);
   end Self_Test;

   --------------------
   -- Show_Full_Path --
   --------------------

   function Show_Full_Path return Boolean is
   begin
      return AP.Boolean_Value (Show_Full_Path_Option);
   end Show_Full_Path;

   -----------------
   -- Source_File --
   -----------------

   function Source_File (Index : Positive) return String is
   begin
      return Source_File_Vector (Index);
   end Source_File;

   -----------------------
   -- Source_File_Count --
   -----------------------

   function Source_File_Count return Natural is
   begin
      return Source_File_Vector.Last_Index;
   end Source_File_Count;

   -----------------
   -- Start_Class --
   -----------------

   function Start_Class return String is
   begin
      return AP.String_Value ("start class");
   end Start_Class;

   --------------------------------
   -- Tagatha_Trace_Improvements --
   --------------------------------

   function Tagatha_Trace_Improvements return Boolean is
   begin
      return AP.Boolean_Value (Tagatha_Trace_Improvements_Option);
   end Tagatha_Trace_Improvements;

   --------------------------
   -- Tagatha_Trace_P_Code --
   --------------------------

   function Tagatha_Trace_P_Code return Boolean is
   begin
      return AP.Boolean_Value (Tagatha_Trace_P_Code_Option);
   end Tagatha_Trace_P_Code;

   -----------------------------
   -- Tagatha_Trace_Transfers --
   -----------------------------

   function Tagatha_Trace_Transfers return Boolean is
   begin
      return AP.Boolean_Value (Tagatha_Trace_Transfers_Option);
   end Tagatha_Trace_Transfers;

end Aquarius.Options;
