#############################################################################
##
#W  atlasbrowse.g         GAP 4 package `browse'                Thomas Breuer
##
#Y  Copyright (C)  2006,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
##


#############################################################################
##
#F  BrowseData.AtlasRepGroupInfoRepsTable( <name>, <log>, <replay> )
##
##  This function is called by `AtlasRepOverviewTable' when a row in the
##  overview table is clicked.
##  It returns a browse table with the details for the chosen group,
##  which is shown via a second level `Browse' call.
##
BrowseData.AtlasRepGroupInfoRepsTable:= function( name, log, replay )
    local tocs, info;

    tocs:= [ AtlasTableOfContents( "remote" ).TableOfContents ];

    # Construct the information about representations for the group `name'.
    info:= AtlasOfGroupRepresentationsInfoGroup( name[1], tocs, name[2],
               true, [] );

    # Construct the browse table.
    return rec(
      work:= rec(
        align:= "tl",
        header:= [ "", [ NCurses.attrs.UNDERLINE, true,
                   Concatenation( "AtlasRep info for ", name[1] ) ], "" ],
        main:= List( info.list,
                     x -> [ rec( rows:= [ x[2] ], align:= "l" ) ] ),
        labelsRow:= List( info.list, x -> [ x[1] ] ),
        sepLabelsRow:= [ " ", ":" ],
        Click:= rec(
          select_entry:= rec(
            helplines:= [ "add the selected representation to the result" ],
            action:= function( t )
              if t.dynamic.selectedEntry <> [ 0, 0 ] then
                # Set the return value and close the window.
                t.dynamic.Return:= t.dynamic.selectedEntry;
                BrowseData.actions.QuitTable.action( t );
              fi;
            end ),
          select_row:= ~.work.Click.select_entry,
        ),
      ),
      dynamic:= rec(
        initialSteps:= [ 115, 114 ],
        log:= log,
        replay:= replay,
        Return:= fail,
      ),
    );
end;


#############################################################################
##
#F  BrowseAtlasInfo()
##
##  <#GAPDoc Label="AtlasRep_section">
##  <Section Label="sec:atlasdisp">
##  <Heading>Table of Contents of <Package>AtlasRep</Package></Heading>
##
##  The &GAP; package <Package>AtlasRep</Package>
##  (see&nbsp;<Cite Key="AtlasRep"/>) is an interface to a database
##  of representations and related data.
##  The table of contents of this database can be displayed via the function
##  <Ref Func="DisplayAtlasInfo" BookName="atlasrep"/> of this package.
##  The &Browse; package provides an alternative based on the function
##  <Ref Func="NCurses.BrowseGeneric"/>;
##  one can scroll, search, and fetch representations for later use.
##
##  <ManSection>
##  <Func Name="BrowseAtlasInfo" Arg=""/>
##
##  <Returns>
##  the list of <Q>clicked</Q> representations.
##  </Returns>
##
##  <Description>
##  This function shows the table of contents of the &GAP; package
##  <Package>AtlasRep</Package> in a browse table,
##  cf. Section&nbsp;<Ref Sect="Accessing Data of the AtlasRep Package"
##  BookName="atlasrep"/> in the package manual.
##  When one <Q>clicks</Q> on one of the table rows or entries then a
##  browse table with an overview of the available representations for this
##  group is shown, and <Q>clicking</Q> on one of its rows adds this
##  representation to the list of return values of <C>BrowseAtlasInfo</C>.
##  <P/>
##  The full functionality of the function
##  <Ref Func="NCurses.BrowseGeneric"/> is available.
##  <P/>
##  The following example shows how <C>BrowseAtlasInfo</C> can be
##  used to fetch permutation representations of the alternating groups
##  <M>A_5</M> and <M>A_6</M>:
##  We search for the group name <C>"A5"</C> in the overview table, and the
##  first cell in the table row for <M>A_5</M> becomes selected;
##  hitting the <B>Enter</B> key causes a new window to be opened, with an
##  overview of the available representations for <M>A_5</M>;
##  moving down by one row and hitting the <B>Enter</B> key again causes the
##  second representation to be added to the result list,
##  the second window is closed, and we are back in the overview table;
##  we move the selection down twice (to the row for the group <M>A_6</M>),
##  and choose the first representation for this group;
##  finally we leave the table, and the return value is the list with the
##  data for the two representations.
##  <P/>
##  <Example><![CDATA[
##  gap> d:= [ NCurses.keys.DOWN ];;  r:= [ NCurses.keys.RIGHT ];;
##  gap> c:= [ NCurses.keys.ENTER ];;
##  gap> BrowseData.SetReplay( Concatenation(
##  >        "/A5",         # Find the string A5 ...
##  >        d, d, r,       # ... such that just the word matches,
##  >        c,             # start the search,
##  >        c,             # click the table entry A5,
##  >        d,             # move down by one row,
##  >        c,             # click the row for this representation,
##  >        d, d,          # move down by two rows,
##  >        c,             # click the table entry A6,
##  >        c,             # click the first row,
##  >        "Q" ) );       # and quit the application.
##  gap> tworeps:= BrowseAtlasInfo();;
##  gap> BrowseData.SetReplay( false );
##  gap> if fail in tworeps then
##  >      Print( "no access to the Web ATLAS\n" );
##  >    else
##  >      Print( List( tworeps, x -> x.identifier[1] ), "\n" );
##  >    fi;
##  [ "A5", "A6" ]
##  ]]></Example>
##  <P/>
##  <E>Implementation remarks</E>:
##  The first browse table shown has a static header,
##  no footer and row labels,
##  one row of column labels describing the type of data summarized in the
##  columns.
##  When a row is selected, the <Q>click</Q> functionality opens a new window
##  (via a second level call to <Ref Func="NCurses.BrowseGeneric"/>),
##  in which a browse table with the list of available representations for
##  the given group is shown;
##  in this table, <Q>click</Q> results in adding the selected representation
##  to the result list, and leaving the second level table,
##  So one returns to the first browse table and can choose further
##  representations, perhaps of other groups.
##  When the first level table is left, the list of chosen representations
##  is returned.
##  <P/>
##  This function is available only if the &GAP; package
##  <Package>AtlasRep</Package> is available.
##  <P/>
##  The code can be found in the file <F>lib/atlasbrowse.g</F> of the package.
##  </Description>
##  </ManSection>
##  </Section>
##  <#/GAPDoc>
##
BindGlobal( "BrowseAtlasInfo", function()
    local tocs, gapnames, columns, active, str, matrix, alignments, row,
          i, j, info, ls, dir, out, name, table;

    tocs:= [ AtlasTableOfContents( "remote" ).TableOfContents ];

    # Consider only those names for which actually information is available.
    gapnames:= Filtered( AtlasOfGroupRepresentationsInfo.GAPnames,
                   x -> ForAny( tocs, toc -> IsBound( toc.( x[2] ) ) ) );

    # Compute the data of the columns.
    columns:= [ [ "G", "r", List( gapnames, x -> x[1] ) ] ];
    Append( columns, List( AGRDataTypes( "rep", "slp" ), type ->
           [ type[2].DisplayOverviewInfo[1],
             type[2].DisplayOverviewInfo[2],
             List( gapnames,
               n -> type[2].DisplayOverviewInfo[3]( tocs, n[2] )[1] ) ] ) );
    active:= Filtered( [ 1 .. Length( columns ) ],
                 i -> not IsEmpty( columns[i][1] ) );

    # Construct and show the browse table.
    return NCurses.BrowseGeneric( rec(
      work:= rec(
        align:= "tl",
        header:= [ "", [ NCurses.attrs.UNDERLINE, true,
                         "Atlas Of Group Representations: Overview" ], "" ],
        main:= List( [ 1 .. Length( gapnames ) ], i ->  List( active,
                    j -> rec( rows:= [ columns[j][3][i] ], align:= "r" ) ) ),
        labelsCol:= [ List( columns{ active }, x -> [ x[1] ] ) ],
        sepLabelsCol:= [ "-" ],
        sepCol:= [ "| ", " | ", " | ", " | ", " | ", " | ", " |" ],
        Click:= rec(
          select_entry:= rec(
            helplines:= [ "open an overview for the selected entry" ],
            action:= function( t )
              local tt, result;

              if t.dynamic.selectedEntry <> [ 0, 0 ] then
                tt:= BrowseData.AtlasRepGroupInfoRepsTable(
                       gapnames[ t.dynamic.selectedEntry[1] / 2 ],
                       t.dynamic.log,
                       t.dynamic.replay );
                result:= NCurses.BrowseGeneric( tt );
                if tt.dynamic.interrupt then
                  BrowseData.actions.QuitTable.action( t );
                elif result <> fail then
                  Add( t.dynamic.Return, AtlasGenerators(
                         gapnames[ t.dynamic.selectedEntry[1] / 2 ][1],
                         result[1] ) );
                fi;
              fi;
            end ),
          select_row:= rec(
            helplines:= [ "open an overview for the selected row" ],
            action:= ~.work.Click.select_entry.action ),
        ),
      ),
      dynamic:= rec(
        Return:= [],
      )
    ) );
end );


#############################################################################
##
#E

