// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License

namespace Azure.Iot.Operations.Opc2WotLib
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public partial class WotDataSchemaObject : WotDataSchema
    {
        private string? description;
        private string? schemaName;
        private string typeRef;
        private Dictionary<string, WotDataSchema> fieldDataSchemas;
        private List<string> requiredFieldNames;

        public WotDataSchemaObject(OpcUaNode containingNode, string? description, string? schemaName, Dictionary<string, OpcUaObjectField> fields, IEnumerable<OpcUaNodeId> ancestors, bool isUnion)
        {
            this.description = description;
            this.schemaName = schemaName;
            this.typeRef = containingNode.GetTypeRef();

            fieldDataSchemas =  fields.ToDictionary(
                field => field.Key,
                field => WotDataSchema.Create(field.Value.DataType, field.Value.ValueRank, field.Value.ContainingNode, field.Value.Description, ancestors));

            requiredFieldNames = isUnion ?
                new List<string>() :
                fields.Where(field => !field.Value.IsOptional).Select(field => field.Key).ToList();
        }
    }
}
