' convert it with additional command line argument -DRELATIVE_INCLUDE="." to use locally
!if %variable_exists("RELATIVE_INCLUDE")
  !include %get_variable_value("RELATIVE_INCLUDE")/C4_Container.puml
!else
  !include https://raw.githubusercontent.com/kirchsth/C4-PlantUML/extended/C4_Container.puml
!endif

' Colors
' ##################################

!global $NODE_FONT_COLOR = "#000000"
!global $NODE_BG_COLOR = "#FFFFFF"
!global $NODE_BORDER_COLOR = "#A2A2A2"

' Styling
' ##################################

' orig was without background
'UpdateSkinparamsAndLegendEntry("node", $fontColor=$NODE_FONT_COLOR, $borderColor=$NODE_BORDER_COLOR)
UpdateSkinparamsAndLegendEntry("node", $bgColor=$NODE_BG_COLOR, $fontColor=$NODE_FONT_COLOR, $borderColor=$NODE_BORDER_COLOR)
skinparam rectangle<<node>> {
    FontStyle normal
}

' Layout
' ##################################

' comment if node should not be added to legend. No dynamic legend extension required
SetDefaultLegendEntries("person\nsystem\ncontainer\nexternal_person\nexternal_system\nexternal_container\nnode")

' Special
' ##################################

' PlantUML supports no automatic line breaks of "PlantUML containers" (C4 Deployment_Node is a "PlantUML container")
' therefore Deployment_Node() implements an automatic line break based on spaces (like in all other objects).
' If a $type contains \n then the these are used (and no automatic space based line breaks are done)
' $NODE_TYPE_MAX_CHAR_WIDTH defines the automatic line break position
!global $NODE_TYPE_MAX_CHAR_WIDTH = 35
!global $NODE_DESCR_MAX_CHAR_WIDTH=32

' Elements
' ##################################

!unquoted function $breakType($type, $widthStr)
!$width = %intval($widthStr)
!$multiLine = ""
!if (%strpos($type, "\n") >= 0)
  !while (%strpos($type, "\n") >= 0)
    !$brPos = %strpos($type, "\n")
    !$multiLine = $multiLine + %substr($type, 0, $brPos) + '</size>\n<size:'+$TECHN_FONT_SIZE+'>'
    !$type = %substr($type, $brPos+2)
  !endwhile
!else
  !while (%strlen($type)>$width)
    !$brPos = $width
    !while ($brPos>0 && %substr($type, $brPos, 1)!= ' ')
      !$brPos = $brPos - 1
    !endwhile

    !if ($brPos < 1)
      !$brPos = %strpos($type, " ")
    !else
    !endif

    !if ($brPos > 0)
      !$multiLine = $multiLine + %substr($type, 0, $brPos) + '</size>\n<size:'+$TECHN_FONT_SIZE+'>'
      !$type = %substr($type, $brPos + 1)
    !else
      !$multiLine = $multiLine+ $type
      !$type = ""
    !endif
  !endwhile
!endif
!if (%strlen($type)>0)
  !$multiLine = $multiLine + $type
!endif
!return $multiLine
!endfunction

!unquoted function $breakDescr($descr, $widthStr)
!$width = %intval($widthStr)
!$multiLine = ""
!if (%strpos($descr, "\n") >= 0)
!else
  !while (%strlen($descr)>$width)
    !$brPos = $width 
    !while ($brPos>0 && %substr($descr, $brPos, 1)!= ' ')
      !$brPos = $brPos - 1
    !endwhile

    !if ($brPos < 1)
      !$brPos = %strpos($descr, " ")
    !else
    !endif

    !if ($brPos > 0)
      !$multiLine = $multiLine + %substr($descr, 0, $brPos) + "\n"
      !$descr = %substr($descr, $brPos + 1)
    !else
      !$multiLine = $multiLine+ $descr
      !$descr = ""
    !endif
  !endwhile
!endif
!if (%strlen($descr)>0)
  !$multiLine = $multiLine + $descr
!endif
!return $multiLine
!endfunction

!function $getNode($label, $type, $descr, $sprite)
  !$nodeText = ""
  !if ($sprite != "")
    !$nodeText = $nodeText + '<$'+$sprite+'>\n'
  !endif
  !$nodeText = $nodeText + '==' + $label
  !if ($type != "")
    !$nodeText = $nodeText + '\n<size:' + $TECHN_FONT_SIZE + '>[' + $breakType($type, $NODE_TYPE_MAX_CHAR_WIDTH) + ']</size>'
  !endif
  !if ($descr != "")
    !$nodeText = $nodeText + '\n\n' + $breakDescr($descr, $NODE_DESCR_MAX_CHAR_WIDTH)
  !endif
  !return $nodeText 
!endfunction

!function $getNode_L($label, $type, $descr, $sprite)
  !$nodeText = ""
  !if ($sprite != "")
    !$nodeText = $nodeText + '<$'+$sprite+'>\l'
  !endif
  !$nodeText = $nodeText + '==' + $label
  !if ($type != "")
    !$nodeText = $nodeText + '\l<size:' + $TECHN_FONT_SIZE + '>[' + $breakType($type, $NODE_TYPE_MAX_CHAR_WIDTH) + ']</size>'
  !endif
  !if ($descr != "")
    !$nodeText = $nodeText + '\l\l' + $breakDescr($descr, $NODE_DESCR_MAX_CHAR_WIDTH)
  !endif
  !return $nodeText 
!endfunction

!function $getNode_R($label, $type, $descr, $sprite)
  !$nodeText = ""
  !if ($sprite != "")
    !$nodeText = $nodeText + '<$'+$sprite+'>\r'
  !endif
  !$nodeText = $nodeText + '==' + $label
  !if ($type != "")
    !$nodeText = $nodeText + '\r<size:' + $TECHN_FONT_SIZE + '>[' + $breakType($type, $NODE_TYPE_MAX_CHAR_WIDTH) + ']</size>'
  !endif
  !if ($descr != "")
    !$nodeText = $nodeText + '\r\r' + $breakDescr($descr, $NODE_DESCR_MAX_CHAR_WIDTH)
  !endif
  !return $nodeText 
!endfunction

!unquoted procedure Deployment_Node($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode($label, $type, $descr, $sprite)$getProps()" $toStereos("node",$tags) as $alias
!endprocedure

!unquoted procedure Deployment_Node_L($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode_L($label, $type, $descr, $sprite)$getProps_L()" $toStereos("node",$tags) as $alias
!endprocedure

!unquoted procedure Deployment_Node_R($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode_R($label, $type, $descr, $sprite)$getProps_R()" $toStereos("node",$tags) as $alias
!endprocedure

!unquoted procedure Node($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode($label, $type, $descr, $sprite)$getProps()" $toStereos("node",$tags) as $alias
!endprocedure

!unquoted procedure Node_L($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode_L($label, $type, $descr, $sprite)$getProps_L()" $toStereos("node",$tags) as $alias
!endprocedure

!unquoted procedure Node_R($alias, $label, $type="", $descr="", $sprite="", $tags="")
rectangle "$getNode_R($label, $type, $descr, $sprite)$getProps_R()" $toStereos("node",$tags) as $alias
!endprocedure
