module CChokeProblems
    use SimulationVariables
	implicit none    
	public 
    contains

    subroutine ChokeProblemsToJson(parent)

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

        ! 1. create new node
        call json%create_object(p,'ChokeProblems')
        
        ! 2. add member of data type to new node
        call ProblemToJson(p,"HydraulicChoke1Plugged",data%problems%ChokeProblems%HydraulicChoke1Plugged)
        call ProblemToJson(p,"HydraulicChoke1Fail",data%problems%ChokeProblems%HydraulicChoke1Fail)
        call ProblemToJson(p,"HydraulicChoke1Washout",data%problems%ChokeProblems%HydraulicChoke1Washout)
        call ProblemToJson(p,"HydraulicChoke2Plugged",data%problems%ChokeProblems%HydraulicChoke2Plugged)
        call ProblemToJson(p,"HydraulicChoke2Fail",data%problems%ChokeProblems%HydraulicChoke2Fail)
        call ProblemToJson(p,"HydraulicChoke2Washout",data%problems%ChokeProblems%HydraulicChoke2Washout)
        call ProblemToJson(p,"ManualChoke1Plugged",data%problems%ChokeProblems%ManualChoke1Plugged)
        call ProblemToJson(p,"ManualChoke1Fail",data%problems%ChokeProblems%ManualChoke1Fail)
        call ProblemToJson(p,"ManualChoke1Washout",data%problems%ChokeProblems%ManualChoke1Washout)
        call ProblemToJson(p,"ManualChoke2Plugged",data%problems%ChokeProblems%ManualChoke2Plugged)
        call ProblemToJson(p,"ManualChoke2Fail",data%problems%ChokeProblems%ManualChoke2Fail)
        call ProblemToJson(p,"ManualChoke2Washout",data%problems%ChokeProblems%ManualChoke2Washout)
        call ProblemToJson(p,"ChokePanelAirFail",data%problems%ChokeProblems%ChokePanelAirFail)
            
        ! 3. add new node to parent
        call json%add(parent,p)
    end subroutine

    subroutine ProcessChokeProblemsDueTime(time)
        implicit none
        integer :: time
        if(data%problems%ChokeProblems%HydraulicChoke1Plugged%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke1Plugged, ChangeHydraulicChoke1Plugged, time)
        if(data%problems%ChokeProblems%HydraulicChoke1Fail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke1Fail, ChangeHydraulicChoke1Fail, time)
        if(data%problems%ChokeProblems%HydraulicChoke1Washout%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke1Washout, ChangeHydraulicChoke1Washout, time)
        if(data%problems%ChokeProblems%HydraulicChoke2Plugged%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke2Plugged, ChangeHydraulicChoke2Plugged, time)
        if(data%problems%ChokeProblems%HydraulicChoke2Fail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke2Fail, ChangeHydraulicChoke2Fail, time)
        if(data%problems%ChokeProblems%HydraulicChoke2Washout%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%HydraulicChoke2Washout, ChangeHydraulicChoke2Washout, time)
        if(data%problems%ChokeProblems%ManualChoke1Plugged%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke1Plugged, ChangeManualChoke1Plugged, time)
        if(data%problems%ChokeProblems%ManualChoke1Fail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke1Fail, ChangeManualChoke1Fail, time)
        if(data%problems%ChokeProblems%ManualChoke1Washout%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke1Washout, ChangeManualChoke1Washout, time)
        if(data%problems%ChokeProblems%ManualChoke2Plugged%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke2Plugged, ChangeManualChoke2Plugged, time)
        if(data%problems%ChokeProblems%ManualChoke2Fail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke2Fail, ChangeManualChoke2Fail, time)
        if(data%problems%ChokeProblems%ManualChoke2Washout%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ManualChoke2Washout, ChangeManualChoke2Washout, time)
        if(data%problems%ChokeProblems%ChokePanelAirFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%ChokeProblems%ChokePanelAirFail, ChangeChokePanelAirFail, time)
    end subroutine
	
	subroutine ProcessChokeProblemsDuePumpStrokes(strokes)
        implicit none
        integer :: strokes
        if(data%problems%ChokeProblems%HydraulicChoke1Plugged%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke1Plugged, ChangeHydraulicChoke1Plugged, strokes)
        if(data%problems%ChokeProblems%HydraulicChoke1Fail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke1Fail, ChangeHydraulicChoke1Fail, strokes)
        if(data%problems%ChokeProblems%HydraulicChoke1Washout%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke1Washout, ChangeHydraulicChoke1Washout, strokes)
        if(data%problems%ChokeProblems%HydraulicChoke2Plugged%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke2Plugged, ChangeHydraulicChoke2Plugged, strokes)
        if(data%problems%ChokeProblems%HydraulicChoke2Fail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke2Fail, ChangeHydraulicChoke2Fail, strokes)
        if(data%problems%ChokeProblems%HydraulicChoke2Washout%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%HydraulicChoke2Washout, ChangeHydraulicChoke2Washout, strokes)
        if(data%problems%ChokeProblems%ManualChoke1Plugged%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke1Plugged, ChangeManualChoke1Plugged, strokes)
        if(data%problems%ChokeProblems%ManualChoke1Fail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke1Fail, ChangeManualChoke1Fail, strokes)
        if(data%problems%ChokeProblems%ManualChoke1Washout%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke1Washout, ChangeManualChoke1Washout, strokes)
        if(data%problems%ChokeProblems%ManualChoke2Plugged%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke2Plugged, ChangeManualChoke2Plugged, strokes)
        if(data%problems%ChokeProblems%ManualChoke2Fail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke2Fail, ChangeManualChoke2Fail, strokes)
        if(data%problems%ChokeProblems%ManualChoke2Washout%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ManualChoke2Washout, ChangeManualChoke2Washout, strokes)
        if(data%problems%ChokeProblems%ChokePanelAirFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%ChokeProblems%ChokePanelAirFail, ChangeChokePanelAirFail, strokes)
    end subroutine
	
	subroutine ProcessChokeProblemsDueVolumePumped(volume)
        implicit none
        real(8) :: volume
        if(data%problems%ChokeProblems%HydraulicChoke1Plugged%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke1Plugged, ChangeHydraulicChoke1Plugged, volume)
        if(data%problems%ChokeProblems%HydraulicChoke1Fail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke1Fail, ChangeHydraulicChoke1Fail, volume)
        if(data%problems%ChokeProblems%HydraulicChoke1Washout%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke1Washout, ChangeHydraulicChoke1Washout, volume)
        if(data%problems%ChokeProblems%HydraulicChoke2Plugged%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke2Plugged, ChangeHydraulicChoke2Plugged, volume)
        if(data%problems%ChokeProblems%HydraulicChoke2Fail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke2Fail, ChangeHydraulicChoke2Fail, volume)
        if(data%problems%ChokeProblems%HydraulicChoke2Washout%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%HydraulicChoke2Washout, ChangeHydraulicChoke2Washout, volume)
        if(data%problems%ChokeProblems%ManualChoke1Plugged%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke1Plugged, ChangeManualChoke1Plugged, volume)
        if(data%problems%ChokeProblems%ManualChoke1Fail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke1Fail, ChangeManualChoke1Fail, volume)
        if(data%problems%ChokeProblems%ManualChoke1Washout%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke1Washout, ChangeManualChoke1Washout, volume)
        if(data%problems%ChokeProblems%ManualChoke2Plugged%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke2Plugged, ChangeManualChoke2Plugged, volume)
        if(data%problems%ChokeProblems%ManualChoke2Fail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke2Fail, ChangeManualChoke2Fail, volume)
        if(data%problems%ChokeProblems%ManualChoke2Washout%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ManualChoke2Washout, ChangeManualChoke2Washout, volume)
        if(data%problems%ChokeProblems%ChokePanelAirFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%ChokeProblems%ChokePanelAirFail, ChangeChokePanelAirFail, volume)
    end subroutine
	
	subroutine ProcessChokeProblemsDueDistanceDrilled(distance)
        implicit none
        real(8) :: distance
        if(data%problems%ChokeProblems%HydraulicChoke1Plugged%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke1Plugged, ChangeHydraulicChoke1Plugged, distance)
        if(data%problems%ChokeProblems%HydraulicChoke1Fail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke1Fail, ChangeHydraulicChoke1Fail, distance)
        if(data%problems%ChokeProblems%HydraulicChoke1Washout%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke1Washout, ChangeHydraulicChoke1Washout, distance)
        if(data%problems%ChokeProblems%HydraulicChoke2Plugged%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke2Plugged, ChangeHydraulicChoke2Plugged, distance)
        if(data%problems%ChokeProblems%HydraulicChoke2Fail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke2Fail, ChangeHydraulicChoke2Fail, distance)
        if(data%problems%ChokeProblems%HydraulicChoke2Washout%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%HydraulicChoke2Washout, ChangeHydraulicChoke2Washout, distance)
        if(data%problems%ChokeProblems%ManualChoke1Plugged%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke1Plugged, ChangeManualChoke1Plugged, distance)
        if(data%problems%ChokeProblems%ManualChoke1Fail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke1Fail, ChangeManualChoke1Fail, distance)
        if(data%problems%ChokeProblems%ManualChoke1Washout%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke1Washout, ChangeManualChoke1Washout, distance)
        if(data%problems%ChokeProblems%ManualChoke2Plugged%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke2Plugged, ChangeManualChoke2Plugged, distance)
        if(data%problems%ChokeProblems%ManualChoke2Fail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke2Fail, ChangeManualChoke2Fail, distance)
        if(data%problems%ChokeProblems%ManualChoke2Washout%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ManualChoke2Washout, ChangeManualChoke2Washout, distance)
        if(data%problems%ChokeProblems%ChokePanelAirFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%ChokeProblems%ChokePanelAirFail, ChangeChokePanelAirFail, distance)
    end subroutine
	
    
    
    
    subroutine ChangeHydraulicChoke1Plugged(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke1PluggedPtr)) call HydraulicChoke1PluggedPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(1)%PlugMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(1)%PlugMalf = 1
    endsubroutine
    
    subroutine ChangeHydraulicChoke1Fail(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke1FailPtr)) call HydraulicChoke1FailPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(1)%FailMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(1)%FailMalf = 1
    endsubroutine
    
    subroutine ChangeHydraulicChoke1Washout(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        use CChokeManifoldVariables
    use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke1WashoutPtr)) call HydraulicChoke1WashoutPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(1)%WashoutMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(1)%WashoutMalf = 1
        
        if(status == Clear_StatusType) data%EquipmentControl%ChokeManifold%HyChock1OnProblem = .false.
        if(status == Executed_StatusType) data%EquipmentControl%ChokeManifold%HyChock1OnProblem = .true.
    endsubroutine
    
    subroutine ChangeHydraulicChoke2Plugged(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke2PluggedPtr)) call HydraulicChoke2PluggedPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(2)%PlugMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(2)%PlugMalf = 1
    endsubroutine
    
    subroutine ChangeHydraulicChoke2Fail(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke2FailPtr)) call HydraulicChoke2FailPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(2)%FailMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(2)%FailMalf = 1
    endsubroutine
    
    subroutine ChangeHydraulicChoke2Washout(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        use CChokeManifoldVariables
    use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(HydraulicChoke2WashoutPtr)) call HydraulicChoke2WashoutPtr(status)
        if(status == Clear_StatusType) data%State%CHOOKE(2)%WashoutMalf = 0
        if(status == Executed_StatusType) data%State%CHOOKE(2)%WashoutMalf = 1
        
        if(status == Clear_StatusType) data%EquipmentControl%ChokeManifold%HyChock2OnProblem = .false.
        if(status == Executed_StatusType) data%EquipmentControl%ChokeManifold%HyChock2OnProblem = .true.
    endsubroutine
    
    subroutine ChangeManualChoke1Plugged(status)
        USE FricPressDropVarsModule
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke1PluggedPtr)) call ManualChoke1PluggedPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%ManChoke1Plug = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%ManChoke1Plug = 1
    endsubroutine
    
    subroutine ChangeManualChoke1Fail(status)
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke1FailPtr)) call ManualChoke1FailPtr(status)
        !if(status == Clear_StatusType) print*,'On_ManualChoke1Fail_Clear'
        !if(status == Executed_StatusType) print*,'On_ManualChoke1Fail_Execute'
    endsubroutine
    
    subroutine ChangeManualChoke1Washout(status)
        USE FricPressDropVarsModule
        use CChokeManifoldVariables
    use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke1WashoutPtr)) call ManualChoke1WashoutPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%ManChoke1Washout = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%ManChoke1Washout = 1
        
        if(status == Clear_StatusType) data%EquipmentControl%ChokeManifold%LeftManChokeOnProblem = .false.
        if(status == Executed_StatusType) data%EquipmentControl%ChokeManifold%LeftManChokeOnProblem = .true.
    endsubroutine
    
    subroutine ChangeManualChoke2Plugged(status)
        USE FricPressDropVarsModule
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke2PluggedPtr)) call ManualChoke2PluggedPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%ManChoke2Plug = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%ManChoke2Plug = 1
    endsubroutine
    
    subroutine ChangeManualChoke2Fail(status)
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke2FailPtr)) call ManualChoke2FailPtr(status)
        !if(status == Clear_StatusType) print*,'On_ManualChoke2Fail_Clear'
        !if(status == Executed_StatusType) print*,'On_ManualChoke2Fail_Execute'
    endsubroutine
    
    subroutine ChangeManualChoke2Washout(status)
        USE FricPressDropVarsModule
        use CChokeManifoldVariables
    use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(ManualChoke2WashoutPtr)) call ManualChoke2WashoutPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%ManChoke2Washout = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%ManChoke2Washout = 1
        
        if(status == Clear_StatusType) data%EquipmentControl%ChokeManifold%RightManChokeOnProblem = .false.
        if(status == Executed_StatusType) data%EquipmentControl%ChokeManifold%RightManChokeOnProblem = .true.
    endsubroutine
    
    subroutine ChangeChokePanelAirFail(status)
        USE CHOKEVARIABLES
use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        ! if(associated(ChokePanelAirFailPtr)) call ChokePanelAirFailPtr(status)
        if(status == Clear_StatusType) data%State%AirDrivenPump%ChokeAirFail = 0
        if(status == Executed_StatusType) data%State%AirDrivenPump%ChokeAirFail = 1
    endsubroutine
    
end module CChokeProblems