// Copyright (c) 2023 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#pragma once
#include "label.hpp"

namespace Dawn {
  struct ChildInfo;

  struct ChildrenInfo {
    std::vector<struct ChildInfo> children;
  };

  struct ChildInfo {
    enum ChildType type;
    struct ChildrenInfo children;
    std::string ref;
    bool_t hasRef = false;
    struct LabelInfo label;
  };

  class ChildrenParser : public XmlParser<struct ChildrenInfo> {
    protected:
      std::vector<std::string> getRequiredAttributes() {
        return std::vector<std::string>();
      }

      std::map<std::string, std::string> getOptionalAttributes() {
        return std::map<std::string, std::string>();
      }

      int32_t onParse(
        Xml *node,
        std::map<std::string, std::string> values,
        struct ChildrenInfo *out,
        std::string *error
      ) {
        // Parse children of self.
        int32_t ret = 0;
        auto itChildren = node->children.begin();
        while(itChildren != node->children.end()) {
          auto c = *itChildren;
          struct ChildInfo child;

          if(c->node == "label") {
            child.type = CHILD_TYPE_LABEL;

            ret = (LabelParser()).parse(c, &child.label, error);
          } else {
            *error = "Unrecognized UI Element " + c->node;
            return 1;
          }
          if(ret != 0) return ret;

          // Now Parse children of children
          ret = (ChildrenParser()).parse(c, &child.children, error);
          if(ret != 0) return ret;
          
          ++itChildren;
        }

        return ret;
      }
  };
}