module CTdsElevatorModesEnum
    use OperationScenariosModule
    implicit none
    contains
    
    subroutine Evaluate_TdsElevatorModes()
        use CCommon, only: SetStandRack
        implicit none
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'Evaluate_TdsElevatorModes=TopDrive'
#endif


            !TOPDRIVE-CODE=7
            if (Get_Elevator() == ELEVATOR_LATCH_STRING_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_NOTHING .and.&
                Get_TdsSwing() == TDS_SWING_OFF_END ) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STRING)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                return
            end if
                
                
                
            !TOPDRIVE-CODE=8
            if (Get_HookHeight() <= (TL() + TJH() - data%State%OperationScenario%ECG) .and.&
                Get_ElevatorPickup() == .false. .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STRING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STRING)
                return
            end if


                
            !TOPDRIVE-CODE=9
            if (Get_ElevatorPickup() .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STRING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STRING)
                return
            end if
                
                
            !TOPDRIVE-CODE=10
            if (Get_Elevator() == ELEVATOR_UNLATCH_STRING_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STRING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_NOTHING)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                return
            end if
                
                
                
                
            !TOPDRIVE-CODE=11
            if (Get_Elevator() == ELEVATOR_UNLATCH_STRING_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STRING .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_STRING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_NOTHING)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                return
            end if
                
                
                
                
                
            !TOPDRIVE-CODE=12
            if (Get_Elevator() == ELEVATOR_LATCH_STAND_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_NOTHING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STAND)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                call SetStandRack(Get_StandRack() - 1)
                return
            end if
                
                
                
                
            !TOPDRIVE-CODE=13
            if (Get_Elevator() == ELEVATOR_UNLATCH_STAND_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STAND) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_NOTHING)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                call SetStandRack(Get_StandRack() + 1)
                return
            end if
                
                
                
                
            !TOPDRIVE-CODE=14
            if (Get_Elevator() == ELEVATOR_LATCH_SINGLE_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_NOTHING .and.&
                Get_TdsSwing() == TDS_SWING_TILT_END .and.&
                Get_FillMouseHoleLed()) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_SINGLE)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                call Set_UnlatchLed(.true.)
                call Set_FillMouseHoleLed(.false.)
                call Set_MouseHole(MOUSE_HOLE_NEUTRAL)
                return
            end if
                
                
                
                
                
            !TOPDRIVE-CODE=15
            if (Get_Elevator() == ELEVATOR_UNLATCH_SINGLE_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_SINGLE .and.&
                Get_TdsSwing() == TDS_SWING_TILT_END .and.&
                Get_FillMouseHoleLed() == .false.) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_NOTHING)
                call Set_Elevator(ELEVATOR_NEUTRAL)
                call Set_UnlatchLed(.false.)
                call Set_FillMouseHoleLed(.true.)
                call Set_MouseHole(MOUSE_HOLE_NEUTRAL)
                return
            end if
                
                
                
            !TOPDRIVE-CODE=16
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%PL - data%State%OperationScenario%ECG) .and.&
                Get_Tong() == TONG_BREAKOUT_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STRING .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_SINGLE)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_REMOVE_SINGLE)
                return
            end if  
                
                
                
              
            !TOPDRIVE-CODE=17
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%PL - data%State%OperationScenario%ECG) .and.&
                Get_Tong() == TONG_BREAKOUT_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STRING .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_SINGLE)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_REMOVE_SINGLE)
                return
            end if  
                
                
                
            !TOPDRIVE-CODE=18
            if (Get_ElevatorPickup() .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_SINGLE) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_SINGLE)
                return
            end if   
                
                
                
                
            !TOPDRIVE-CODE=19
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%PL - data%State%OperationScenario%ECG) .and.&
                Get_ElevatorPickup() == .false. .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_SINGLE) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_SINGLE)
                
                return
            end if 
                
                
                
                
                
                
            !TOPDRIVE-CODE=20
            if (Get_Tong() == TONG_MAKEUP_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_SINGLE) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STRING)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_ADD_SINGLE)
                return
            end if   
                
                
                
                
            !TOPDRIVE-CODE=21
            if (Get_Tong() == TONG_MAKEUP_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_SINGLE) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STRING)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_ADD_SINGLE)
                return
            end if    
                
                
                
            !TOPDRIVE-CODE=22
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%SL - data%State%OperationScenario%ECG) .and.&
                Get_Tong() == TONG_BREAKOUT_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STRING .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STAND)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_REMOVE_STAND)
                return
            end if   
                
                
                
            !TOPDRIVE-CODE=23
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%SL - data%State%OperationScenario%ECG) .and.&
                Get_Tong() == TONG_BREAKOUT_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STRING .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STAND)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_REMOVE_STAND)
                return
            end if     
                
                
                
            !TOPDRIVE-CODE=24
            if (Get_ElevatorPickup() .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STAND) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STAND)
                return
            end if  
                
                
                
                
                
            !TOPDRIVE-CODE=25
            if (Get_HookHeight() <= (TL() + NFC() + data%State%OperationScenario%SL - data%State%OperationScenario%ECG) .and.&
                Get_ElevatorPickup() == .false. .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STAND) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STAND)
                return
            end if 
                
                
                
                
            !TOPDRIVE-CODE=26
            if (Get_Tong() == TONG_MAKEUP_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STAND) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_CONNECTION_STRING)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_ADD_STAND)
                return
            end if 
                
                
                
                
                
                
                
                
                
            !TOPDRIVE-CODE=27
            if (Get_Tong() == TONG_MAKEUP_END .and.&
                Get_TdsElevatorModes() == TDS_ELEVATOR_LATCH_STAND) then
            
                call Set_TdsElevatorModes(TDS_ELEVATOR_LATCH_STRING)
                call Set_Tong(TONG_NEUTRAL)
                call Set_StringUpdate(STRING_UPDATE_ADD_STAND)
                return
            end if 
                
                
                
                
                
                
                
                
                
                

        endif
        
        
        
        
        
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'Evaluate_TdsElevatorModes=Kelly'
#endif
        endif
        
        
        
        
    end subroutine
    
    subroutine Subscribe_TdsElevatorModes()
        use CDrillingConsoleVariables
    use SimulationVariables
    use SimulationVariables
        implicit none
    end subroutine
    
end module CTdsElevatorModesEnum