module CRotaryProblems
    use SimulationVariables
	implicit none    
	public 
    contains
    subroutine RotaryProblemsToJson(parent)

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

        ! 1. create new node
        call json%create_object(p,'RotaryProblems')
        
        ! 2. add member of data type to new node
        call ProblemToJson(p,"MotorFail",data%problems%RotaryProblems%MotorFail)
        call ProblemToJson(p,"OverideTorqueLimit",data%problems%RotaryProblems%OverideTorqueLimit)

        ! 3. add new node to parent
        call json%add(parent,p)
    end subroutine

    subroutine ProcessRotaryProblemsDueTime(time)
        implicit none
        integer :: time
        
        if(data%problems%RotaryProblems%MotorFail%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%RotaryProblems%MotorFail, ChangeMotorFail, time)
        if(data%problems%RotaryProblems%OverideTorqueLimit%ProblemType == Time_ProblemType) call ProcessDueTime(data%problems%RotaryProblems%OverideTorqueLimit, ChangeOverideTorqueLimit, time)
        
    end subroutine
    
    subroutine ProcessRotaryProblemsDuePumpStrokes(strokes)
        implicit none
        integer :: strokes
        
        if(data%problems%RotaryProblems%MotorFail%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%RotaryProblems%MotorFail, ChangeMotorFail, strokes)
        if(data%problems%RotaryProblems%OverideTorqueLimit%ProblemType == PumpStrokes_ProblemType) call ProcessDuePumpStrokes(data%problems%RotaryProblems%OverideTorqueLimit, ChangeOverideTorqueLimit, strokes)
        
    end subroutine
    
    subroutine ProcessRotaryProblemsDueVolumePumped(volume)
        implicit none
        real(8) :: volume
        
        if(data%problems%RotaryProblems%MotorFail%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%RotaryProblems%MotorFail, ChangeMotorFail, volume)
        if(data%problems%RotaryProblems%OverideTorqueLimit%ProblemType == VolumePumped_ProblemType) call ProcessDueVolumePumped(data%problems%RotaryProblems%OverideTorqueLimit, ChangeOverideTorqueLimit, volume)
        
    end subroutine
    
    subroutine ProcessRotaryProblemsDueDistanceDrilled(distance)
        implicit none
        real(8) :: distance
        
        if(data%problems%RotaryProblems%MotorFail%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%RotaryProblems%MotorFail, ChangeMotorFail, distance)
        if(data%problems%RotaryProblems%OverideTorqueLimit%ProblemType == DistanceDrilled_ProblemType) call ProcessDueDistanceDrilled(data%problems%RotaryProblems%OverideTorqueLimit, ChangeOverideTorqueLimit, distance)
        
    end subroutine
    
    
    
    
    
    
    
    subroutine ChangeMotorFail(status)
        use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        !if(associated(MotorFailPtr)) call MotorFailPtr(status)
        if(status == Clear_StatusType)        data%State%RTable%MotorFaileMalf=0
        if(status == Executed_StatusType)     data%State%RTable%MotorFaileMalf=1
    endsubroutine
    
    subroutine ChangeOverideTorqueLimit(status)
        use SimulationVariables !@
        implicit none
        integer, intent (in) :: status
        !if(associated(OverideTorqueLimitPtr)) call OverideTorqueLimitPtr(status)
        if(status == Clear_StatusType)        data%State%RTable%OverideTorqueLimitMalf=0
        if(status == Executed_StatusType)     data%State%RTable%OverideTorqueLimitMalf=1
    endsubroutine

end module CRotaryProblems