{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://foundryapparatus.affonso.dev/schemas/v1/gui.schema.json",
  "title": "Foundry Apparatus v1 - GUI layout",
  "description": "Reusable GUI layout (foundry_apparatus/guis/). Widget types are an open vocabulary validated against registered widget descriptors.",
  "type": "object",
  "allOf": [
    {
      "$ref": "common.schema.json#/$defs/definition_header"
    },
    {
      "$ref": "#/$defs/layout"
    }
  ],
  "unevaluatedProperties": false,
  "$defs": {
    "layout": {
      "type": "object",
      "required": [
        "width",
        "height",
        "widgets"
      ],
      "properties": {
        "width": {
          "type": "integer",
          "minimum": 1
        },
        "height": {
          "type": "integer",
          "minimum": 1
        },
        "background_texture": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "theme": {
          "description": "Theme id (namespace:name). Each element is drawn with the GUI sprite <namespace>:<name>/<element> when the pack has it, else with foundry_apparatus:default/<element>.",
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "widgets": {
          "type": "array",
          "maxItems": 256,
          "items": {
            "$ref": "#/$defs/widget"
          }
        },
        "title": {
          "description": "Where the machine's name is drawn: 'center' (default), 'left', 'right', 'hidden', or an exact position.",
          "oneOf": [
            {
              "enum": [
                "left",
                "center",
                "right",
                "hidden"
              ]
            },
            {
              "type": "object",
              "properties": {
                "align": {
                  "enum": [
                    "left",
                    "center",
                    "right",
                    "hidden"
                  ],
                  "default": "center"
                },
                "x": {
                  "type": "integer"
                },
                "y": {
                  "type": "integer"
                }
              },
              "additionalProperties": false
            }
          ]
        },
        "auto_side_config": {
          "description": "Add the standard side configuration tab when the machine allows player side configuration and this layout places no side configuration widget itself.",
          "type": "boolean",
          "default": true
        },
        "side_config_group": {
          "description": "How the side configuration groups the machine's storages. 'type' (default): one tab per resource type, and each face picks which storage of that type it exposes (like Mekanism). 'storage': one tab per storage, each face picks input, output, both or blocked (like Industrial Foregoing). 'machine': one tab, each face picks input, output, both or blocked for every storage at once (like Thermal).",
          "enum": [
            "type",
            "storage",
            "machine"
          ],
          "default": "type"
        },
        "storage_styles": {
          "description": "Display name and colour of each storage in this GUI (slots, tanks, buttons, side faces); reloadable.",
          "type": "object",
          "propertyNames": {
            "$ref": "common.schema.json#/$defs/local_name"
          },
          "additionalProperties": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "color": {
                "type": "string",
                "pattern": "^#[0-9a-fA-F]{6}$"
              }
            },
            "additionalProperties": false
          }
        },
        "panels": {
          "description": "Extra panels beside, over or attached to the main panel, always visible or toggled by buttons.",
          "type": "array",
          "maxItems": 32,
          "items": {
            "$ref": "#/$defs/panel"
          }
        },
        "colors": {
          "$ref": "#/$defs/colors"
        }
      }
    },
    "inline_layout": {
      "description": "A layout embedded in a machine definition.",
      "type": "object",
      "allOf": [
        {
          "$ref": "#/$defs/layout"
        }
      ],
      "unevaluatedProperties": false
    },
    "widget": {
      "type": "object",
      "required": [
        "id",
        "type",
        "x",
        "y",
        "width",
        "height"
      ],
      "properties": {
        "id": {
          "$ref": "common.schema.json#/$defs/local_name"
        },
        "type": {
          "$ref": "common.schema.json#/$defs/vocabulary_type"
        },
        "x": {
          "type": "integer",
          "minimum": 0
        },
        "y": {
          "type": "integer",
          "minimum": 0
        },
        "width": {
          "type": "integer",
          "minimum": 1
        },
        "height": {
          "type": "integer",
          "minimum": 1
        },
        "target_binding": {
          "description": "Storage or variable of the machine this widget displays or controls.",
          "$ref": "common.schema.json#/$defs/local_name"
        },
        "params": {
          "type": "object"
        },
        "style": {
          "$ref": "#/$defs/style",
          "description": "How the widget looks; options its type does not use are rejected.",
          "properties": {
            "frame": false,
            "depth": false
          }
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "enum": [
                  "foundry_apparatus:panel_button",
                  "foundry_apparatus:side_config_button"
                ]
              }
            }
          },
          "else": {
            "properties": {
              "style": {
                "properties": {
                  "variant": false,
                  "hover_sprite": false,
                  "selected_sprite": false
                }
              }
            }
          }
        },
        {
          "if": {
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "enum": [
                  "foundry_apparatus:item_slot",
                  "foundry_apparatus:resource_gauge"
                ]
              }
            }
          },
          "else": {
            "properties": {
              "style": {
                "properties": {
                  "highlight": false
                }
              }
            }
          }
        },
        {
          "if": {
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "enum": [
                  "foundry_apparatus:resource_gauge",
                  "foundry_apparatus:progress_bar"
                ]
              }
            }
          },
          "else": {
            "properties": {
              "style": {
                "properties": {
                  "fill_sprite": false
                }
              }
            }
          }
        },
        {
          "if": {
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "foundry_apparatus:resource_gauge"
              }
            }
          },
          "else": {
            "properties": {
              "style": {
                "properties": {
                  "marks": false
                }
              }
            }
          }
        }
      ]
    },
    "panel": {
      "type": "object",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "allOf": [
            {
              "$ref": "common.schema.json#/$defs/local_name"
            },
            {
              "not": {
                "const": "main"
              }
            }
          ]
        },
        "show": {
          "enum": [
            "always",
            "toggle"
          ],
          "default": "toggle"
        },
        "placement": {
          "description": "Easy placement: beside the main panel, over the player inventory, or over the machine area above it.",
          "enum": [
            "left",
            "right",
            "inventory",
            "content"
          ]
        },
        "anchor": {
          "description": "Advanced placement: attached to a side of the main panel or another panel (or inside it), moved by x/y and aligned.",
          "type": "object",
          "required": [
            "side"
          ],
          "properties": {
            "to": {
              "type": "string",
              "default": "main"
            },
            "side": {
              "enum": [
                "left",
                "right",
                "top",
                "bottom",
                "inside"
              ]
            },
            "x": {
              "type": "integer"
            },
            "y": {
              "type": "integer"
            },
            "align": {
              "enum": [
                "start",
                "center",
                "end"
              ],
              "default": "start"
            }
          },
          "additionalProperties": false
        },
        "width": {
          "type": "integer",
          "minimum": 1
        },
        "height": {
          "type": "integer",
          "minimum": 1
        },
        "background_texture": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "widgets": {
          "type": "array",
          "maxItems": 256,
          "items": {
            "$ref": "#/$defs/widget"
          }
        },
        "style": {
          "$ref": "#/$defs/style",
          "description": "How the panel looks: 'frame' and 'sprite' only.",
          "properties": {
            "highlight": false,
            "hover_sprite": false,
            "selected_sprite": false,
            "fill_sprite": false,
            "variant": false,
            "marks": false
          }
        }
      },
      "oneOf": [
        {
          "required": [
            "placement"
          ],
          "not": {
            "required": [
              "anchor"
            ]
          }
        },
        {
          "required": [
            "anchor"
          ],
          "not": {
            "required": [
              "placement"
            ]
          }
        }
      ],
      "allOf": [
        {
          "if": {
            "required": [
              "placement"
            ],
            "properties": {
              "placement": {
                "enum": [
                  "inventory",
                  "content"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "width": false,
              "height": false
            }
          },
          "else": {
            "required": [
              "width",
              "height"
            ]
          }
        }
      ],
      "additionalProperties": false
    },
    "style": {
      "description": "How a widget or panel looks, on top of the layout's theme. Every field is optional; a field the widget type does not use is reported (FA-SCH-004).",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "highlight": {
          "description": "item_slot and resource_gauge: show the storage's colour as an outline (default), a tint over the inside, or not at all.",
          "enum": [
            "outline",
            "tint",
            "none"
          ]
        },
        "sprite": {
          "description": "GUI sprite id (assets/<ns>/textures/gui/sprites/<path>.png) for the widget's frame or base, or the panel's background. Replaces the theme's sprite.",
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "hover_sprite": {
          "description": "panel_button and side_config_button: sprite while the mouse is over the button.",
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "selected_sprite": {
          "description": "panel_button and side_config_button: sprite while the button's panel is open.",
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "fill_sprite": {
          "description": "resource_gauge and progress_bar: sprite for the filled part.",
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "variant": {
          "description": "panel_button and side_config_button: a plain button (default) or a tab attached to the edge of the panel it sits on.",
          "enum": [
            "button",
            "tab"
          ]
        },
        "frame": {
          "description": "Panels only: draw the panel background (default) or nothing, for panels that only hold buttons or tabs.",
          "enum": [
            "panel",
            "none"
          ]
        },
        "marks": {
          "description": "resource_gauge: how many level marks to draw along the long side, 0 for none (default 3, at the quarters).",
          "type": "integer",
          "minimum": 0,
          "maximum": 16
        },
        "depth": {
          "description": "Panels only: drawn over the main panel (default) or under it, so the main panel's edge covers where they meet (tabs that come out from under the screen).",
          "enum": [
            "above",
            "below"
          ]
        }
      }
    },
    "colors": {
      "description": "Colours of this GUI, over its theme's (assets/<ns>/gui_themes/<name>.json). Every key is optional.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "storage_palette": {
          "description": "Colours given in order to storages without their own colour (energy has its own).",
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "items": {
            "type": "string",
            "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
          }
        },
        "energy": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Colour of energy storages without their own colour."
        },
        "blocked_face": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Colour of a blocked face in the side configuration."
        },
        "title": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Colour of the title and the inventory label."
        },
        "focus_outline": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Outline around the slots of the storage being pointed at."
        },
        "focus_fill": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Overlay on the slots of the storage being pointed at (use #AARRGGBB for transparency)."
        },
        "progress_fill": {
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$",
          "description": "Fill of progress bars that are not the arrow."
        },
        "tint_opacity": {
          "description": "How strong 'highlight: tint' colours a slot, from 0 to 1.",
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "status": {
          "description": "Status indicator colour per machine state.",
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "running": {
              "type": "string",
              "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
            },
            "paused": {
              "type": "string",
              "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
            },
            "errored": {
              "type": "string",
              "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
            },
            "protected": {
              "type": "string",
              "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
            },
            "idle": {
              "type": "string",
              "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
            }
          }
        },
        "side_tab": {
          "description": "Color of the side configuration tab when side_config_group is 'machine'.",
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
        },
        "mode_in": {
          "description": "Face color for input when side_config_group is 'machine'.",
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
        },
        "mode_out": {
          "description": "Face color for output when side_config_group is 'machine'.",
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
        },
        "mode_both": {
          "description": "Face color for input and output when side_config_group is 'machine'.",
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
        },
        "mixed_face": {
          "description": "Face color when a face exposes every storage of a type (side_config_group 'type').",
          "type": "string",
          "pattern": "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
        }
      }
    }
  }
}
