using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace uBIMEarthTools.Utils
{
    public class FailureHandler : IFailuresPreprocessor
    {
        public string ErrorMessage { set; get; }
        public string ErrorSeverity { set; get; }

        public FailureHandler()
        {
            ErrorMessage = "";
            ErrorSeverity = "";
        }

        public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
        {
            IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
            foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
            {
                FailureDefinitionId id = failureMessageAccessor.GetFailureDefinitionId();
                try
                {
                    ErrorMessage = failureMessageAccessor.GetDescriptionText();
                }
                catch
                {
                    ErrorMessage = "Unknown Error";
                }

                try
                {
                    FailureSeverity failureSeverity = failureMessageAccessor.GetSeverity();
                    ErrorSeverity = failureSeverity.ToString();
                    failuresAccessor.DeleteWarning(failureMessageAccessor);
                    if (!string.IsNullOrEmpty(ErrorMessage))
                    {
                        return FailureProcessingResult.ProceedWithCommit;
                    }
                    else
                    {
                        return FailureProcessingResult.Continue;
                    }
                    //if (failureSeverity == FailureSeverity.Warning)
                    //{
                    //    failuresAccessor.DeleteWarning(failureMessageAccessor);
                    //    return FailureProcessingResult.ProceedWithCommit;

                    //}
                    //else
                    //{
                    //    failuresAccessor.ResolveFailure(failureMessageAccessor);
                    //    return FailureProcessingResult.ProceedWithCommit;
                    //}
                }
                catch
                { }
            }
            return FailureProcessingResult.Continue;
        }
    }
}
