module FluidFlowMain
    implicit none
    public
    contains
!
    ! subroutine FluidFlow_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     !call OnSimulationInitialization%Add(FluidFlow_Init)
    !     call OnSimulationStop%Add(FluidFlow_Stop)
    !     call OnFluidFlowStart%Add(FluidFlow_Start)
    !     call OnFluidFlowStep%Add(FluidFlow_Step)
    !     !call OnFluidFlowOutput%Add(FluidFlow_Output)
    !     call OnFluidFlowMain%Add(FluidFlowMainBody)
    ! end subroutine
    
    ! subroutine FluidFlow_Stop
    !     implicit none
    !     !WRITE (*,*) ' fluid flow done_Stop'
    !     call DEALLOCATE_ARRAYS_NormalCirculation()
    !     CALL DeallocateFlowTypes
    ! end subroutine FluidFlow_Stop
    
    subroutine FluidFlow_Init
        USE Fluid_Flow_Startup_Vars
        implicit none
        !WRITE (*,*) ' fluid flow done_Start'
        CALL NormalCirculation_StartUp()
        CALL FlowStartup
        
        Call TD_StartUp
        Call TD_WellReadData
        Call TD_WellElementsReadData
        Call TD_DrillStemReadData
        Call TD_PipePropertiesReadData
    end subroutine FluidFlow_Init
    
    subroutine FluidFlow_Step
        implicit none
        integer :: i, FlowDuration, SimulationStateOld
        integer,dimension(8) :: FlowStartTime,FlowEndTime
        
        !WRITE (*,*) ' fluid flow done_Step'
        !call Fluid_Flow_Solver
        
        
        
        
        
        
        CALL DATE_AND_TIME(values=FlowStartTime)
        call Fluid_Flow_Solver
         
        CALL DATE_AND_TIME(values=FlowEndTime)

        FlowDuration = 3600000 * (FlowEndTime(5) - FlowStartTime(5)) + 60000 * (FlowEndTime(6) - FlowStartTime(6)) + 1000 * (FlowEndTime(7) - FlowStartTime(7)) + (FlowEndTime(8) - FlowStartTime(8))
        
        WRITE (*,*) 'FlowDuration (ms)=' , FlowDuration
        
    end subroutine FluidFlow_Step
    
    !subroutine FluidFlow_Output
    !    implicit none
    !end subroutine FluidFlow_Output
    
!     subroutine FluidFlowMainBody
! !
!     use ifport
!     use ifmt
!     ! use CSimulationVariables
!     USE Fluid_Flow_Startup_Vars
!     !use general_info, only : reset_data
!     !use well_info
!     !use drilling_info
!     use CLog1
! !
!     implicit none

    !integer :: i, FlowDuration, SimulationStateOld
    !integer,dimension(8) :: FlowStartTime,FlowEndTime
    !
    !
    !CALL NormalCirculation_StartUp()
    !CALL FlowStartup
    !
    !Call TD_StartUp
    !Call TD_WellReadData
    !Call TD_WellElementsReadData
    !Call TD_DrillStemReadData
    !Call TD_PipePropertiesReadData
    !
    !
    !LoopSimulation: do 
    !    !WRITE (*,*) ' fluid flow done 0'
    !!
    !    CALL DATE_AND_TIME(values=FlowStartTime)
    !    !WRITE (*,*) 'FlowStartTime=', FlowStartTime
    !!
    !    call Fluid_Flow_Solver
    !        
    !!
    !    CALL DATE_AND_TIME(values=FlowEndTime)
    !!
    !    !WRITE (*,*) ' fluid flow done 1'
    !
    !      
    !
    !    FlowDuration = 3600000 * (FlowEndTime(5) - FlowStartTime(5)) + 60000 * (FlowEndTime(6) - FlowStartTime(6)) + 1000 * (FlowEndTime(7) - FlowStartTime(7)) + (FlowEndTime(8) - FlowStartTime(8))
    !      
    !      
    !    !call Log_1('FlowDuration=', FlowDuration)
    !    !WRITE (*,*) 'FlowDuration (ms)=' , FlowDuration
    !    
    !    if ((100 - FlowDuration) > 0) then
    !        !WRITE (*,*) 'fluid flow done 2'
    !        call sleepqq(100 - FlowDuration)
    !        !WRITE (*,*) ' fluid flow done 3'
    !        
    !    end if
    !    !WRITE (*,*) ' fluid flow done 4'
    !      
    !    
    !      
    !    !WRITE (*,*) "FlowDuration", FlowDuration
    !        !if(IsStopped) then
    !        !    EXIT LoopSimulation
    !        !ENDIF
    !    !write(*,*) 'IsStopped=' , IsStopped
    !      
    !    if(IsStopped) then
    !    !write(*,*) '44444444444'
    !          
    !        call DEALLOCATE_ARRAYS_NormalCirculation()
    !        CALL DeallocateFlowTypes
    !        call Quit()
    !    end if
    !    !  
    !    !  if(IsStopped) exit LoopSimulation
    !    !  
    !end do LoopSimulation
    !!call DEALLOCATE_ARRAYS_NormalCirculation()
    !!CALL DeallocateFlowTypes
    !
!
    ! end subroutine FluidFlowMainBody
    
end module FluidFlowMain