module BlindRamsMain
    implicit none
    public
    contains
    
    ! subroutine BlindRams_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     call OnSimulationInitialization%Add(BlindRams_Init)
    !     call OnSimulationStop%Add(BlindRams_Init)
    !     call OnBlindRamsStep%Add(BlindRams_Step)
    !     call OnBlindRamsOutput%Add(BlindRams_Output)
    !     call OnBlindRamsMain%Add(BlindRamsMainBody)
    ! end subroutine
    
    ! subroutine BlindRams_Init
    !     implicit none
    ! end subroutine BlindRams_Init
    
    subroutine BlindRams_Step
        use SimulationVariables
        implicit none
        if (data%State%RamLine%FINISHED_shear/=0) then
            call SHEAR_RAMS
            if (data%State%RamLine%ShearRamIsOpening .or. data%State%RamLine%ShearRamIsClosing .or. data%State%RAM(4)%Bottles_Charged_MalfActive) then
                data%State%Annular%FirstSet= 0
                data%State%AnnularComputational%RamsFirstSet= 0
                if(data%State%RamLine%FINISHED_shear==0) then
                    call SHEAR_RAMS_SUB !only body of loop2
                    if (data%State%RamLine%FINISHED_shear/=0 .and. data%State%RamLine%FINISHED_shear==1 .and. data%State%RAM(4)%Bottles_Charged_MalfActive==.true.) then
                        call bop_code(1,data%State%ShearRAM%H_ShearRamBop,4)
                    endif
                endif
            endif
        else
            call SHEAR_RAMS_SUB
            if (data%State%RamLine%FINISHED_shear/=0 .and. data%State%RamLine%FINISHED_shear==1 .and. data%State%RAM(4)%Bottles_Charged_MalfActive==.true.) then
                call bop_code(1,data%State%ShearRAM%H_ShearRamBop,4)
            endif
        endif

    end subroutine BlindRams_Step
    
    ! subroutine BlindRams_Output
    !     implicit none
    ! end subroutine BlindRams_Output
    
    ! subroutine BlindRamsMainBody
    !     USE ifport
    !     USE ifmt
    !     ! use CSimulationVariables
    !   !  USE BOP
        
    !     implicit none  
        
    !     INTEGER :: ShearRamDuration
    !     integer,dimension(8) :: ShearRamStartTime , ShearRamEndTime          
        
            
    ! !         CALL BOP_StartUp()
    !     loop1: DO
            
    !         CALL DATE_AND_TIME(values=ShearRamStartTime)

            
    !         CALL SHEAR_RAMS
            
    !         CALL DATE_AND_TIME(values=ShearRamEndTime)
            
    !         ShearRamDuration = 3600000 * (ShearRamEndTime(5) - ShearRamStartTime(5)) + 60000 * (ShearRamEndTime(6) - ShearRamStartTime(6)) + 1000 * (ShearRamEndTime(7) - ShearRamStartTime(7)) + (ShearRamEndTime(8) - ShearRamStartTime(8))
            
    !         if (ShearRamDuration < 100) then
    !             call sleepqq(100 - ShearRamDuration)
    !         ELSE
    !             WRITE (*,*) 'ShearRam BOP run duration exceeded 100 ms and =', ShearRamDuration
    !         end if            
            
            
    !         IF (IsStopped==.true.) THEN
    !             EXIT loop1
    !         ENDIF
            

    !     ENDDO loop1 
        
    ! end subroutine BlindRamsMainBody
    
end module BlindRamsMain