﻿// Node.cs
// Node implementation for GraphsLib.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


namespace GraphsLib
{
    public class Node
    {
        public Node(NodeLink[] neighbors, long index, string label)
        {
            foreach (NodeLink nl in neighbors)
            {
                nl.from = this;
            }

            mNeighbors = neighbors;
            mVisited = false;
            mIndex = index;
            mLabel = label;
        }

        public Node()
        {
            mNeighbors = new NodeLink[0];
            mVisited = false;
            mIndex = -1;
            mLabel = "";
        }

        public Node(string label)
        {
            mNeighbors = new NodeLink[0];
            mVisited = false;
            mIndex = -1;
            mLabel = label;
        }

        public Node(Node n)
        {
            mNeighbors = n.neighbors;
            mVisited = n.visited;
            mIndex = n.index;
            mLabel = n.label;
        }

        public NodeLink[] neighbors
        {
            get { return mNeighbors; }
            set { mNeighbors = value; }
        }

        public bool visited
        {
            get { return mVisited; }
            set { mVisited = value; }
        }

        public long index
        {
            get { return mIndex; }
            set { mIndex = value; }
        }

        public string label
        {
            get { return mLabel; }
            set { mLabel = value; }
        }

        public static bool operator ==(Node a, Node b)
        {
            if ((object)a == null)
            {
                if ((object)b == null)
                {
                    return true;
                }
                return false;
            }

            if ((object)b == null)
            {
                if ((object)a == null)
                {
                    return true;
                }
                return false;
            }

            return (a.index == b.index);
        }

        public static bool operator !=(Node a, Node b)
        {
            if ((object)a == null)
            {
                if ((object)b == null)
                {
                    return false;
                }
                return true;
            }

            if ((object)b == null)
            {
                if ((object)a == null)
                {
                    return false;
                }
                return true;
            }

            return !(a.index == b.index);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override string ToString()
        {
            long lastNeighbor = mNeighbors.LongLength - 1;
            string s = mLabel + ": ";
            foreach (NodeLink n in mNeighbors)
            {
                s += n.to.label.ToString() + " (" + n.weight;
                if (n != mNeighbors[lastNeighbor])
                {
                    s += "), ";
                }
            }
            return s;
        }

        private NodeLink[] mNeighbors;
        private bool mVisited;
        private long mIndex;
        private string mLabel;
    }
}
