using IdGen;
using Microsoft.Extensions.Configuration;

namespace Atomx.Data.Services
{
    /// <summary>
    /// 
    /// </summary>
    public interface IIdCreatorService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        long NewId();
    }

    /// <summary>
    /// 
    /// </summary>
    public class IdCreatorService : IIdCreatorService
    {

        private IdGenerator IdCreator { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public IdCreatorService(IConfiguration configuration)
        {
            DateTime epoch = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var structure = new IdStructure(45, 2, 16);
            var options = new IdGeneratorOptions(structure, new DefaultTimeSource(epoch));
            var workId = int.Parse(configuration.GetSection("IdWorkId").Value ?? "0");
            IdCreator = new IdGenerator(workId, options);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public long NewId()
        {
            return IdCreator.CreateId();
        }
    }
}
