module CIrIbopPermission
    use OperationScenariosModule
    implicit none
    contains
    
    subroutine Evaluate_IrIbopPermission()
        use TD_DrillStemComponents
        use UnitySignalsModule !for CTdsConnectionModesEnum
        use CStudentStationVariables!, only: data%State%StudentStation%FillupHeadInstallation
        implicit none
        
        
        if (data%Configuration%Hoisting%DriveType == TopDrive_DriveType) then
#ifdef OST
            print*, 'Evaluate_IrIbopPermission=TopDrive'
#endif


            !TOPDRIVE-CODE=58
            if (GetRotaryRpm() == 0.0d0 .and.&
                Get_NearFloorConnection() >= 3 .and. Get_NearFloorConnection() <= 10 .and.&
                Get_JointConnectionPossible() == .false. .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING .and.&
                data%State%StudentStation%FillupHeadInstallation == .false.) then
            
                call Set_IrIbopPermission(.true.)
                return
            end if


                
                
            call Set_IrIbopPermission(.false.)
                
                
                
        endif
        
        
        
        
        
        if (data%Configuration%Hoisting%DriveType == Kelly_DriveType) then
#ifdef OST
            print*, 'Evaluate_IrIbopPermission=Kelly'
#endif

            !OPERATION-CODE=62
            if (GetRotaryRpm() == 0.0d0 .and.&
                Get_OperationCondition() == OPERATION_TRIP .and.&
                !(Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .or. Get_ElevatorConnection() == ELEVATOR_LATCH_STRING) .and.&
                data%State%TD_String%TopJointHeight >= 3.0 .and. data%State%TD_String%TopJointHeight <= 10.0 .and.&
                !Get_IbopHeight() >= 22.0 .and. Get_IbopHeight() <= 35.0 .and.&
                Get_JointConnectionPossible() == .false. .and.&
                Get_Swing() == SWING_WELL_END .and.&
                Get_FillupHead() == FILLUP_HEAD_REMOVE) then
                call Set_IrIbopPermission(.true.)
                return
            end if
            
            
            !if (Get_OperationCondition() == OPERATION_DRILL .and.&
            !    Get_IbopHeight() >= 1 .and. Get_IbopHeight() <= 8 .and.&
            !    Get_NearFloorConnection() >= 1 .and. Get_NearFloorConnection() <= 8 .and.&
            !    Get_JointConnectionPossible() == .false. .and.&
            !    Get_KellyConnection() /= KELLY_CONNECTION_STRING .and.&
            !    Get_Swing() == SWING_WELL_END .and.&
            !    Get_Slips() == SLIPS_SET_END) then
            !    call Set_IrIbopPermission(.true.)
            !    return
            !end if
            
            
            
            
            call Set_IrIbopPermission(.false.)
            
            
            
        endif
        
        
        
        
        
        
    end subroutine
        
    subroutine Subscribe_IrIbopPermission()
        implicit none
    end subroutine
    
end module CIrIbopPermission