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



            !TOPDRIVE-CODE=55
            if (GetRotaryRpm() == 0.0d0 .and.&
                (Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 10.0) .and.&
                Get_JointConnectionPossible() == .false. .and.&
                Get_TdsConnectionModes() == TDS_CONNECTION_NOTHING .and.&
                data%State%StudentStation%FillupHeadInstallation == .false.) then
            
                call Set_IrSafetyValvePermission(.true.)
                return
            end if



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


            !OPERATION-CODE=57
            if (Get_OperationCondition() == OPERATION_TRIP .and.&
                GetRotaryRpm() == 0.0d0 .and.&
                !(Get_ElevatorConnection() == ELEVATOR_CONNECTION_NOTHING .or. Get_ElevatorConnection() == ELEVATOR_LATCH_STRING) .and.&
                !Get_NearFloorConnection() >= 3.0 .and. Get_NearFloorConnection() <= 10.0 .and.&
                data%State%TD_String%TopJointHeight >= 3.0 .and. data%State%TD_String%TopJointHeight <= 10.0 .and.&
                !Get_SafetyValveHeight() >= 22.0 .and. Get_SafetyValveHeight() <= 35.0 .and.&
                Get_JointConnectionPossible() == .false. .and.&
                Get_FillupHead() == FILLUP_HEAD_REMOVE .and.&
                Get_Swing() == SWING_WELL_END) then
                call Set_IrSafetyValvePermission(.true.)
                return
            end if
                
                
                
                
                
            call Set_IrSafetyValvePermission(.false.)
                
        endif
        
        
        
        
            
            
            
        
        
        
    end subroutine
    
    subroutine Subscribe_IrSafetyValvePermission()
        implicit none
        ! imp me...
    end subroutine
    
end module CIrSafetyValvePermission