module CSlipsNotification
    use OperationScenariosModule
    implicit none
    contains
    
    subroutine Evaluate_SlipsNotification()
        implicit none
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'Evaluate_SlipsNotification=TopDrive'  
#endif            
            
            !TOPDRIVE-CODE=28
            if (Get_ZeroStringSpeed() .and.&
                GetRotaryRpm() == 0.0d0 .and.&
                Get_Slips() == SLIPS_UNSET_END .and.&
                Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 6.0) then
            
                call Set_SlipsNotification(.true.)
                return
            end if 
            
            
            
            !TOPDRIVE-CODE=29
            if (Get_ZeroStringSpeed() .and.&
                GetRotaryRpm() == 0.0d0 .and.&
                (Get_TdsElevatorModes() == TDS_ELEVATOR_CONNECTION_STRING .or.&
                Get_TdsConnectionModes() == TDS_CONNECTION_STRING) .and.&
                Get_Slips() == SLIPS_SET_END .and.&
                Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 6.0) then
            
                call Set_SlipsNotification(.true.)
                return
            end if
            
            
            

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

            !OPERATION-CODE=53
            if (Get_ZeroStringSpeed() .and.&
                GetRotaryRpm() == 0.0d0 .and.&
                !Get_KellyConnection() == KELLY_CONNECTION_STRING
                Get_Slips() == SLIPS_UNSET_END .and.&
                Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 6.0) then
                call Set_SlipsNotification(.true.)
                return
            end if
            
        
            
            
            
            !OPERATION-CODE=77
            if (Get_ZeroStringSpeed() .and.&
                GetRotaryRpm() == 0.0d0 .and.&
                Get_Slips() == SLIPS_SET_END .and.&
                Get_IsKellyBushingSetInTable() == .false. .and.&
                Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 6.0 .and.&
                (Get_ElevatorConnection() == ELEVATOR_CONNECTION_STRING .or. Get_KellyConnection() == KELLY_CONNECTION_STRING)) then
                call Set_SlipsNotification(.true.)
            
                return
            end if
            
            
            
            
            
            !if (Get_OperationCondition() == OPERATION_DRILL .and.&
            !    Get_ZeroStringSpeed() .and.&
            !    Get_SlackOff() .and.&
            !    Get_KellyConnection() == KELLY_CONNECTION_STRING) then
            !    call Set_SlipsNotification(.true.)
            !    return
            !end if
            !
            !
            !if (Get_OperationCondition() == OPERATION_TRIP .and.&
            !    Get_ZeroStringSpeed() .and.&
            !    Get_NearFloorConnection() >= 21 .and. Get_NearFloorConnection() <= 25 .and.&
            !    Get_ElevatorConnection() == ELEVATOR_CONNECTION_STRING) then
            !    call Set_SlipsNotification(.true.)
            !    return
            !end if
        
            
            
            
            call Set_SlipsNotification(.false.)

        endif
        
        
        
        
        
        
    end subroutine
    
    ! subroutine Subscribe_SlipsNotification()
    !     implicit none
        
    !     call OnOperationConditionChange%Add(Evaluate_SlipsNotification)
    !     call OnSlackOffChange%Add(Evaluate_SlipsNotification)
    !     call OnZeroStringSpeedChange%Add(Evaluate_SlipsNotification)
    !     call OnNearFloorConnectionChange%Add(Evaluate_SlipsNotification)
    !     call data%State%OperationScenario%OnElevatorConnectionChange%Add(Evaluate_SlipsNotification)
    !     call OnKellyConnectionChange%Add(Evaluate_SlipsNotification)
    !     call OnSlipsChange%Add(Evaluate_SlipsNotification)
        
    ! end subroutine
    
end module CSlipsNotification