CLASS ycl_abapgit_hotkeys DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    CLASS-METHODS:
      get_default_hotkeys_from_pages
        RETURNING
          VALUE(rt_hotkey_actions) TYPE yif_abapgit_gui_page_hotkey=>tty_hotkey_action
        RAISING
          ycx_abapgit_exception,

      get_relevant_hotkeys_for_page
        IMPORTING
          io_page           TYPE REF TO ycl_abapgit_gui_page
        RETURNING
          VALUE(rt_hotkeys) TYPE yif_abapgit_definitions=>tty_hotkey
        RAISING
          ycx_abapgit_exception.

ENDCLASS.



CLASS ycl_abapgit_hotkeys IMPLEMENTATION.

  METHOD get_default_hotkeys_from_pages.

    DATA: lt_hotkey_actions TYPE yif_abapgit_gui_page_hotkey=>tty_hotkey_action,
          lo_interface      TYPE REF TO cl_oo_interface,
          lt_classes        TYPE seo_relkeys.

    FIELD-SYMBOLS: <ls_class> TYPE seorelkey.

    TRY.
        lo_interface ?= cl_oo_class=>get_instance( |YIF_ABAPGIT_GUI_PAGE_HOTKEY| ).

      CATCH cx_class_not_existent.
        " hotkeys are only available with installed abapGit repository
        RETURN.
    ENDTRY.

    lt_classes = lo_interface->get_implementing_classes( ).

    LOOP AT lt_classes ASSIGNING <ls_class>.

      CALL METHOD (<ls_class>-clsname)=>yif_abapgit_gui_page_hotkey~get_hotkey_actions
        RECEIVING
          rt_hotkey_actions = lt_hotkey_actions.

      INSERT LINES OF lt_hotkey_actions INTO TABLE rt_hotkey_actions.

    ENDLOOP.

    " the global shortcuts are defined in the base class
    lt_hotkey_actions = ycl_abapgit_gui_page=>get_hotkey_actions( ).
    INSERT LINES OF lt_hotkey_actions INTO TABLE rt_hotkey_actions.

    SORT rt_hotkey_actions.
    DELETE ADJACENT DUPLICATES FROM rt_hotkey_actions.

  ENDMETHOD.


  METHOD get_relevant_hotkeys_for_page.

    DATA: lo_settings                    TYPE REF TO ycl_abapgit_settings,
          lv_class_name                  TYPE abap_abstypename,
          lt_hotkey_actions_of_curr_page TYPE yif_abapgit_gui_page_hotkey=>tty_hotkey_action,
          lv_save_tabix                  TYPE syst-tabix,
          lt_hotkey_actions              TYPE yif_abapgit_gui_page_hotkey=>tty_hotkey_action.

    FIELD-SYMBOLS: <ls_hotkey>              TYPE yif_abapgit_definitions=>ty_hotkey.

    lo_settings = ycl_abapgit_persist_settings=>get_instance( )->read( ).

    rt_hotkeys = lo_settings->get_hotkeys( ).

    lv_class_name = cl_abap_classdescr=>get_class_name( io_page ).

    TRY.
        CALL METHOD (lv_class_name)=>yif_abapgit_gui_page_hotkey~get_hotkey_actions
          RECEIVING
            rt_hotkey_actions = lt_hotkey_actions_of_curr_page.

      CATCH cx_root.
        RETURN.
    ENDTRY.

    " these are the global shortcuts
    lt_hotkey_actions = ycl_abapgit_gui_page=>get_hotkey_actions( ).
    INSERT LINES OF lt_hotkey_actions INTO TABLE lt_hotkey_actions_of_curr_page.

    LOOP AT rt_hotkeys ASSIGNING <ls_hotkey>.

      lv_save_tabix = sy-tabix.

      READ TABLE lt_hotkey_actions_of_curr_page TRANSPORTING NO FIELDS
                                                WITH TABLE KEY action
                                                COMPONENTS action = <ls_hotkey>-action.
      IF sy-subrc <> 0.
        " We only offer hotkeys which are supported by the current page or globally
        DELETE rt_hotkeys INDEX lv_save_tabix.
      ENDIF.

    ENDLOOP.

  ENDMETHOD.

ENDCLASS.
