module MudPropertiesModule
    use SimulationVariables
    use json_module
    implicit none
    
    contains

    subroutine MudPropertiesToJson(parent)

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

        ! 1. create new node
        call json%create_object(p,'Mud')
        
        ! 2. add member of data type to new node
        call json%add(p,"ActiveMudType",data%Configuration%Mud%ActiveMudType)
        call json%add(p,"ActiveRheologyModel",data%Configuration%Mud%ActiveRheologyModel)
        call json%add(p,"ActiveMudVolume",data%Configuration%Mud%ActiveMudVolume)
        call json%add(p,"ActiveMudVolumeGal",data%Configuration%Mud%ActiveMudVolumeGal)
        call json%add(p,"ActiveDensity",data%Configuration%Mud%ActiveDensity)
        call json%add(p,"ActivePlasticViscosity",data%Configuration%Mud%ActivePlasticViscosity)
        call json%add(p,"ActiveYieldPoint",data%Configuration%Mud%ActiveYieldPoint)
        call json%add(p,"ActiveThetaThreeHundred",data%Configuration%Mud%ActiveThetaThreeHundred)
        call json%add(p,"ActiveThetaSixHundred",data%Configuration%Mud%ActiveThetaSixHundred)
        call json%add(p,"ReserveMudType",data%Configuration%Mud%ReserveMudType)
        call json%add(p,"ReserveMudVolume",data%Configuration%Mud%ReserveMudVolume)
        call json%add(p,"ReserveMudVolumeGal",data%Configuration%Mud%ReserveMudVolumeGal)
        call json%add(p,"ReserveDensity",data%Configuration%Mud%ReserveDensity)
        call json%add(p,"ReservePlasticViscosity",data%Configuration%Mud%ReservePlasticViscosity)
        call json%add(p,"ReserveYieldPoint",data%Configuration%Mud%ReserveYieldPoint)
        call json%add(p,"ReserveThetaThreeHundred",data%Configuration%Mud%ReserveThetaThreeHundred)
        call json%add(p,"ReserveThetaSixHundred",data%Configuration%Mud%ReserveThetaSixHundred)
        call json%add(p,"ActiveTotalTankCapacity",data%Configuration%Mud%ActiveTotalTankCapacity)
        call json%add(p,"ActiveTotalTankCapacityGal",data%Configuration%Mud%ActiveTotalTankCapacityGal)
        call json%add(p,"ActiveSettledContents",data%Configuration%Mud%ActiveSettledContents)
        call json%add(p,"ActiveSettledContentsGal",data%Configuration%Mud%ActiveSettledContentsGal)
        call json%add(p,"ActiveTotalContents",data%Configuration%Mud%ActiveTotalContents)
        call json%add(p,"ActiveTotalContentsGal",data%Configuration%Mud%ActiveTotalContentsGal)
        call json%add(p,"ActiveAutoDensity",data%Configuration%Mud%ActiveAutoDensity)
        call json%add(p,"InitialTripTankMudVolume",data%Configuration%Mud%InitialTripTankMudVolume)
        call json%add(p,"InitialTripTankMudVolumeGal",data%Configuration%Mud%InitialTripTankMudVolumeGal)
        call json%add(p,"PedalFlowMeter",data%Configuration%Mud%PedalFlowMeter)
        
        ! 3. add new node to parent
        call json%add(parent,p)
    end subroutine

    subroutine MudPropertiesFromJson(parent)
        use json_module,IK =>json_ik
        type(json_value),pointer :: parent
        type(json_core) :: json
        type(json_value),pointer :: p,pitems,pitem,pval,pbit

        call json%get(parent,'Mud',p)

        call json%get(p,"ActiveMudType",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveMudType)
        call json%get(p,"ActiveRheologyModel",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveRheologyModel)
        call json%get(p,"ActiveMudVolume",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveMudVolume)
        call json%get(p,"ActiveMudVolumeGal",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveMudVolumeGal)
        call json%get(p,"ActiveDensity",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveDensity)
        call json%get(p,"ActivePlasticViscosity",pitem)
		call json%get(pitem,data%Configuration%Mud%ActivePlasticViscosity)
        call json%get(p,"ActiveYieldPoint",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveYieldPoint)
        call json%get(p,"ActiveThetaThreeHundred",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveThetaThreeHundred)
        call json%get(p,"ActiveThetaSixHundred",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveThetaSixHundred)
        call json%get(p,"ReserveMudType",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveMudType)
        call json%get(p,"ReserveMudVolume",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveMudVolume)
        call json%get(p,"ReserveMudVolumeGal",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveMudVolumeGal)
        call json%get(p,"ReserveDensity",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveDensity)
        call json%get(p,"ReservePlasticViscosity",pitem)
		call json%get(pitem,data%Configuration%Mud%ReservePlasticViscosity)
        call json%get(p,"ReserveYieldPoint",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveYieldPoint)
        call json%get(p,"ReserveThetaThreeHundred",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveThetaThreeHundred)
        call json%get(p,"ReserveThetaSixHundred",pitem)
		call json%get(pitem,data%Configuration%Mud%ReserveThetaSixHundred)
        call json%get(p,"ActiveTotalTankCapacity",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveTotalTankCapacity)
        call json%get(p,"ActiveTotalTankCapacityGal",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveTotalTankCapacityGal)
        call json%get(p,"ActiveSettledContents",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveSettledContents)
        call json%get(p,"ActiveSettledContentsGal",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveSettledContentsGal)
        call json%get(p,"ActiveTotalContents",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveTotalContents)
        call json%get(p,"ActiveTotalContentsGal",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveTotalContentsGal)
        call json%get(p,"ActiveAutoDensity",pitem)
		call json%get(pitem,data%Configuration%Mud%ActiveAutoDensity)
        call json%get(p,"InitialTripTankMudVolume",pitem)
		call json%get(pitem,data%Configuration%Mud%InitialTripTankMudVolume)
        call json%get(p,"InitialTripTankMudVolumeGal",pitem)
		call json%get(pitem,data%Configuration%Mud%InitialTripTankMudVolumeGal)
        call json%get(p,"PedalFlowMeter",pitem)
		call json%get(pitem,data%Configuration%Mud%PedalFlowMeter)
			
    end subroutine
    
    subroutine Set_ActiveMudVolume_StudentStation(v)
        implicit none
        real*8, intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%Configuration%Mud%ActiveMudVolume == v) return
        #endif
        data%Configuration%Mud%ActiveMudVolume = v   
        ! if(associated(ActiveMudVolumePtr)) call ActiveMudVolumePtr(data%Configuration%Mud%ActiveMudVolume)
    end subroutine
        
    subroutine Set_ActiveDensity_StudentStation(v)
        implicit none
        real*8, intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%Configuration%Mud%ActiveDensity == v) return
        #endif
        data%Configuration%Mud%ActiveDensity = v    
        ! if(associated(ActiveDensityPtr)) call ActiveDensityPtr(data%Configuration%Mud%ActiveDensity)
    end subroutine
    
    subroutine Set_ReserveMudVolume_StudentStation(v)
        implicit none
        real*8, intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%Configuration%Mud%ReserveMudVolume == v) return
        #endif
        data%Configuration%Mud%ReserveMudVolume = v  
        ! if(associated(ReserveMudVolumePtr)) call ReserveMudVolumePtr(data%Configuration%Mud%ReserveMudVolume)
    end subroutine
        
    subroutine Set_ReserveDensity_StudentStation(v)
        implicit none
        real*8, intent(in) :: v
        #ifdef ExcludeExtraChanges
                if(data%Configuration%Mud%ReserveDensity == v) return
        #endif
        data%Configuration%Mud%ReserveDensity = v   
        ! if(associated(ReserveDensityPtr)) call ReserveDensityPtr(data%Configuration%Mud%ReserveDensity)
    end subroutine

end module MudPropertiesModule