module CBopProblems
    use SimulationVariables
	implicit none    
	public 
    contains   

    subroutine BopProblemsToJson(parent)

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

        ! 1. create new node
        call json%create_object(p,'BopProblems')
        
        ! 2. add member of data type to new node
        call ProblemToJson(p,"AnnularWash",data%problems%BopProblems%AnnularWash)
        call ProblemToJson(p,"AnnularFail",data%problems%BopProblems%AnnularFail)
        call ProblemToJson(p,"AnnularLeak",data%problems%BopProblems%AnnularLeak)
        call ProblemToJson(p,"UpperRamWash",data%problems%BopProblems%UpperRamWash)
        call ProblemToJson(p,"UpperRamFail",data%problems%BopProblems%UpperRamFail)
        call ProblemToJson(p,"UpperRamLeak",data%problems%BopProblems%UpperRamLeak)
        call ProblemToJson(p,"MiddleRamWash",data%problems%BopProblems%MiddleRamWash)
        call ProblemToJson(p,"MiddleRamFail",data%problems%BopProblems%MiddleRamFail)
        call ProblemToJson(p,"MiddleRamLeak",data%problems%BopProblems%MiddleRamLeak)
        call ProblemToJson(p,"LowerRamWash",data%problems%BopProblems%LowerRamWash)
        call ProblemToJson(p,"LowerRamFail",data%problems%BopProblems%LowerRamFail)
        call ProblemToJson(p,"LowerRamLeak",data%problems%BopProblems%LowerRamLeak)
        call ProblemToJson(p,"AccumulatorPumpFail",data%problems%BopProblems%AccumulatorPumpFail)
        call ProblemToJson(p,"AccumulatorPumpLeak",data%problems%BopProblems%AccumulatorPumpLeak)
        call ProblemToJson(p,"AccumulatorSystemFail",data%problems%BopProblems%AccumulatorSystemFail)
        call ProblemToJson(p,"AccumulatorSystemLeak",data%problems%BopProblems%AccumulatorSystemLeak)
        
        ! 3. add new node to parent
        call json%add(parent,p)
    end subroutine

    subroutine ProcessBopProblemsDueTime(time)
        implicit none
        integer :: time
        if(data%problems%BopProblems%AnnularWash%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AnnularWash, ChangeAnnularWash, time)
        if(data%problems%BopProblems%AnnularFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AnnularFail, ChangeAnnularFail, time)
        if(data%problems%BopProblems%AnnularLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AnnularLeak, ChangeAnnularLeak, time)
        if(data%problems%BopProblems%UpperRamWash%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%UpperRamWash, ChangeUpperRamWash, time)
        if(data%problems%BopProblems%UpperRamFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%UpperRamFail, ChangeUpperRamFail, time)
        if(data%problems%BopProblems%UpperRamLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%UpperRamLeak, ChangeUpperRamLeak, time)
        if(data%problems%BopProblems%MiddleRamWash%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%MiddleRamWash, ChangeMiddleRamWash, time)
        if(data%problems%BopProblems%MiddleRamFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%MiddleRamFail, ChangeMiddleRamFail, time)
        if(data%problems%BopProblems%MiddleRamLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%MiddleRamLeak, ChangeMiddleRamLeak, time)
        if(data%problems%BopProblems%LowerRamWash%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%LowerRamWash, ChangeLowerRamWash, time)
        if(data%problems%BopProblems%LowerRamFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%LowerRamFail, ChangeLowerRamFail, time)
        if(data%problems%BopProblems%LowerRamLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%LowerRamLeak, ChangeLowerRamLeak, time)
        if(data%problems%BopProblems%AccumulatorPumpFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AccumulatorPumpFail, ChangeAccumulatorPumpFail, time)
        if(data%problems%BopProblems%AccumulatorPumpLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AccumulatorPumpLeak, ChangeAccumulatorPumpLeak, time)
        if(data%problems%BopProblems%AccumulatorSystemFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AccumulatorSystemFail, ChangeAccumulatorSystemFail, time)
        if(data%problems%BopProblems%AccumulatorSystemLeak%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%BopProblems%AccumulatorSystemLeak, ChangeAccumulatorSystemLeak, time)
    end subroutine
	
	subroutine ProcessBopProblemsDuePumpStrokes(strokes)
        implicit none
        integer :: strokes
        if(data%problems%BopProblems%AnnularWash%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AnnularWash, ChangeAnnularWash, strokes)
        if(data%problems%BopProblems%AnnularFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AnnularFail, ChangeAnnularFail, strokes)
        if(data%problems%BopProblems%AnnularLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AnnularLeak, ChangeAnnularLeak, strokes)
        if(data%problems%BopProblems%UpperRamWash%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%UpperRamWash, ChangeUpperRamWash, strokes)
        if(data%problems%BopProblems%UpperRamFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%UpperRamFail, ChangeUpperRamFail, strokes)
        if(data%problems%BopProblems%UpperRamLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%UpperRamLeak, ChangeUpperRamLeak, strokes)
        if(data%problems%BopProblems%MiddleRamWash%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%MiddleRamWash, ChangeMiddleRamWash, strokes)
        if(data%problems%BopProblems%MiddleRamFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%MiddleRamFail, ChangeMiddleRamFail, strokes)
        if(data%problems%BopProblems%MiddleRamLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%MiddleRamLeak, ChangeMiddleRamLeak, strokes)
        if(data%problems%BopProblems%LowerRamWash%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%LowerRamWash, ChangeLowerRamWash, strokes)
        if(data%problems%BopProblems%LowerRamFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%LowerRamFail, ChangeLowerRamFail, strokes)
        if(data%problems%BopProblems%LowerRamLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%LowerRamLeak, ChangeLowerRamLeak, strokes)
        if(data%problems%BopProblems%AccumulatorPumpFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AccumulatorPumpFail, ChangeAccumulatorPumpFail, strokes)
        if(data%problems%BopProblems%AccumulatorPumpLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AccumulatorPumpLeak, ChangeAccumulatorPumpLeak, strokes)
        if(data%problems%BopProblems%AccumulatorSystemFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AccumulatorSystemFail, ChangeAccumulatorSystemFail, strokes)
        if(data%problems%BopProblems%AccumulatorSystemLeak%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%BopProblems%AccumulatorSystemLeak, ChangeAccumulatorSystemLeak, strokes)
    end subroutine
	
	subroutine ProcessBopProblemsDueVolumePumped(volume)
        implicit none
        real(8) :: volume
        if(data%problems%BopProblems%AnnularWash%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AnnularWash, ChangeAnnularWash, volume)
        if(data%problems%BopProblems%AnnularFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AnnularFail, ChangeAnnularFail, volume)
        if(data%problems%BopProblems%AnnularLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AnnularLeak, ChangeAnnularLeak, volume)
        if(data%problems%BopProblems%UpperRamWash%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%UpperRamWash, ChangeUpperRamWash, volume)
        if(data%problems%BopProblems%UpperRamFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%UpperRamFail, ChangeUpperRamFail, volume)
        if(data%problems%BopProblems%UpperRamLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%UpperRamLeak, ChangeUpperRamLeak, volume)
        if(data%problems%BopProblems%MiddleRamWash%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%MiddleRamWash, ChangeMiddleRamWash, volume)
        if(data%problems%BopProblems%MiddleRamFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%MiddleRamFail, ChangeMiddleRamFail, volume)
        if(data%problems%BopProblems%MiddleRamLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%MiddleRamLeak, ChangeMiddleRamLeak, volume)
        if(data%problems%BopProblems%LowerRamWash%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%LowerRamWash, ChangeLowerRamWash, volume)
        if(data%problems%BopProblems%LowerRamFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%LowerRamFail, ChangeLowerRamFail, volume)
        if(data%problems%BopProblems%LowerRamLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%LowerRamLeak, ChangeLowerRamLeak, volume)
        if(data%problems%BopProblems%AccumulatorPumpFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AccumulatorPumpFail, ChangeAccumulatorPumpFail, volume)
        if(data%problems%BopProblems%AccumulatorPumpLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AccumulatorPumpLeak, ChangeAccumulatorPumpLeak, volume)
        if(data%problems%BopProblems%AccumulatorSystemFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AccumulatorSystemFail, ChangeAccumulatorSystemFail,volume)
        if(data%problems%BopProblems%AccumulatorSystemLeak%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%BopProblems%AccumulatorSystemLeak, ChangeAccumulatorSystemLeak, volume)
    end subroutine
	
	subroutine ProcessBopProblemsDueDistanceDrilled(distance)
        implicit none
        real(8) :: distance
        if(data%problems%BopProblems%AnnularWash%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AnnularWash, ChangeAnnularWash, distance)
        if(data%problems%BopProblems%AnnularFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AnnularFail, ChangeAnnularFail, distance)
        if(data%problems%BopProblems%AnnularLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AnnularLeak, ChangeAnnularLeak, distance)
        if(data%problems%BopProblems%UpperRamWash%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%UpperRamWash, ChangeUpperRamWash, distance)
        if(data%problems%BopProblems%UpperRamFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%UpperRamFail, ChangeUpperRamFail, distance)
        if(data%problems%BopProblems%UpperRamLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%UpperRamLeak, ChangeUpperRamLeak, distance)
        if(data%problems%BopProblems%MiddleRamWash%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%MiddleRamWash, ChangeMiddleRamWash, distance)
        if(data%problems%BopProblems%MiddleRamFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%MiddleRamFail, ChangeMiddleRamFail, distance)
        if(data%problems%BopProblems%MiddleRamLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%MiddleRamLeak, ChangeMiddleRamLeak, distance)
        if(data%problems%BopProblems%LowerRamWash%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%LowerRamWash, ChangeLowerRamWash, distance)
        if(data%problems%BopProblems%LowerRamFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%LowerRamFail, ChangeLowerRamFail, distance)
        if(data%problems%BopProblems%LowerRamLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%LowerRamLeak, ChangeLowerRamLeak, distance)
        if(data%problems%BopProblems%AccumulatorPumpFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AccumulatorPumpFail, ChangeAccumulatorPumpFail, distance)
        if(data%problems%BopProblems%AccumulatorPumpLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AccumulatorPumpLeak, ChangeAccumulatorPumpLeak, distance)
        if(data%problems%BopProblems%AccumulatorSystemFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AccumulatorSystemFail, ChangeAccumulatorSystemFail, distance)
        if(data%problems%BopProblems%AccumulatorSystemLeak%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%BopProblems%AccumulatorSystemLeak, ChangeAccumulatorSystemLeak, distance)
    end subroutine
    
    
    subroutine ChangeAnnularWash(status)
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(AnnularWashPtr)) call AnnularWashPtr(status)
        !if(status == Clear_StatusType) print*,'On_AnnularWash_Clear'
        !if(status == Executed_StatusType) print*,'On_AnnularWash_Execute'
    endsubroutine
    
    subroutine ChangeAnnularFail(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(AnnularFailPtr)) call AnnularFailPtr(status)
        if(status == Clear_StatusType) data%State%Annular%AnnularFailureMalf = 0
        if(status == Executed_StatusType) data%State%Annular%AnnularFailureMalf = 1
    endsubroutine
    
    subroutine ChangeAnnularLeak(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(AnnularLeakPtr)) call AnnularLeakPtr(status)
        if(status == Clear_StatusType) data%State%Annular%AnnularLeakMalf = 0
        if(status == Executed_StatusType) data%State%Annular%AnnularLeakMalf = 1
    endsubroutine
    
    
    
    subroutine ChangeUpperRamWash(status)
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(UpperRamWashPtr)) call UpperRamWashPtr(status)
        !if(status == Clear_StatusType) print*,'On_UpperRamWash_Clear'
        !if(status == Executed_StatusType) print*,'On_UpperRamWash_Execute'
    endsubroutine
    
    subroutine ChangeUpperRamFail(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(UpperRamFailPtr)) call UpperRamFailPtr(status)
        if(status == Clear_StatusType) data%State%PipeRam1%UpperRamsFailureMalf = 0
        if(status == Executed_StatusType) data%State%PipeRam1%UpperRamsFailureMalf = 1
    endsubroutine
    
    subroutine ChangeUpperRamLeak(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(UpperRamLeakPtr)) call UpperRamLeakPtr(status)
        if(status == Clear_StatusType) data%State%PipeRam1%UpperRamsLeakMalf = 0
        if(status == Executed_StatusType) data%State%PipeRam1%UpperRamsLeakMalf = 1
    endsubroutine
    
    
    subroutine ChangeMiddleRamWash(status)
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(MiddleRamWashPtr)) call MiddleRamWashPtr(status)
        !if(status == Clear_StatusType) print*,'On_MiddleRamWash_Clear'
        !if(status == Executed_StatusType) print*,'On_MiddleRamWash_Execute'
    endsubroutine
    
    subroutine ChangeMiddleRamFail(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(MiddleRamFailPtr)) call MiddleRamFailPtr(status)
        if(status == Clear_StatusType) data%State%ShearRAM%MiddleRamsFailureMalf = 0
        if(status == Executed_StatusType) data%State%ShearRAM%MiddleRamsFailureMalf = 1
    endsubroutine
    
    subroutine ChangeMiddleRamLeak(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(MiddleRamLeakPtr)) call MiddleRamLeakPtr(status)
        if(status == Clear_StatusType) data%State%ShearRAM%MiddleRamsLeakMalf = 0
        if(status == Executed_StatusType) data%State%ShearRAM%MiddleRamsLeakMalf = 1
    endsubroutine
    
    
    
    subroutine ChangeLowerRamWash(status)
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(LowerRamWashPtr)) call LowerRamWashPtr(status)
        !if(status == Clear_StatusType) print*,'On_LowerRamWash_Clear'
        !if(status == Executed_StatusType) print*,'On_LowerRamWash_Execute'
    endsubroutine
    
    subroutine ChangeLowerRamFail(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! ! if(associated(LowerRamFailPtr)) call LowerRamFailPtr(status)
        if(status == Clear_StatusType) data%State%PipeRam2%LowerRamsFailureMalf = 0
        if(status == Executed_StatusType) data%State%PipeRam2%LowerRamsFailureMalf = 1
    endsubroutine
    
    subroutine ChangeLowerRamLeak(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(LowerRamLeakPtr)) call LowerRamLeakPtr(status)
        if(status == Clear_StatusType) data%State%PipeRam2%LowerRamsLeakMalf = 0
        if(status == Executed_StatusType) data%State%PipeRam2%LowerRamsLeakMalf = 1
    endsubroutine
    
    subroutine ChangeAccumulatorPumpFail(status)
        use SimulationVariables
        implicit none
        integer, intent (in) :: status
        ! if(associated(AccumulatorPumpFailPtr)) call AccumulatorPumpFailPtr(status)
        if(status == Clear_StatusType) data%State%BopStackAcc%AccPupmsFailMalf = 0
        if(status == Executed_StatusType) data%State%BopStackAcc%AccPupmsFailMalf = 1
    endsubroutine
    
    subroutine ChangeAccumulatorPumpLeak(status)
        implicit none
        integer, intent (in) :: status
        ! if(associated(AccumulatorPumpLeakPtr)) call AccumulatorPumpLeakPtr(status)
        !if(status == Clear_StatusType) print*,'On_AccumulatorPumpLeak_Clear'
        !if(status == Executed_StatusType) print*,'On_AccumulatorPumpLeak_Execute'
    endsubroutine
    
    subroutine ChangeAccumulatorSystemFail(status)
        implicit none
        integer, intent (in) :: status
        ! if(associated(AccumulatorSystemFailPtr)) call AccumulatorSystemFailPtr(status)
        !if(status == Clear_StatusType) print*,'On_AccumulatorSystemFail_Clear'
        !if(status == Executed_StatusType) print*,'On_AccumulatorSystemFail_Execute'
    endsubroutine
    
    subroutine ChangeAccumulatorSystemLeak(status)
        implicit none
        integer, intent (in) :: status
        ! if(associated(AccumulatorSystemLeakPtr)) call AccumulatorSystemLeakPtr(status)
        !if(status == Clear_StatusType) print*,'On_AccumulatorSystemLeak_Clear'
        !if(status == Executed_StatusType) print*,'On_AccumulatorSystemLeak_Execute'
    endsubroutine

end module CBopProblems