Compile function SRP_GIT_UTILITY_EVENTS(Event, CtrlEntId, @PARAMS)
/************************************************************************************************
*
* This program is proprietary and is not to be used by or disclosed to others, nor is it to
* be copied without written permission from SRP Computer Solutions, Inc.
*
* Name       :  SRP_GIT_UTILITY_Events
*
* Description:  Event handling commuter module for the SRP_GIT_UTILITY form.
*
* User Defined Properties:
*
* Parameters:
*	EventID		[in] -- Event ID
*	CtrlEntId	[in] -- Control whose event is being handled
*	Param1-13	[in] -- Event Parameters
*
* Returns:
*	Event handling result, usually 1
*
* History (Date, Initials, Notes)
*	04/05/19    DJS    Original programmer
*	07/29/19    KRF    Updated with different controls and cleaned up the code
*
************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window SRP_GIT_UTILITY

$Insert LOGICAL

Equ IMAGE_LOC$ to 'BMPS\SRPGit.zip#'

Declare function RGB, SRP_Git_Services, Git_Services, SRP_Array, Set_Property, Get_Property
Declare subroutine SRP_Git_Services, SRP_Set_Minmaxinfo, Set_Property, Send_Event, Post_Event, Send_Message

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------


Event WINDOW.CREATE(CreateParam)
    
    If SRP_Git_Services("HasSettings") then
           
        // Setup combo boxes
        Set_Property(@Window:'.CMB_FILTER_APP', 'LIST', @FM:@APPID)
        Set_Property(@Window:'.CMB_FILTER_WORK', 'LIST', @FM:SRP_Git_Services("GetSupportedEntityTypes"))
        
        GoSub Setup_OLE_Controls
        GoSub Load_Auto
        
        // Default to pull
        Send_Event(@Window:".PUB_PULL", "OLE", "OnClick", "0,0", "Left", 0, 0)
        
    end else
       
       Call Start_Window("SRP_GIT_SETTINGS")
       Call Post_Event(@Window, "CLOSE")
       
    end
    
End Event


Event PUB_PULL.OnClick(Point, Button, Shift, Ctrl)
    
    Set_Property(@Window:".PUB_PULL", "OLE.State", 1)
    Set_Property(@Window:".PUB_PULL", "OLE.FontBold", 1)
    Set_Property(@Window:".PUB_PUSH", "OLE.State", 0)
    Set_Property(@Window:".PUB_PUSH", "OLE.FontBold", 0)
    Set_Property(@Window, "@OPERATION", "PULL")
    
    Set_Property(@Window:".STA_WORKING", "TEXT", "Git Repo Entities")
    Set_Property(@Window:".STA_STAGE", "TEXT", "Entities to Pull into OpenInsight")
    Set_Property(@Window:".EDL_FROM_COMMIT", "VISIBLE", Yes$)
    Set_Property(@Window:".STA_FROM_COMMIT", "VISIBLE", Yes$)
    Set_Property(@Window:".EDL_TO_COMMIT", "VISIBLE", Yes$)
    Set_Property(@Window:".STA_TO_COMMIT", "VISIBLE", Yes$)
    Set_Property(@Window:".OLE_REFRESH", "VISIBLE", Get_Property(@Window:".OLE_MANUAL", "OLE.Caption") _EQC "Auto")
    Set_Property(@Window:".OLE_MANUAL", "VISIBLE", Yes$)
    Set_Property(@Window:".OLE_HEADS", "VISIBLE", Get_Property(@Window:".OLE_MANUAL", "OLE.Caption") _EQC "Auto")
    Set_Property(@Window:".PUB_APPLY", "TEXT", "Pull")
    Set_Property(@Window:".OLE_RPT_STAGE", "OLE.EmptyTablePrompt", "Move items here you want to pull from Git into OpenInsight.")
    
    GoSub Populate
    
end event


Event PUB_PUSH.OnClick(Point, Button, Shift, Ctrl)
    
    Set_Property(@Window:".PUB_PULL", "OLE.State", 0)
    Set_Property(@Window:".PUB_PULL", "OLE.FontBold", 0)
    Set_Property(@Window:".PUB_PUSH", "OLE.State", 1)
    Set_Property(@Window:".PUB_PUSH", "OLE.FontBold", 1)
    Set_Property(@Window, "@OPERATION", "PUSH")
    
    Set_Property(@Window:".STA_WORKING", "TEXT", "OpenInsight Entities")
    Set_Property(@Window:".STA_STAGE", "TEXT", "Entities to Push into Git")
    Set_Property(@Window:".EDL_FROM_COMMIT", "VISIBLE", No$)
    Set_Property(@Window:".STA_FROM_COMMIT", "VISIBLE", No$)
    Set_Property(@Window:".EDL_TO_COMMIT", "VISIBLE", No$)
    Set_Property(@Window:".STA_TO_COMMIT", "VISIBLE", No$)
    Set_Property(@Window:".OLE_REFRESH", "VISIBLE", No$)
    Set_Property(@Window:".OLE_MANUAL", "VISIBLE", No$)
    Set_Property(@Window:".OLE_HEADS", "VISIBLE", No$)
    Set_Property(@Window:".EDL_SEARCH", "FOCUS", Yes$)
    Set_Property(@Window:".PUB_APPLY", "TEXT", "Push")
    Set_Property(@Window:".OLE_RPT_STAGE", "OLE.EmptyTablePrompt", "Move items here you want to push from OpenInsight into Git.")
    
    GoSub Populate
    
end event


Event CMB_FILTER_APP.CHANGED(NewData)
    GoSub Populate
end event


Event CMB_FILTER_WORK.CHANGED(NewData)
    GoSub Populate
end event


* Event CMB_FROM_COMMIT.CHANGED(NewData)    
*    GoSub Populate
* end event
* 
* 
* Event CMB_TO_COMMIT.CHANGED(NewData)    
*    GoSub Populate
* end event


Event OLE_REFRESH.OnClick(Point, Button, Shift, Ctrl)
    
    Text = Get_Property(@Window:".OLE_MANUAL", "OLE.Caption")
    If Text _EQC "Manual" then
        GoSub Populate
    end else
        Set_Property(@Window, "REDRAW", No$)
        Set_Property("SYSTEM", "CURSOR", "H")
        GoSub Load_Commits
        Set_Property("SYSTEM", "CURSOR", "A")
        Set_Property(@Window, "REDRAW", Yes$)
    end
    
end event


Event OLE_MANUAL.OnClick(Point, Button, Shift, Ctrl)
    
    Set_Property(@Window, "REDRAW", No$)
    Text = Get_Property(@Window:".OLE_MANUAL", "OLE.Caption")
    If Text _EQC "Manual" then
        Set_Property(@Window:".STA_FROM_COMMIT", "TEXT", "Start:")
        Set_Property(@Window:".STA_TO_COMMIT", "TEXT", "End:")
        Set_Property(@Window:".OLE_MANUAL", "OLE.Caption", "Auto")
        Set_Property(@Window:".OLE_HEADS", "VISIBLE", Yes$)
        Set_Property(@Window:".OLE_REFRESH", "VISIBLE", Yes$)
        GoSub Enable_Combos
        GoSub Load_Commits
    end else
        Set_Property(@Window:".OLE_SUBCLASS", "OLE.Combo[EDL_FROM_COMMIT]", 0)
        Set_Property(@Window:".OLE_SUBCLASS", "OLE.Combo[EDL_TO_COMMIT]", 0)
        Set_Property(@Window:".STA_FROM_COMMIT", "TEXT", "Last Pull:")
        Set_Property(@Window:".STA_TO_COMMIT", "TEXT", "Current:")
        Set_Property(@Window:".OLE_MANUAL", "OLE.Caption", "Manual")
        Set_Property(@Window:".OLE_HEADS", "VISIBLE", No$)
        Set_Property(@Window:".OLE_REFRESH", "VISIBLE", No$)
        GoSub Load_Auto
        GoSub Populate
    end
    Set_Property(@Window, "REDRAW", Yes$)
    
end event

Event OLE_HEADS.OnClick(Point, Button, Shift, Ctrl)
    Text = Get_Property(@Window:".OLE_HEADS", "OLE.Caption")
    If Text _EQC "All Refs" then
        Set_Property(@Window:".OLE_HEADS", "OLE.Caption", "Heads")
    end else
        Set_Property(@Window:".OLE_HEADS", "OLE.Caption", "All Refs")
    end
    GoSub Load_Commits
end event


Event EDL_SEARCH.CHANGED(NewData)
    Set_Property(@Window, "TIMER", 0:@FM:500) 	
end event


Event EDL_CONFIRM.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    If Get_Property(CtrlEntId, "TEXT") EQ "PULL" then
        GoSub Execute
    end
    
end event


Event WINDOW.TIMER()
    
    Text = Get_Property(@Window:".EDL_SEARCH", "TEXT")
    List = Get_Property(@Window:".OLE_RPT_WORK", "@ORIGLIST")
    NewList = ""
    If Len(Text) then
        For Each Row in List
            If IndexC(Row, Text, 1) GT 0 then
                NewList := Row:@FM
            end
        Next Row
        NewList[-1, 1] = ""
    end else
        Transfer List to NewList
    end
    Set_Property(@Window:".OLE_RPT_WORK", "OLE.List", NewList)
    
end event


Event PUB_APPLY.CLICK()
    
    GitWarning = SRP_Git_Services("GetWarning")
    If Len(GitWarning) AND Get_Property(@Window, '@OPERATION') EQ "PULL" then
        Set_Property(@Window:".EDL_CONFIRM", "TEXT", "")
        Set_Property(@Window, "VPOSITION", 3)
        Set_Property(@Window:".EDL_CONFIRM", "FOCUS", Yes$)
    end else
        GoSub Execute
    end

end event


Event PUB_CLOSE.CLICK()
    Post_Event(@Window, 'CLOSE')
end event


Event PUB_BACK.CLICK()
    Set_Property(@Window, "VPOSITION", 1)
end event


Event OLE_ADD_ALL.OnClick(Point, Button, Shift, Ctrl)
    
    Send_Message(@Window:'.OLE_RPT_WORK', 'OLE.ExpandAll')
    EntityList = Get_Property(@Window:'.OLE_RPT_WORK', 'OLE.List')
    Set_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List', EntityList)
    GoSub EnableArrowButtons
    GoSub EnableStagingCtrls
    
end event


Event OLE_ADD_SELECTED.OnClick(Point, Button, Shift, Ctrl)
    
    EntitiesToAdd = Get_Property(@Window:'.OLE_RPT_WORK', 'OLE.SelList')
    StageList = Get_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List')
    ResultList = SRP_Array('Join', StageList, EntitiesToAdd, 'OR', @FM)
    Set_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List', ResultList)
    GoSub EnableArrowButtons
    GoSub EnableStagingCtrls
    
end event


Event OLE_REMOVE_ALL.OnClick(Point, Button, Shift, Ctrl)
    
    Send_Message(@Window:'.OLE_RPT_STAGE', 'OLE.ExpandAll')
    Set_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List', '')
    GoSub EnableArrowButtons
    GoSub EnableStagingCtrls
    
end event


Event OLE_REMOVE_SELECTED.OnClick(Point, Button, Shift, Ctrl)
   
    StageRptCtrl = @Window : '.OLE_RPT_STAGE'
    SelList = Get_Property(StageRptCtrl, 'OLE.SelList')
    EntityList = Get_Property(StageRptCtrl, 'OLE.List')
    For each Row in SelList using @FM setting sPos
        Locate Row in EntityList using @FM setting ePos then EntityList = Delete(EntityList, ePos, 0, 0)
    Next Row
    Set_Property(StageRptCtrl, 'OLE.List', EntityList)
    GoSub EnableArrowButtons
    GoSub EnableStagingCtrls
    
end event


Event OLE_RPT_WORK.OnSelChange()
    GoSub EnableArrowButtons
end event


Event OLE_RPT_WORK.OnItemDblClick(Row, Button, Col, Point)
    Send_Event(@Window:".OLE_ADD_SELECTED", "OLE", "OnClick", "", "", "", "")
end event


Event OLE_RPT_STAGE.OnSelChange()
    GoSub EnableArrowButtons
end event


Event OLE_RPT_STAGE.OnItemDblClick(Row, Button, Col, Point)
    Send_Event(@Window:".OLE_REMOVE_SELECTED", "OLE", "OnClick", "", "", "", "")
end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)
    Set_Property(@Window:".EDL_FROM_COMMIT", "@HASH", Get_Property(@Window:".OLE_SUBCLASS", "OLE.ComboRowData[EDL_FROM_COMMIT]")<2>)
    Set_Property(@Window:".EDL_TO_COMMIT", "@HASH", Get_Property(@Window:".OLE_SUBCLASS", "OLE.ComboRowData[EDL_TO_COMMIT]")<2>)
    GoSub Populate
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Setup_OLE_Controls:
       
    // Subclassing
    Ctrl = @Window:".OLE_SUBCLASS"
    Send_Message(Ctrl, "OLE.Subclass", Get_Property(@Window:".EDL_FROM_COMMIT", "HANDLE"), @Window:".EDL_FROM_COMMIT")
    Send_Message(Ctrl, "OLE.Subclass", Get_Property(@Window:".EDL_TO_COMMIT", "HANDLE"), @Window:".EDL_TO_COMMIT")
    Set_Property(Ctrl, "OLE.CurrentWindow", @Window)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnComboClick', Yes$)
    
    // Setup buttons
    Background = ""
    Background<1> = "Vertical(Gradient(White, White), Border(White))"
    Background<2> = "Vertical(Gradient(White, White), Gradient(Blue L=50, Blue L=50, 4), Border(White))"
    Background<3> = "Vertical(Gradient(White, White), Gradient(Blue L=50, Blue L=50, 4), Border(White))"
    Foreground = ""
    Foreground<1> = "Blue L=50"
    Foreground<2> = "Blue L=50"
    Foreground<3> = "Blue L=50"
*    CtrlIds = "PUB_PULL,PUB_PUSH"
    Set_Property(@Window:".PUB_PULL", "OLE.Background", Background)
    Set_Property(@Window:".PUB_PULL", "OLE.Forecolor", Foreground)
    Send_Message(@Window:".PUB_PULL", 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Swap "Blue" with "Red" in Background
    Swap "Blue" with "Red" in Foreground
    Set_Property(@Window:".PUB_PUSH", "OLE.Background", Background)
    Set_Property(@Window:".PUB_PUSH", "OLE.Forecolor", Foreground)
    Send_Message(@Window:".PUB_PUSH", 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
*    For i = 1 to 2
*        Ctrl = @Window:".":Field(CtrlIds, ",", i, 1)
*        Set_Property(Ctrl, "OLE.Background", Background)
*        Set_Property(Ctrl, "OLE.Forecolor", Foreground)
*        Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
*    Next i
    
    // Qualify OLE events that we want to intercept
    Send_Message(@Window:'.OLE_ADD_ALL', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_ADD_SELECTED', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_REMOVE_ALL', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_REMOVE_SELECTED', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_REFRESH', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_MANUAL', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    Send_Message(@Window:'.OLE_HEADS', 'QUALIFY_EVENT', 'OLE.OnClick', Yes$)
    
    // Setup selection buttons
    Style = 'TBNG'
	Background = ''
    Background<1> = 'None'
    Background<2> = 'Vertical(Gradient(S L=90, S L=90), Border(S L=70))'
    Background<3> = 'Vertical(Gradient(S L=80, S L=80), Border(S L=60))'
    Background<4> = 'None'
    Background<5> = 'Vertical(Gradient(S L=80, S L=80), Border(S L=60))'
	Set_Property(@Window:'.OLE_ADD_ALL', 'OLE.Style', Style)
	Set_Property(@Window:'.OLE_ADD_SELECTED', 'OLE.Style', Style)
	Set_Property(@Window:'.OLE_REMOVE_SELECTED', 'OLE.Style', Style)
	Set_Property(@Window:'.OLE_REMOVE_ALL', 'OLE.Style', Style)
	Set_Property(@Window:'.OLE_ADD_ALL', 'OLE.Background', Background)
	Set_Property(@Window:'.OLE_ADD_SELECTED', 'OLE.Background', Background)
	Set_Property(@Window:'.OLE_REMOVE_SELECTED', 'OLE.Background', Background)
	Set_Property(@Window:'.OLE_REMOVE_ALL', 'OLE.Background', Background)
	Set_Property(@Window:'.OLE_ADD_ALL', 'OLE.Icon', IMAGE_LOC$:'ArrowDblRight.png')
	Set_Property(@Window:'.OLE_ADD_SELECTED', 'OLE.Icon', IMAGE_LOC$:'ArrowRight.png')
	Set_Property(@Window:'.OLE_REMOVE_SELECTED', 'OLE.Icon', IMAGE_LOC$:'ArrowLeft.png')
	Set_Property(@Window:'.OLE_REMOVE_ALL', 'OLE.Icon', IMAGE_LOC$:'ArrowDblLeft.png')
	
	// Setup Working OLE Report Table
	Ctrl = @Window:'.OLE_RPT_WORK' ; GoSub Setup_ReportTable
	Ctrl = @Window:'.OLE_RPT_STAGE' ; GoSub Setup_ReportTable
    
return

Setup_ReportTable:

    ColumnList     = ''       
    ColumnList<-1> = 'Id'     :@VM:'Text':@VM:100:@VM:0:@VM:@VM:@VM:1:@VM:0:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segeo UI Semilight':@SVM:'10'
    ColumnList<-1> = 'App'    :@VM:'Text':@VM:120:@VM:0:@VM:@VM:@VM:1:@VM:1:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segoe UI Semilight':@SVM:'10'
    ColumnList<-1> = 'Type'   :@VM:'Text':@VM:100:@VM:0:@VM:@VM:@VM:1:@VM:0:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segoe UI Semilight':@SVM:'10'
    ColumnList<-1> = 'Class'  :@VM:'Text':@VM:100:@VM:0:@VM:@VM:@VM:1:@VM:0:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segoe UI Semilight':@SVM:'10'
    ColumnList<-1> = 'Name'   :@VM:'Text':@VM:300:@VM:1:@VM:@VM:@VM:1:@VM:1:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segoe UI Semilight':@SVM:'10'
    ColumnList<-1> = 'Status' :@VM:'Text':@VM:80 :@VM:0:@VM:@VM:@VM:0:@VM:1:@VM:50:@VM:'Left':@VM:@VM:@VM:@VM:'Segoe UI Semilight':@SVM:'10'
                        
    Set_Property(Ctrl, 'OLE.ColumnList', ColumnList)
    Set_Property(Ctrl, 'OLE.GroupOrder', 3)
    Set_Property(Ctrl, 'OLE.SortOrder', 3:@FM:5)
    Set_Property(Ctrl, 'OLE.MultiSelect', 1)
    Set_Property(Ctrl, 'OLE.HorizontalGridStyle', 'None')
    Set_Property(Ctrl, "OLE.AlwaysShowSelection", Yes$)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', Yes$)
    Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnItemDblClick', Yes$)
    
return

   
EnableArrowButtons:

    Set_Property(@Window:'.OLE_ADD_ALL', 'ENABLED', Get_Property(@Window:'.OLE_RPT_WORK', 'OLE.RowCount') GT 0)
	Set_Property(@Window:'.OLE_ADD_SELECTED', 'ENABLED', Get_Property(@Window:'.OLE_RPT_WORK', 'OLE.SelList') NE "")
	Set_Property(@Window:'.OLE_REMOVE_SELECTED', 'ENABLED', Get_Property(@Window:'.OLE_RPT_STAGE', 'OLE.SelList') NE "")
    Set_Property(@Window:'.OLE_REMOVE_ALL', 'ENABLED', Get_Property(@Window:'.OLE_RPT_STAGE', 'OLE.RowCount') GT 0)
	
return


EnableStagingCtrls:
    
    StagingList = Get_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List')
    If StagingList NE '' then
        Set_Property(@Window:'.PUB_APPLY', 'ENABLED', Yes$)
    end else
        Set_Property(@Window:'.PUB_APPLY', 'ENABLED', No$)
    end
    
return


Populate:

    Set_Property('SYSTEM', 'CURSOR', 'H')
    Set_Property(@Window:".OLE_RPT_WORK", "OLE.EmptyTablePrompt", "Loading...")
    Set_Property(@Window:'.OLE_RPT_WORK', 'OLE.List', "")
    Set_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List', "")
    
    Types = Get_Property(@Window:".CMB_FILTER_WORK", "TEXT")
    Apps = Get_Property(@Window:".CMB_FILTER_APP", "TEXT")
    Operation  = Get_Property(@Window, '@OPERATION')
    
    Begin Case
        
        Case Operation EQ "PULL"
            FromCommit = Get_Property(@Window:".EDL_FROM_COMMIT", "@HASH")
            ToCommit = Get_Property(@Window:".EDL_TO_COMMIT", "@HASH")
            EntityList = SRP_Git_Services("GetRepoEntities", Types, Apps, FromCommit, ToCommit)
            
        Case Operation EQ "PUSH"
            EntityList = SRP_Git_Services("GetOIEntities", Types, Apps)
                 
    End Case
    
    Set_Property(@Window:".EDL_SEARCH", "TEXT", "")
    Set_Property(@Window:'.OLE_RPT_WORK', 'OLE.List', EntityList)
    Set_Property(@Window:'.OLE_RPT_WORK', '@ORIGLIST', EntityList)
    Apps = @APPID
    NumApps = DCount(Apps, @FM)
    If NumApps GE 2 then
        FirstApps = Field(Apps, @FM, 1, NumApps - 1)
        Swap @FM with ", " in FirstApps
        Apps = FirstApps:", or ":Apps<NumApps>
    end
    Set_Property(@Window:".OLE_RPT_WORK", "OLE.EmptyTablePrompt", "No entities found in ":Apps:" matching criteria.")

    GoSub EnableArrowButtons
    
    Set_Property('SYSTEM', 'CURSOR', 'A')
    
return


Load_Commits:
    
    // Clear lists
    Set_Property(@Window:".EDL_FROM_COMMIT", "TEXT", "")
    Set_Property(@Window:".EDL_TO_COMMIT", "TEXT", "")
    
    // Load the commit options
    If Get_Property(@Window:".OLE_HEADS", "OLE.Caption") _EQC "Heads" then
        CommitData = @FM:SRP_Git_Services("GetCommits")
    end else
        CommitData = @FM:SRP_Git_Services("GetBranchHeads")
    end

    // Get the SHA for the last saved commit
    LastCommit = SRP_Git_Services("LoadLastCommit")<1>

    // Find the curr head and the last saved commit
    LastCommitPos = 1
    CurrPos = 1
    For Each Commit in CommitData setting Pos
        If Commit<1, 2> EQ LastCommit then LastCommitPos = Pos
        If Commit<1, 1> EQ "*" then CurrPos = Pos
    Next Commit
    
    // Populate the controls
    Convert @FM:@VM to @TM:@STM in CommitData
    Set_Property(@Window:".OLE_SUBCLASS", "OLE.ComboData[EDL_FROM_COMMIT]", CommitData)
    Set_Property(@Window:".OLE_SUBCLASS", "OLE.ComboData[EDL_TO_COMMIT]", CommitData)
    Set_Property(@Window:".OLE_SUBCLASS", "OLE.ComboSelPos[EDL_FROM_COMMIT]", LastCommitPos)
    Set_Property(@Window:".OLE_SUBCLASS", "OLE.ComboSelPos[EDL_TO_COMMIT]", CurrPos)
    
return


Enable_Combos:
    
    // Subclassing
    Ctrl = @Window:".OLE_SUBCLASS"
    ComboDropDown = ""
    ComboDropDown<1>    = 1                                 ;// by default, we won't show the combo box itself
    ComboDropDown<2, 1> = "*":@TM:"SHA":@TM:"Branch":@TM:"When":@TM:"Author":@TM:"Commit"    :@TM:""
    ComboDropDown<2, 2> = "L":@TM:"L"  :@TM:"L"     :@TM:"L"   :@TM:"L"     :@TM:"L":@STM:300:@TM:"L":@STM:0
    ComboDropDown<2, 4> = 7                                 ;// master column
    ComboDropDown<2, 5> = 0                                 ;// auto fill off
    ComboDropDown<2, 10> = 0                                ;// Only show the drop down when the user types
    ComboDropDown<2, 11> = 1                                ;// Use LIST Format
    ComboDropDown<2, 18> = 1                                ;// Show Popup while in read only mode
    ComboDropDown<2, 22> = 'Segeo UI Semilight':@SVM:'9'    ;// Font
    Set_Property(Ctrl, "OLE.Combo[EDL_FROM_COMMIT]", ComboDropDown)
    Set_Property(Ctrl, "OLE.Combo[EDL_TO_COMMIT]", ComboDropDown)
    
return

Load_Auto:
    
    LastCommit = SRP_Git_Services("LoadLastCommit")
    If Len(LastCommit) then
        Text = '[':LastCommit<3>:'] ':LastCommit<2>:' (':LastCommit<4>:')'
        Set_Property(@Window:".EDL_FROM_COMMIT", "TEXT", Text)
        Set_Property(@Window:".EDL_FROM_COMMIT", "@HASH", LastCommit<1>)
    end else
        Set_Property(@Window:".EDL_FROM_COMMIT", "TEXT", "")
        Set_Property(@Window:".EDL_FROM_COMMIT", "@HASH", "")
    end
    
    CurrCommit = SRP_Git_Services("GetCurrentCommit")
    If Len(CurrCommit) then
        Text = '[':CurrCommit<3>:'] ':CurrCommit<6>:' (':CurrCommit<5>:')'
        Set_Property(@Window:".EDL_TO_COMMIT", "TEXT", Text)
        Set_Property(@Window:".EDL_TO_COMMIT", "@HASH", "*")
    end else
        Set_Property(@Window:".EDL_TO_COMMIT", "TEXT", "")
        Set_Property(@Window:".EDL_TO_COMMIT", "@HASH", "")
    end  
    
return

Execute:
    
    Set_Property('SYSTEM', 'CURSOR', 'H')
    ErrorList   = ''
    Operation   = Get_Property(@Window, '@OPERATION')
    StageList   = Get_Property(@Window:'.OLE_RPT_STAGE', 'OLE.List')
    RotatedList = SRP_Array('Rotate', StageList)
    EntityList  = RotatedList<1>
    StatusList  = RotatedList<6>
    Convert @VM to @FM in EntityList
    Convert @VM to @FM in StatusList
    Set_Property(@Window, "VPOSITION", 2)
    Set_Property(@Window:".PUB_BACK", "ENABLED", No$)
    Set_Property(@Window:".EDB_STATUS", "TEXT", "")
    Begin Case
        Case Operation EQ "PULL"
            ToCommit = Get_Property(@Window:".CMB_TO_COMMIT", "TEXT")
            If Len(ToCommit) then
                CommitLabels = Get_Property(@Window:".CMB_TO_COMMIT", "LIST")
                Locate ToCommit in CommitLabels using @FM setting Pos then
                    ToCommit = Get_Property(@Window:".CMB_TO_COMMIT", "@IDS")<Pos>
                end
            end
            ErrorList = SRP_Git_Services('PullEntities', EntityList, StatusList, @Window:".EDB_STATUS", ToCommit)
        Case Operation EQ "PUSH"                
            ErrorList = SRP_Git_Services('PushEntities', EntityList, @Window:".EDB_STATUS")
    End Case
    Set_Property(@Window:".PUB_BACK", "ENABLED", Yes$)
    CurrCommit = SRP_Git_Services("GetCurrentCommit")
    SRP_Git_Services("SaveLastCommit", CurrCommit<2>, CurrCommit<6>, CurrCommit<3>, CurrCommit<5>)
    Set_Property('SYSTEM', 'CURSOR', 'A')
    
return

