using System.Linq;

namespace Valg.EVA.Skanning.Contracts.CommonEnums
{
    public class TransferState
    {
        public int Id { get; }
        public string Name { get; }
        public string UploadStatusName { get; }
        public int? UploadStatusCode { get; }

        private TransferState(int id, string name, string uploadStatusName, int? uploadStatusCode)
        {
            Id = id;
            Name = name;
            UploadStatusName = uploadStatusName;
            UploadStatusCode = uploadStatusCode;
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            return ((TransferState)obj).Id == Id;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode(); 
        }

        public override string ToString()
        {
            return Name;
        }

        public static readonly TransferState NotStarted = new TransferState(1, "NotStarted", null, null);
        public static readonly TransferState FileTransferStarted = new TransferState(2, "FileTransferStarted", null, null);
        public static readonly TransferState FileTransferFinished = new TransferState(3, "FileTransferFinished", null, null);
        public static readonly TransferState FileTransferFailed = new TransferState(4, "FileTransferFailed", null, null);
        public static readonly TransferState QueuedOnServer = new TransferState(5, "QueuedOnServer", "Queued", 1000);
        public static readonly TransferState StartedOnServer = new TransferState(6, "StartedOnServer", "Started", 1001);
        public static readonly TransferState FinishedOnServer = new TransferState(7, "FinishedOnServer", "Finished", 1002);
        public static readonly TransferState ErrorOnServer = new TransferState(8, "ErrorOnServer", "Error", 1003);
        public static readonly TransferState[] AllTransferStates = { NotStarted, FileTransferStarted, FileTransferFinished, FileTransferFailed, QueuedOnServer, StartedOnServer, FinishedOnServer, ErrorOnServer };

        public static TransferState GetTransferStateFromUploadStatusCode(int status)
        {
            var match = AllTransferStates.SingleOrDefault(s => s.UploadStatusCode == status);
            return match;
        }

        public static TransferState GetTransferStateFromId(int transferStateId)
        {
            return AllTransferStates.Single(s => s.Id == transferStateId);
        }
    }
}
