module CPumps
    use SimulationVariables
    use json_module
    implicit none    
    public    
    contains

    subroutine PumpsToJson(parent)

        type(json_value),pointer :: parent
        type(json_core) :: json
        type(json_value),pointer :: p

        ! 1. create new node
        call json%create_object(p,'Pumps')
        
        ! 2. add member of data type to new node
        ! call StringConfigurationToJson(p) 
        ! call FormationToJson(p) 
        call json%add(p,"MudPump1LinerDiameter",data%Configuration%Pumps%MudPump1LinerDiameter)
        call json%add(p,"MudPump1Stroke",data%Configuration%Pumps%MudPump1Stroke)
        call json%add(p,"MudPump1MechanicalEfficiency",data%Configuration%Pumps%MudPump1MechanicalEfficiency)
        call json%add(p,"MudPump1VolumetricEfficiency",data%Configuration%Pumps%MudPump1VolumetricEfficiency)
        call json%add(p,"MudPump1Output",data%Configuration%Pumps%MudPump1Output)
        call json%add(p,"MudPump1OutputBblStroke",data%Configuration%Pumps%MudPump1OutputBblStroke)
        call json%add(p,"MudPump1Maximum",data%Configuration%Pumps%MudPump1Maximum)
        call json%add(p,"MudPump1ReliefValvePressure",data%Configuration%Pumps%MudPump1ReliefValvePressure)
        call json%add(p,"MudPump2LinerDiameter",data%Configuration%Pumps%MudPump2LinerDiameter)
        call json%add(p,"MudPump2Stroke",data%Configuration%Pumps%MudPump2Stroke)
        call json%add(p,"MudPump2MechanicalEfficiency",data%Configuration%Pumps%MudPump2MechanicalEfficiency)
        call json%add(p,"MudPump2VolumetricEfficiency",data%Configuration%Pumps%MudPump2VolumetricEfficiency)
        call json%add(p,"MudPump2Output",data%Configuration%Pumps%MudPump2Output)
        call json%add(p,"MudPump2OutputBblStroke",data%Configuration%Pumps%MudPump2OutputBblStroke)
        call json%add(p,"MudPump2Maximum",data%Configuration%Pumps%MudPump2Maximum)
        call json%add(p,"MudPump2ReliefValvePressure",data%Configuration%Pumps%MudPump2ReliefValvePressure)
        call json%add(p,"CementPumpLinerDiameter",data%Configuration%Pumps%CementPumpLinerDiameter)
        call json%add(p,"CementPumpStroke",data%Configuration%Pumps%CementPumpStroke)
        call json%add(p,"CementPumpMechanicalEfficiency",data%Configuration%Pumps%CementPumpMechanicalEfficiency)
        call json%add(p,"CementPumpVolumetricEfficiency",data%Configuration%Pumps%CementPumpVolumetricEfficiency)
        call json%add(p,"CementPumpOutput",data%Configuration%Pumps%CementPumpOutput)
        call json%add(p,"CementPumpOutputBblStroke",data%Configuration%Pumps%CementPumpOutputBblStroke)
        call json%add(p,"CementPumpMaximum",data%Configuration%Pumps%CementPumpMaximum)
        call json%add(p,"CementPumpReliefValvePressure        ",data%Configuration%Pumps%CementPumpReliefValvePressure        )
        call json%add(p,"MudPump1ReliefValveIsSet",data%Configuration%Pumps%MudPump1ReliefValveIsSet)
        call json%add(p,"MudPump2ReliefValveIsSet",data%Configuration%Pumps%MudPump2ReliefValveIsSet)
        call json%add(p,"CementPumpReliefValveIsSet",data%Configuration%Pumps%CementPumpReliefValveIsSet)
        call json%add(p,"ManualPumpPower",data%Configuration%Pumps%ManualPumpPower)
        call json%add(p,"Valve1",data%Configuration%Pumps%Valve1)
        call json%add(p,"Valve2",data%Configuration%Pumps%Valve2)
        call json%add(p,"Valve3",data%Configuration%Pumps%Valve3)
        call json%add(p,"Valve4",data%Configuration%Pumps%Valve4)
        call json%add(p,"Valve5",data%Configuration%Pumps%Valve5)
        
        ! 3. add new node to parent
        call json%add(parent,p)
    end subroutine


    subroutine OpenPump1()
        use CManifolds
        implicit none
        call ChangeValve(16, .true.)
    end subroutine
    
    subroutine ClosePump1()
        use CManifolds
        implicit none
        call ChangeValve(16, .false.)
    end subroutine
    
    subroutine OpenPump2()
        use CManifolds
        implicit none
        call ChangeValve(17, .true.)
    end subroutine
    
    subroutine ClosePump2()
        use CManifolds
        implicit none
        call ChangeValve(17, .false.)
    end subroutine
    
    
    subroutine OpenCementPump()
        use CManifolds
        implicit none
        call ChangeValve(18, .true.)
    end subroutine
    
    subroutine CloseCementPump()
        use CManifolds
        implicit none
        call ChangeValve(18, .false.)
    end subroutine

    ! subroutine SetMudPump1LinerDiameterN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump1LinerDiameterPtr)) call MudPump1LinerDiameterPtr(a)
    ! end subroutine
    
    ! subroutine SetMudPump2LinerDiameterN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump2LinerDiameterPtr)) call MudPump2LinerDiameterPtr(a)
    ! end subroutine
    
    ! subroutine SetMudPump3LinerDiameterN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump3LinerDiameterPtr)) call MudPump3LinerDiameterPtr(a)
    ! end subroutine
    
    
    
    ! subroutine SetMudPump1OutputBblStrokeN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump1OutputBblStrokePtr)) call MudPump1OutputBblStrokePtr(a)
    ! end subroutine
    
    ! subroutine SetMudPump2OutputBblStrokeN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump2OutputBblStrokePtr)) call MudPump2OutputBblStrokePtr(a)
    ! end subroutine
    
    ! subroutine SetMudPump3OutputBblStrokeN(a)
    !     implicit none
    !     real(8) :: a
    !     if(associated(MudPump3OutputBblStrokePtr)) call MudPump3OutputBblStrokePtr(a)
    ! end subroutine
    
    
    
    
    ! subroutine CalcMudPump1LinerDiameter()
    !     use, intrinsic :: IEEE_ARITHMETIC
    !     implicit none
    !     real(8) :: a
    !     a = (MathPI / 4.d0) * data%Configuration%Pumps%MudPump1Stroke * 3.0d0 * data%Configuration%Pumps%MudPump1VolumetricEfficiency / 9702.03d0
    !     a = dsqrt(data%Configuration%Pumps%MudPump1OutputBblStroke / a)
    !     if (.not.IEEE_IS_FINITE(a) .or. IEEE_IS_NAN(a)) then
    !         data%Configuration%Pumps%MudPump1LinerDiameter = 0.0
    !     else
    !         data%Configuration%Pumps%MudPump1LinerDiameter = a
    !     endif
    !     call SetMudPump1LinerDiameterN(data%Configuration%Pumps%MudPump1LinerDiameter)
    ! end subroutine
    
    ! subroutine CalcMudPump2LinerDiameter()
    !     use, intrinsic :: IEEE_ARITHMETIC
    !     implicit none
    !     real(8) :: a
    !     a = (MathPI / 4.d0) * data%Configuration%Pumps%MudPump2Stroke * 3.0d0 * data%Configuration%Pumps%MudPump2VolumetricEfficiency / 9702.03d0
    !     a = dsqrt(data%Configuration%Pumps%MudPump2OutputBblStroke / a)
    !     if (.not.IEEE_IS_FINITE(a) .or. IEEE_IS_NAN(a)) then
    !         data%Configuration%Pumps%MudPump2LinerDiameter = 0.0
    !     else
    !         data%Configuration%Pumps%MudPump2LinerDiameter = a
    !     endif
    !     call SetMudPump2LinerDiameterN(data%Configuration%Pumps%MudPump2LinerDiameter)
    ! end subroutine
    
    ! subroutine CalcMudPump3LinerDiameter()
    !     use, intrinsic :: IEEE_ARITHMETIC
    !     implicit none
    !     real(8) :: a
    !     a = (MathPI / 4.d0) * data%Configuration%Pumps%CementPumpStroke * 3.0d0 * data%Configuration%Pumps%CementPumpVolumetricEfficiency / 9702.03d0
    !     a = dsqrt(data%Configuration%Pumps%CementPumpOutputBblStroke / a)
    !     if (.not.IEEE_IS_FINITE(a) .or. IEEE_IS_NAN(a)) then
    !         data%Configuration%Pumps%CementPumpLinerDiameter = 0.0
    !     else
    !         data%Configuration%Pumps%CementPumpLinerDiameter = a
    !     endif
    !     call SetMudPump3LinerDiameterN(data%Configuration%Pumps%CementPumpLinerDiameter)
    ! end subroutine
    
    
    
    ! subroutine CalcPump1OutputBblStroke()
    !     implicit none
    !     data%Configuration%Pumps%MudPump1OutputBblStroke = (MathPI / 4.d0) * (data%Configuration%Pumps%MudPump1LinerDiameter**2) * data%Configuration%Pumps%MudPump1Stroke * 3.0d0 * data%Configuration%Pumps%MudPump1VolumetricEfficiency / 9702.03d0
    !     call SetMudPump1OutputBblStrokeN(data%Configuration%Pumps%MudPump1OutputBblStroke)
    ! end subroutine
    
    ! subroutine CalcPump2OutputBblStroke()    
    !     implicit none
    !     data%Configuration%Pumps%MudPump2OutputBblStroke = (MathPI / 4.d0) * (data%Configuration%Pumps%MudPump2LinerDiameter**2) * data%Configuration%Pumps%MudPump2Stroke * 3.0d0 * data%Configuration%Pumps%MudPump2VolumetricEfficiency / 9702.03d0
    !     call SetMudPump2OutputBblStrokeN(data%Configuration%Pumps%MudPump2OutputBblStroke)
    ! end subroutine
    
    ! subroutine CalcPump3OutputBblStroke()
    !     implicit none
    !     data%Configuration%Pumps%CementPumpOutputBblStroke = (MathPI / 4.d0) * (data%Configuration%Pumps%CementPumpLinerDiameter**2) * data%Configuration%Pumps%CementPumpStroke * 3.0d0 * data%Configuration%Pumps%CementPumpVolumetricEfficiency / 9702.03d0
    !     call SetMudPump3OutputBblStrokeN(data%Configuration%Pumps%CementPumpOutputBblStroke)
    ! end subroutine
        
end module CPumps