# 1 "/mnt/c/Projects/VSIM/SimulationCore2/CSharp/Problems/CBitProblems.f90"
module CBitProblems
    use SimulationVariables
    use json_module
	implicit none    
	public 
    contains
    
    subroutine BitProblemsFromJson(parent)
        type(json_value),pointer :: parent
        type(json_core) :: json
        type(json_value),pointer :: p,pval

        call json%get(parent,'BitProblems',p)
        call ProblemFromJson(p,"JetWashout",data%problems%BitProblems%JetWashout)
        call ProblemFromJson(p,"PlugJets",data%problems%BitProblems%PlugJets)
		call json%get(p,"JetWashoutCount",pval)
		call json%get(pval,data%problems%BitProblems%JetWashoutCount)
		call json%get(p,"PlugJetsCount",pval)
		call json%get(pval,data%problems%BitProblems%PlugJetsCount)
    end subroutine

    subroutine BitProblemsToJson(parent)

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

! 1. create new node
        call json%create_object(p,'BitProblems')
        
! 2. add member of data type to new node
        call ProblemToJson(p,"JetWashout",data%problems%BitProblems%JetWashout)
        call ProblemToJson(p,"PlugJets",data%problems%BitProblems%PlugJets)
        call json%add(p, "JetWashoutCount",data%problems%BitProblems%JetWashoutCount)
        call json%add(p, "PlugJetsCount",data%problems%BitProblems%PlugJetsCount)
! 3. add new node to parent
        call json%add(parent,p)
    end subroutine

    
    subroutine ProcessBitProblemsDueTime(time)
        implicit none
        integer :: time
        if(data%problems%BitProblems%PlugJets%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BitProblems%PlugJets, ChangePlugJets, time)
        if(data%problems%BitProblems%JetWashout%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BitProblems%JetWashout, ChangeJetWashout, time)
    end subroutine
    
    subroutine ProcessBitProblemsDuePumpStrokes(strokes)
        implicit none
        integer :: strokes

        if(data%problems%BitProblems%PlugJets%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BitProblems%PlugJets, ChangePlugJets, strokes)
        if(data%problems%BitProblems%JetWashout%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BitProblems%JetWashout, ChangeJetWashout, strokes)
        
    end subroutine
    
    subroutine ProcessBitProblemsDueVolumePumped(volume)
        implicit none
        real(8) :: volume
        
        if(data%problems%BitProblems%PlugJets%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BitProblems%PlugJets, ChangePlugJets, volume)
        if(data%problems%BitProblems%JetWashout%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BitProblems%JetWashout, ChangeJetWashout, volume)
        
    end subroutine
    
    subroutine ProcessBitProblemsDueDistanceDrilled(distance)
        implicit none
        real(8) :: distance
        
        if(data%problems%BitProblems%PlugJets%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BitProblems%PlugJets, ChangePlugJets, distance)
        if(data%problems%BitProblems%JetWashout%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BitProblems%JetWashout, ChangeJetWashout, distance)
        
    end subroutine
    
    subroutine ChangePlugJets(status)
        USE FricPressDropVarsModule
        implicit none
        integer, intent (in) :: status
! if(associated(data%problems%BitProblems%PlugJetsPtr)) call data%problems%BitProblems%PlugJetsPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%BitJetsPlugged = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%BitJetsPlugged = 1
    endsubroutine
    
    subroutine ChangeJetWashout(status)
        USE FricPressDropVarsModule
        implicit none
        integer, intent (in) :: status
! if(associated(data%problems%BitProblems%JetWashoutPtr)) call data%problems%BitProblems%JetWashoutPtr(status)
        if(status == Clear_StatusType)          data%State%FricPressDrop%BitJetsWashedOut = 0
        if(status == Executed_StatusType)       data%State%FricPressDrop%BitJetsWashedOut = 1
    endsubroutine

    
end module CBitProblems