module CSwingLedNotification
    use OperationScenariosModule
    implicit none
    contains
    
    subroutine Evaluate_SwingLed()
        implicit none
        
        
        
        
        
        if (data%State%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'Evaluate_SwingLed=TopDrive'
#endif



        endif
        
        
        
        
        
        
        
        
        
        if (data%State%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'Evaluate_SwingLed=Kelly'
#endif


            !OPERATION-CODE=22
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection()) .and. Get_HookHeight() <= (data%State%OperationScenario%HL + Get_NearFloorConnection() + data%State%OperationScenario%LG) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .and.&
                Get_JointConnectionPossible() == .false. .and.&
                (Get_Swing() /= SWING_WELL_BEGIN .and.& 
                 Get_Swing() /= SWING_MOUSE_HOLE_BEGIN .and.&
                 Get_Swing() /= SWING_RAT_HOLE_BEGIN) .and.&
                Get_Slips() == SLIPS_SET_END) then
            
                call Set_SwingLed(.true.)
                return
            end if
            
            
            !OPERATION-CODE=23
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HL + Get_NearFloorConnection() + data%State%OperationScenario%PL) .and. Get_HookHeight() <= (data%State%OperationScenario%HL + Get_NearFloorConnection() + data%State%OperationScenario%LG + data%State%OperationScenario%PL) .and.&
                Get_ElevatorConnection() == ELEVATOR_CONNECTION_SINGLE .and.&
                Get_JointConnectionPossible() == .false. .and.&
                (Get_Swing() /= SWING_WELL_BEGIN .and.& 
                 Get_Swing() /= SWING_MOUSE_HOLE_BEGIN .and.&
                 Get_Swing() /= SWING_RAT_HOLE_BEGIN) .and.&
                Get_Slips() == SLIPS_SET_END) then
            
                call Set_SwingLed(.true.)
                return
            end if
            
            
            !OPERATION-CODE=24
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection()) .and. Get_HookHeight() <= (data%State%OperationScenario%HKL + Get_NearFloorConnection() + data%State%OperationScenario%LG) .and.&
                Get_JointConnectionPossible() == .false. .and.&
                Get_KellyConnection() == KELLY_CONNECTION_NOTHING .and.&
                (Get_Swing() /= SWING_WELL_BEGIN .and.& 
                 Get_Swing() /= SWING_MOUSE_HOLE_BEGIN .and.&
                 Get_Swing() /= SWING_RAT_HOLE_BEGIN) .and.&
                Get_Slips() == SLIPS_SET_END) then
            
                call Set_SwingLed(.true.)
                return
            end if
            
            !OPERATION-CODE=25
            if (Get_OperationCondition() == OPERATION_DRILL .and.&
                Get_HookHeight() >= (data%State%OperationScenario%HKL + Get_NearFloorConnection() + data%State%OperationScenario%PL) .and. Get_HookHeight() <= (data%State%OperationScenario%HKL + Get_NearFloorConnection() + data%State%OperationScenario%LG + data%State%OperationScenario%PL) .and.&
                Get_KellyConnection() == KELLY_CONNECTION_SINGLE .and.&
                Get_JointConnectionPossible() == .false. .and.&
                (Get_Swing() /= SWING_WELL_BEGIN .and.& 
                 Get_Swing() /= SWING_MOUSE_HOLE_BEGIN .and.&
                 Get_Swing() /= SWING_RAT_HOLE_BEGIN) .and.&
                Get_Slips() == SLIPS_SET_END) then
            
                call Set_SwingLed(.true.)
                return
            end if
               
            
            call Set_SwingLed(.false.)
            
            
        endif
        
        
        
        
        
        
        
        
    end subroutine
    
    ! subroutine Subscribe_SwingLed()
    !     implicit none
        
    !     call OnOperationConditionChange%Add(Evaluate_SwingLed)
    !     call OnHookHeightChange%Add(Evaluate_SwingLed)
    !     call OnElevatorConnectionChange%Add(Evaluate_SwingLed)
    !     call OnKellyConnectionChange%Add(Evaluate_SwingLed)
    !     call OnSwingChange%Add(Evaluate_SwingLed)
    !     call OnSlipsChange%Add(Evaluate_SwingLed)
    !     call OnFillMouseHoleLedChange%Add(Evaluate_SwingLed)
        
    ! end subroutine
    
end module CSwingLedNotification