// 
using System;
using System.Threading;
using Alachisoft.NosDB.Common.Configuration.Services;
using Alachisoft.NosDB.Common.Net;
using Alachisoft.NosDB.Common.Stats;
using Alachisoft.NosDB.Common.Threading;
using Alachisoft.NosDB.Common.Toplogies.Impl.Interfaces;
using Alachisoft.NosDB.Core.Configuration.Services;
using Alachisoft.NosDB.Core.Toplogies.Impl;

namespace Alachisoft.NosDB.Core.Toplogies
{

    public class NodeContext
    {
        internal Latch _statusLatch = new Latch();
        private long _electionBasedSequenceId;
        private ElectionResult _electionResult;
        private readonly object _electionLock = new object();


        public String ClusterName { get; set; }

        public IDatabaseTopology TopologyImpl { get; set; }
        public Address LocalAddress{get;set;}
        public String LocalShardName { get; set; }
        public IShardServer ShardServer { get; set; }
        public String BasePath { get; set; }
        public String DataPath { get; set; }
        public String DeploymentPath { get; set; }

        public IConfigurationSession ConfigurationSession { get; set; }

        public IDatabasesManager DatabasesManager { get; set; }

        public IStatsCollector ShardStatsCollector { get; set; }

        public Security.Interfaces.ISecurityManager SecurityManager { set; get; }
        /// <summary>
        /// Status Latch for node level status, wether it is running,initializing or stopped.
        /// </summary>
        public Latch StatusLatch 
        {
            get
            {
                return _statusLatch;
            }
        }

        public ElectionResult ElectionResult
        {
            get
            {
                lock (_electionLock)
                {
                    return _electionResult;
                }
            }
            set
            {
                lock (_electionLock)
                {
                    _electionResult = value;
                    _electionBasedSequenceId = -1;
                }
            }
        }

        public long ElectionBasedSequenceId
        {
            get { return Interlocked.Increment(ref _electionBasedSequenceId); }
        }


        
    }
}
