module ChokeControlMain
    USE CHOKEVARIABLES
use SimulationVariables !@
    implicit none
    public
    contains
    
    ! subroutine ChokeControl_Setup()
    !     ! use CSimulationVariables
    !     implicit none
    !     call OnSimulationInitialization%Add(ChokeControl_Init)
    !     call OnSimulationStop%Add(ChokeControl_Init)
        ! call OnChokeControlStep%Add(ChokeControl_Step)
    !     call OnChokeControlOutput%Add(ChokeControl_Output)
    !     call OnChokeControlMain%Add(ChokeControlMainBody)
    ! end subroutine
    
    subroutine ChokeControl_Init
        call Choke_StartUp
    end subroutine ChokeControl_Init
    
    subroutine ChokeControl_Step
        use ChokeModule
        implicit none
        INTEGER CHNUMBER

        IF(data%EquipmentControl%ChokeControlPanel%ChokeSelectorSwitch== 1) THEN
            data%EquipmentControl%ChokeControlPanel%Choke1LED=1
            data%EquipmentControl%ChokeControlPanel%Choke2LED=0
            CHNUMBER=1
        ELSE
            data%EquipmentControl%ChokeControlPanel%Choke2LED=1
            data%EquipmentControl%ChokeControlPanel%Choke1LED=0
            CHNUMBER=2
        ENDIF
        if(ABS(data%EquipmentControl%ChokeControlPanel%ChokeControlLever)==1.0 .AND. data%EquipmentControl%ChokeControlPanel%ChokePanelRigAirSwitch == 1 .AND. data%State%CHOOKE(CHNUMBER)%FailMalf==0 .AND. data%State%AirDrivenPump%ChokeAirFail==0) then
            if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == 1.0) then
                data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .true.
                data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .false.
            endif
            if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == -1.0) then 
                data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .true.
                data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .false.
            endif
            data%State%AirPumpLine%TIME=data%State%AirPumpLine%TIME+data%State%AirPumpLine%DeltaT_Choke  !overal time (s)
            call airpump_code_CHOKE(CHNUMBER)
        else
            call CHOKE_MainBody2()
        endif
    end subroutine ChokeControl_Step
    
    ! subroutine ChokeControl_Output
    !     implicit none
    ! end subroutine ChokeControl_Output
        
!     subroutine ChokeControlMainBody
!         use CRigSizeVariables
!         use CHOKE  
!         implicit none

!          CALL Choke_StartUp()
!         loop1: DO
!             CALL CHOKE_MainBody
!             call sleepqq(100)
!             !IF (IsStopped==.true.) THEN
!             !    EXIT loop1
!             !ENDIF
!     !write(*,*) '1111111111111'
!       if(IsStopped) then
! !write(*,*) '22222222222222'
          
!           CALL DEALLOCATE_ARRAYS_CHOKE()
! !write(*,*) '333333333'
          
!           call Quit()
!       end if            
            
            

!         ENDDO loop1 
        
!         CLOSE(150)
        
!         !CALL DEALLOCATE_ARRAYS_CHOKE()
        
!     end subroutine ChokeControlMainBody
    
end module ChokeControlMain