using Opc.Ua;
using System;
using System.Collections.Generic;
using System.Text;
using Opc.Ua.Client;

namespace console_opcua_sgdn_tornio.Controllers
{
    public enum OperationState
    {
        Idle,
        Ready,
        Setup,
        Machining,
        Completed,
        Aborted,
        Error
    }

    public class OperationContext
    {
        public int OperationId { get; set; }
        public OperationState State { get; set; } = OperationState.Idle;

        public DateTime? StartUtc { get; set; }
        public DateTime? MachiningStartUtc { get; set; }
        public DateTime? EndUtc { get; set; }

        public string ProgramName { get; set; }
    }

    public static class OpcUaOperation
    {
        public static void Browse(Session session, NodeId nodeId, int level = 0)
        {
            ReferenceDescriptionCollection refs;
            byte[] cp;

            session.Browse(
                null,
                null,
                nodeId,
                0,
                BrowseDirection.Forward,
                ReferenceTypeIds.HierarchicalReferences,
                true,
                (uint)(NodeClass.Object | NodeClass.Variable),
                out cp,
                out refs);

            foreach (var r in refs)
            {
                var nid = ExpandedNodeId.ToNodeId(r.NodeId, session.NamespaceUris);
                Console.WriteLine($"{new string(' ', level * 2)}- {r.DisplayName.Text} | {nid}");

                if (nid != null)
                    Browse(session, nid, level + 1);
            }
        }
    }


}
