module ChokeLineMain
    implicit none
    public
    contains
    
    ! subroutine ChokeLine_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     call OnSimulationInitialization%Add(ChokeLine_Init)
    !     call OnSimulationStop%Add(ChokeLine_Init)
    !     call OnChokeLineStep%Add(ChokeLine_Step)
    !     call OnChokeLineOutput%Add(ChokeLine_Output)
    !     call OnChokeLineMain%Add(ChokeLineMainBody)
    ! end subroutine
    
    ! subroutine ChokeLine_Init
    !     implicit none
    ! end subroutine ChokeLine_Init
    
    subroutine ChokeLine_Step
        use SimulationVariables
        implicit none
        if (data%State%ChokeLine%finished/=0) then
            call CHOKE_LINE
            if (data%State%ChokeLine%IsOpening .or. data%State%ChokeLine%IsClosing) then
                data%State%Annular%FirstSet= 0
                data%State%AnnularComputational%RamsFirstSet= 0
                if(data%State%ChokeLine%finished==0) then
                    call CHOKE_LINE_SUB !only body of loop2
                endif
            endif
        else
            call SHEAR_RAMS_SUB
        endif        
    end subroutine ChokeLine_Step
    
    ! subroutine ChokeLine_Output
    !     implicit none
    ! end subroutine ChokeLine_Output
    
    ! subroutine ChokeLineMainBody 
    !     USE ifport
    !     USE ifmt
    !     ! use CSimulationVariables
    !   !  USE BOP

    !     implicit none
        
    !     INTEGER :: ChokeLineDuration
    !     integer,dimension(8) :: ChokeLineStartTime , ChokeLineEndTime         
        
    !       !  CALL BOP_StartUp()
    !     loop1: DO
            
    !         CALL DATE_AND_TIME(values=ChokeLineStartTime)

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

    !     ENDDO loop1 


    !     !    CALL DEALLOCATE_ARRAYS()
            
    ! end subroutine ChokeLineMainBody
    
end module ChokeLineMain