module CSwingEnum
    use OperationScenariosModule
    implicit none
    contains
    
    subroutine Evaluate_Swing()
        implicit none
        
!        if (DriveType == TopDrive_DriveType) then
!#ifdef OST
!            print*, 'Evaluate_Swing=TopDrive'
!#endif
!        endif
!        
!        
!        
!        
!        
!        
!        
!        
!        
!        if (DriveType == Kelly_DriveType) then
!#ifdef OST
!            print*, 'Evaluate_Swing=Kelly'
!#endif
!        endif
        
    end subroutine
    
    ! subroutine Subscribe_Swing()
    !     use CDrillingConsoleVariables
    !     use ConfigurationVariables
    !     use ConfigurationVariables
    !     implicit none
    !     call OnSwingPress%Add(ButtonPress_Swing)
    ! end subroutine
    
    subroutine ButtonPress_Swing()
        implicit none
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Swing=TopDrive'
#endif
        endif
        
        
        
        
        
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'ButtonPress_Swing=Kelly'
#endif
        

            !!OPERATION-CODE=84
            !if (Get_OperationCondition() == OPERATION_DRILL .and.&
            !    Get_HookHeight() >= (HKL + Get_NearFloorConnection() + RE) .and.&
            !    Get_Swing() == SWING_RAT_HOLE_END) then
            !    call Set_Swing(SWING_WELL_BEGIN)
            !    return
            !endif
        
            !OPERATION-CODE=26
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection() + data%State%OperationScenario%PL) .and.&
                Get_KellyConnection() == KELLY_CONNECTION_SINGLE .and.&
                Get_Swing() == SWING_WELL_END .and.&
                Get_SwingLed() .and.&
                Get_FillMouseHoleLed() == .false.) then
            
                call Set_Swing(SWING_MOUSE_HOLE_BEGIN)
                return
            endif
        
        
            
            
            !OPERATION-CODE=27
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection() + data%State%OperationScenario%PL) .and.&
                Get_KellyConnection() == KELLY_CONNECTION_SINGLE .and.&
                Get_Swing() == SWING_MOUSE_HOLE_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_WELL_BEGIN)
                return
            endif
            
            
            
            
            !OPERATION-CODE=28
             if (Get_OperationCondition() == OPERATION_DRILL .and.&
                 Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection()) .and.&
                 Get_KellyConnection() == KELLY_CONNECTION_NOTHING .and.&
                 Get_Swing() == SWING_WELL_END .and.&
                 Get_SwingLed()) then
            
                call Set_Swing(SWING_MOUSE_HOLE_BEGIN)
                return
            endif
             
             
             
            !OPERATION-CODE=29 
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                 Get_HookHeight() >= (data%State%OperationScenario%HKL + data%State%OperationScenario%SG) .and.&
                 Get_KellyConnection() == KELLY_CONNECTION_NOTHING .and.&
                 Get_Swing() == SWING_MOUSE_HOLE_END .and.&
                 Get_SwingLed()) then
            
                call Set_Swing(SWING_RAT_HOLE_BEGIN)
                return
            endif
             
             
            !OPERATION-CODE=30 
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                 Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection()) .and.&
                 Get_Swing() == SWING_RAT_HOLE_END .and.&
                 Get_SwingLed()) then
            
                call Set_Swing(SWING_WELL_BEGIN)
                return
                 endif
             
        
             
            !OPERATION-CODE=31
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection()) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_Swing() == SWING_WELL_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_MOUSE_HOLE_BEGIN)
                return
            endif
             
        
            
            !OPERATION-CODE=32
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= 27.41 .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                !Get_Swing() == SWING_WELL_END .and.&
                Get_Swing() == SWING_MOUSE_HOLE_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_RAT_HOLE_BEGIN)
                return
            endif
            
            
            
            !OPERATION-CODE=33
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                (Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection()) .and. Get_HookHeight() <= 27.41)  .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_Swing() == SWING_MOUSE_HOLE_END .and.&!Get_Swing() == SWING_MOUSE_HOLE_END .and.&!Get_Swing() /= SWING_WELL_END
                Get_SwingLed()) then
            
                call Set_Swing(SWING_WELL_BEGIN)
                return
            endif
            
            
            
            !OPERATION-CODE=34
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection() + data%State%OperationScenario%PL - data%State%OperationScenario%ECG) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_SINGLE .and.&
                Get_Swing() == SWING_WELL_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_MOUSE_HOLE_BEGIN)
                return
            endif
            
            
            
            
            
            !OPERATION-CODE=35
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection() + data%State%OperationScenario%PL - data%State%OperationScenario%ECG + data%State%OperationScenario%RE) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_SINGLE .and.&
                Get_Swing() == SWING_MOUSE_HOLE_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_WELL_BEGIN)
                return
            endif
            
            !OPERATION-CODE=80
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= 27.41 .and. Get_HookHeight() <= (27.41 + 10) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_Swing() == SWING_RAT_HOLE_END .and.&
                Get_SwingLed()) then
            
                call Set_Swing(SWING_WELL_BEGIN)
                return
            end if   




        endif
        
        
        
        
        
        
        
        
        
        
        
            
            
    end subroutine
    
end module CSwingEnum