@startuml
'-----------Functions--------------------------------------------
!function $getGTcircuitImage($circuitNo=0, $circuitType="GT")
   'GregTech programmed circuit
   !$gtCircuit = "<img:../../lib/resources/gregtech/textures/items/gt.integrated_circuit/upscale/"+$circuitNo+".png>"
   'GT++ programmed bio circuit
   !$gtppBioCircuit = "<img:../../lib/resources/miscutils/textures/items/bioscience/upscale/BioCircuit.png>"
   'GT++ programmed breakthrough circuit
   !$gtppBreakthroughCircuit = "<img:../../lib/resources/miscutils/textures/items/science/general/upscale/AdvancedCircuit.png>"
   !$result = ""

   !if ($circuitNo > 0)
        !$result = $gtCircuit
        !if ($circuitType == "bio")
            !$result = $result + $gtppBioCircuit
        !endif
        !if ($circuitType == "breakthrough")
            !$result = $result + $gtppBreakthroughCircuit
        !endif
   !endif
   !return $result
!endfunction

'returns string witn GT++ Chemical Plant tier description
!function $getChemPlantTier($tier=0)
    !$result = ""
        !if ($tier >= 0)
            !$result = "Tier: " + $tier + ";\nMachine Casings: "
            !$tierMaterial = "unsupported"
            !if ($tier == 0)
                !$tierMaterial = "Strong Bronze"
            !elseif ($tier == 1)
                !$tierMaterial = "Solid Steel"
            !elseif ($tier == 2)
                !$tierMaterial = "Sturdy Alliminium"
            !elseif ($tier == 3)
                !$tierMaterial = "Clean Stainless Steel"
            !elseif ($tier == 4)
                !$tierMaterial = "Stable Titanium"
            !elseif ($tier == 5)
                !$tierMaterial = "Robust Tungstensteel"
            !elseif ($tier == 6)
                !$tierMaterial = "Vigorous Laurenium"
            !elseif ($tier == 7)
                !$tierMaterial = "Rugged Botmium"
            !endif
            !$result = $result + $tierMaterial
        !endif
    !return %string($result)
!endfunction

'-----------Procedures-------------------------------------------

' Draws machines and their inputs/outputs using array from .json file.
' Connections are drawn by $drawConnections function.
' All connections are ordered and depends from their actual position in array of .json file.
' This leads to:
'   1. New machines MUST BE added to the end of the array in .json file,
'      otherwise existing connections will be broken.
'   2. You cant rearrange/add/remove existing machines or their inputs/outputs,
'      otherwise existing connections will be broken.
!procedure $drawMachines($jsonFileName)
    !$fullMachineName = $machineName+$machineNumber
    !$machines = %load_json($jsonFileName)
    !$machineNumber = 0
    !$inputNumber = 0
    !$outputNumber = 0
    !$HVLCRCount = 0
    !$EVLCRCount = 0
    !$IVLCRCount = 0
    !$HVEBFCount = 0
    !$EVEBFCount = 0
    !$HVElectroCount = 0
    !$EVElectroCount = 0
    !$HVDTCount = 0
    !$IVDTCount = 0
    !$HVSifterCount = 0
    !$EVSifterCount = 0
    !$HVFluidSolidifierCount = 0
    !$HVMaceratorCount = 0
    !$HVMixerCount = 0
    !$HVOilCrackerCount = 0
    !$HVFluidExtractorCount = 0
    !$HVCentrifugeCount = 0
    !$EVCentrifugeCount = 0
    !$HVChemicalBathCount = 0
    !$HVDigesterCount = 0
    !$EVDigesterCount = 0
    !$HVDissolutionTankCount = 0
    !$HVVacuumFreezerCount = 0
    !$EVElectromagneticSeparatorCount = 0
    !$HVChemicalPlantCount = 0
    !$HVDehydratorCount = 0
    !$EVDistillationTowerCount = 0
    !$EVNeutronActivatorCount = 0
    !$EVAutoclaveCount = 0
    !$TotalMachinesCount = 0
    !foreach $machine in $machines
        !$machineNumber = $machineNumber + 1
        !$TotalMachinesCount = $TotalMachinesCount + 1
        !$fullMachineName = $machine.name + $machineNumber
        !$tier = $machine.tier
        !$chemPlantTier = $machine.chemPlantTier
        !$circuit = $machine.circuit
        !$circuitType = $machine.circuitType
        !$rectangleName = %string("rec"+$fullMachineName)
        !$countMachineName = $tier + " " + $machine.name
        !if ($countMachineName == "HV LCR")
            !$HVLCRCount = $HVLCRCount + 1
        !elseif ($countMachineName == "EV LCR")
            !$EVLCRCount = $EVLCRCount + 1
        !elseif ($countMachineName == "IV LCR")
            !$IVLCRCount = $IVLCRCount + 1
        !elseif ($countMachineName == "HV EBF")
            !$HVEBFCount = $HVEBFCount + 1
        !elseif ($countMachineName == "EV EBF")
            !$EVEBFCount = $EVEBFCount + 1
        !elseif ($countMachineName == "HV Electrolyzer")
            !$HVElectroCount = $HVElectroCount + 1
        !elseif ($countMachineName == "EV Electrolyzer")
            !$EVElectroCount = $EVElectroCount + 1
        !elseif ($countMachineName == "HV DT")
            !$HVDTCount = $HVDTCount + 1
        !elseif ($countMachineName == "IV DT")
            !$IVDTCount = $IVDTCount + 1
        !elseif ($countMachineName == "HV Sifter")
            !$HVSifterCount = $HVSifterCount + 1
        !elseif ($countMachineName == "EV Sifter")
            !$EVSifterCount = $EVSifterCount + 1
        !elseif ($countMachineName == "HV FluidSolidifier")
            !$HVFluidSolidifierCount = $HVFluidSolidifierCount + 1
        !elseif ($countMachineName == "HV Macerator")
            !$HVMaceratorCount = $HVMaceratorCount + 1
        !elseif ($countMachineName == "HV Mixer")
            !$HVMixerCount = $HVMixerCount + 1
        !elseif ($countMachineName == "HV OilCracker")
            !$HVOilCrackerCount = $HVOilCrackerCount + 1
        !elseif ($countMachineName == "HV FluidExtractor")
            !$HVFluidExtractorCount = $HVFluidExtractorCount + 1
        !elseif ($countMachineName == "HV Centrifuge")
            !$HVCentrifugeCount = $HVCentrifugeCount + 1
        !elseif ($countMachineName == "EV Centrifuge")
            !$EVCentrifugeCount = $EVCentrifugeCount + 1
        !elseif ($countMachineName == "HV ChemicalBath")
            !$HVChemicalBathCount = $HVChemicalBathCount + 1
        !elseif ($countMachineName == "HV Digester")
            !$HVDigesterCount = $HVDigesterCount + 1
        !elseif ($countMachineName == "EV Digester")
            !$EVDigesterCount = $EVDigesterCount + 1
        !elseif ($countMachineName == "HV DissolutionTank")
            !$HVDissolutionTankCount = $HVDissolutionTankCount + 1
        !elseif ($countMachineName == "HV VacuumFreezer")
            !$HVVacuumFreezerCount = $HVVacuumFreezerCount + 1
        !elseif ($countMachineName == "EV ElectromagneticSeparator")
            !$EVElectromagneticSeparatorCount = $EVElectromagneticSeparatorCount + 1
        !elseif ($countMachineName == "HV ChemicalPlant")
            !$HVChemicalPlantCount = $HVChemicalPlantCount + 1
        !elseif ($countMachineName == "HV Dehydrator")
            !$HVDehydratorCount = $HVDehydratorCount + 1
        !elseif ($countMachineName == "EV DistillationTower")
            !$EVDistillationTowerCount = $EVDistillationTowerCount + 1
        !elseif ($countMachineName == "EV NeutronActivator")
            !$EVNeutronActivatorCount = $EVNeutronActivatorCount + 1
        !elseif ($countMachineName == "EV Autoclave")
            !$EVAutoclaveCount = $EVAutoclaveCount + 1
        !endif
        !$rectangleVisibleName = $machine.name
        !if ($circuit > 0)
            !$rectangleVisibleName = $rectangleVisibleName + "\n" + $getGTcircuitImage($circuit, $circuitType)
        !endif
        !if ($chemPlantTier >= 0)
            !$rectangleVisibleName = $rectangleVisibleName + "\n" + $getChemPlantTier($chemPlantTier)
        !endif
        rectangle " " as $fullMachineName {
            rectangle "$rectangleVisibleName" <<$tier>>  as $rectangleName  {
            }
            !foreach $input in $machine.inputs
                !$inputNumber = $inputNumber + 1
                !$portInName = %string("pi"+$inputNumber)
                !if ($DEBUG)
                    portin "$portInName" as $portInName
                !else
                    portin " " as $portInName
                !endif
                $portInName --> $rectangleName: $input.name\n$input.amount
            !endfor

                !foreach $output in $machine.outputs
                    !$outputNumber = $outputNumber + 1
                    !$portOutName = %string("po"+$outputNumber)
                    !if ($DEBUG)
                        portout "$portOutName" as $portOutName
                    !else
                        portout " " as $portOutName
                    !endif
                    $rectangleName #----> $portOutName: $output.name\n$output.amount
                !endfor
          }
        !endfor
    rectangle machines [
        machines
        --
        | machine name              |     count     |
        !if ($HVLCRCount>0)
        |   HV LCR                  |   $HVLCRCount |
        !endif
        !if ($EVLCRCount>0)
        |   EV LCR                  |   $EVLCRCount|
        !endif
        !if ($IVLCRCount>0)
        |   IV LCR                  |   $IVLCRCount|
        !endif
        !if ($HVEBFCount>0)
        |   HV EBF                  |   $HVEBFCount|
        !endif
        !if ($EVEBFCount>0)
        |   EV EBF                  |   $EVEBFCount|
        !endif
        !if ($HVElectroCount>0)
        |   HV Electrolyzer         |   $HVElectroCount|
        !endif
        !if ($EVElectroCount>0)
        |   EV Electrolyzer         |   $EVElectroCount|
        !endif
        !if ($HVDTCount>0)
        |   HV DT                   |   $HVDTCount|
        !endif
        !if ($IVDTCount>0)
        |   IV DT                   |   $IVDTCount|
        !endif
        !if ($HVSifterCount>0)
        |   HV Sifter               |   $HVSifterCount|
        !endif
        !if ($EVSifterCount>0)
        |   EV Sifter               |   $EVSifterCount|
        !endif
        !if ($HVFluidSolidifierCount>0)
        |   HV Fluid Solidifier     |   $HVFluidSolidifierCount|
        !endif
        !if ($HVMaceratorCount>0)
        |   HV Macerator            |   $HVMaceratorCount|
        !endif
        !if ($HVMixerCount>0)
        |   HV Mixer                |   $HVMixerCount|
        !endif
        !if ($HVOilCrackerCount>0)
        |   HV Oil Cracker          |   $HVOilCrackerCount|
        !endif
        !if ($HVFluidExtractorCount>0)
        |   HV Fluid Extractor      |   $HVFluidExtractorCount|
        !endif
        !if ($HVCentrifugeCount>0)
        |   HV Centrifuge      |   $HVCentrifugeCount|
        !endif
        !if ($EVCentrifugeCount>0)
        |   EV Centrifuge      |   $EVCentrifugeCount|
        !endif
        !if ($HVChemicalBathCount>0)
        |   HV Chemical Bath      |   $HVChemicalBathCount|
        !endif
        !if ($HVDigesterCount>0)
        |   HV Digester      |   $HVDigesterCount|
        !endif
        !if ($EVDigesterCount>0)
        |   EV Digester      |   $EVDigesterCount|
        !endif
        !if ($HVDissolutionTankCount>0)
        |   HV Dissolution Tank      |   $HVDissolutionTankCount|
        !endif
        !if ($HVVacuumFreezerCount>0)
        |   HV Vacuum Freezer      |   $HVVacuumFreezerCount|
        !endif
        !if ($EVElectromagneticSeparatorCount>0)
        |   EV Electromagnetic Separator      |   $EVElectromagneticSeparatorCount|
        !endif
        !if ($HVChemicalPlantCount>0)
        |   HV Chemical Plant      |   $HVChemicalPlantCount|
        !endif
        !if ($HVDehydratorCount>0)
        |   HV Dehydrator      |   $HVDehydratorCount|
        !endif
        !if ($EVDistillationTowerCount>0)
        |   EV Distillation Tower      |   $EVDistillationTowerCount|
        !endif
        !if ($EVNeutronActivatorCount>0)
        |   EV Neutron Activator      |   $EVNeutronActivatorCount|
        !endif
        !if ($EVAutoclaveCount>0)
        |   EV Autoclave      |   $EVAutoclaveCount|
        !endif
        |   **total:** | ** $TotalMachinesCount **|
    ]
!endprocedure
@enduml