using Quadarax.Foundation.Core.Business.Processor.Enums;
using Quadarax.Foundation.Core.Business.Processor.Task;
// ReSharper disable InconsistentNaming


namespace Quadarax.Foundation.Core.Business.Processor.Exceptons
{
    public sealed class InvalidTaskStateException : Exception
    {
        #region *** Constants ***
        private const string CS_EXCC_ENT_TYPE = "EntityType";
        private const string CS_EXCC_ENT_ID = "EntityIdentifier";
        private const string CS_EXCC_ENT_CURRENT_STA = "CurrentStatus";
        private const string CS_EXCC_ENT_NEW_STA = "NewStatus";
        private const string CS_EXCC_ENT_EXCEPTED_STA = "ExpectedStatus";
        #endregion

        #region *** Constructors ***
        public InvalidTaskStateException(ITaskItem task, ProcessStatusEnum newStatus, params ProcessStatusEnum[] expectedStatuses) : base(GetMessage(task, newStatus, expectedStatuses))
        {
            Data.Add(CS_EXCC_ENT_TYPE, task.GetType());
            Data.Add(CS_EXCC_ENT_ID, task.Id);
            Data.Add(CS_EXCC_ENT_CURRENT_STA, task.Status);
            Data.Add(CS_EXCC_ENT_NEW_STA, newStatus);
            Data.Add(CS_EXCC_ENT_EXCEPTED_STA, expectedStatuses);
            
        }

        public InvalidTaskStateException(ITaskItem task, params ProcessStatusEnum[] expectedStatuses) : base(
            GetMessage(task, expectedStatuses))
        {
            Data.Add(CS_EXCC_ENT_TYPE, task.GetType());
            Data.Add(CS_EXCC_ENT_ID, task.Id);
            Data.Add(CS_EXCC_ENT_CURRENT_STA, task.Status);
            Data.Add(CS_EXCC_ENT_NEW_STA, string.Empty);
            Data.Add(CS_EXCC_ENT_EXCEPTED_STA, expectedStatuses);
        }
        #endregion

        #region *** Private Operations ***
        private static string GetMessage(ITaskItem task, ProcessStatusEnum newStatus,
            params ProcessStatusEnum[] expectedStatuses)
        {
            return $"Invalid task state exception. Task: {task.GetType()}[{task.Id}] is in state {task.Status} and cannot be changed to {newStatus}. Expected states: {string.Join(", ", expectedStatuses)}";
        }
        private static string GetMessage(ITaskItem task, params ProcessStatusEnum[] expectedStatuses)
        {
            return $"Invalid task state exception. Task: {task.GetType()}[{task.Id}] is in state {task.Status}. Expected states: {string.Join(", ", expectedStatuses)}";
        }
        #endregion
    }
}
