/*
 * Copyright 2013 NEHTA
 *
 * Licensed under the NEHTA Open Source (Apache) License; you may not use this
 * file except in compliance with the License. A copy of the License is in the
 * 'license.txt' file, which should be provided with this work.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using JetBrains.Annotations;
using Nehta.HL7.CDA;
using Nehta.VendorLibrary.Common;

namespace Nehta.VendorLibrary.CDA.SCSModel.Common
{
    /// <summary>
    /// This class is designed to encapsulate the properties within a CDA document that make up 
    /// a medical history,
    /// 
    /// This class implements sevearl interfaces and can be cast into these interfaces to contstrain
    /// the medical histroy to a particular type of CDA medical history
    /// 
    /// Please use the CreateMedicalHistory() method on the appropriate parent SCS object to 
    /// instantiate this class.
    /// </summary>
    [Serializable]
    [DataContract]
    [KnownType(typeof(ProblemDiagnosis))]
    [KnownType(typeof(Statement))]
    internal class MedicalHistory : IMedicalHistory, IMedicalHistoryWithoutExclusions
    {
        #region Properties
        
        /// <summary>
        /// A list of problem diagnosis
        /// </summary>
        [CanBeNull]
        [DataMember]
        public List<IProblemDiagnosis> ProblemDiagnosis { get; set; }

        /// <summary>
        /// An exculstion statement to associate with the problem diagnosis
        /// </summary>
        [CanBeNull]
        [DataMember]
        public Statement ProblemDiagnosisExclusionStatement { get; set; }

        /// <summary>
        /// A list of procedures
        /// </summary>
        [CanBeNull]
        [DataMember]
        public List<Procedure> Procedures { get; set; }

        /// <summary>
        /// An exculstion statement to associate with the procedures
        /// </summary>
        [CanBeNull]
        [DataMember]
        public Statement ProceduresExclusionStatement { get; set; }

        /// <summary>
        /// A list of other medical history items that contain any relevant medical history
        /// </summary>
        [CanBeNull]
        [DataMember]
        public List<IMedicalHistoryItem> MedicalHistoryItems { get; set; }

        /// <summary>
        /// Provide a custom Narrative 
        /// </summary>
        [CanBeNull]
        [DataMember]
        public StrucDocText CustomNarrativeMedicalHistory { get; set; }

        #endregion

        #region Constructors
        internal MedicalHistory()
        {
        }
        #endregion

        #region Validation
        /// <summary>
        /// Validates this medical history
        /// </summary>
        /// <param name="path">The path to this object as a string</param>
        /// <param name="messages">the validation messages to date, these may be added to within this method</param>
        public void Validate(string path, List<ValidationMessage> messages) 
        {
            var vb = new ValidationBuilder(path, messages);

            if (ProblemDiagnosis != null)
            {
                for (var x = 0; x < ProblemDiagnosis.Count; x++)
                {
                    ProblemDiagnosis[x].Validate(vb.Path + string.Format("ProblemDiagnosis[{0}]", x), vb.Messages);
                }
            }

            // Validate problem diagnosis exclusion statement
            if (ProblemDiagnosisExclusionStatement != null)
            {
                ProblemDiagnosisExclusionStatement.Validate(vb.Path + "ProblemDiagnosisExclusionStatement", vb.Messages);
            }

            if (Procedures != null)
            {
                for (var x = 0; x < Procedures.Count; x++)
                {
                    Procedures[x].Validate(vb.Path + string.Format("Procedures[{0}]", x), vb.Messages);
                }
            }

            // Validate procedures exclusion statement
            if (ProceduresExclusionStatement != null)
            {
                ProceduresExclusionStatement.Validate(vb.Path + "ProceduresExclusionStatement", vb.Messages);
            }

            if (MedicalHistoryItems != null)
            {
                for (var x = 0; x < MedicalHistoryItems.Count; x++)
                {
                    MedicalHistoryItems[x].Validate(vb.Path + string.Format("OtherMedicalHistory[{0}]", x), vb.Messages);
                }
            }

            if (ProceduresExclusionStatement == null && ProblemDiagnosisExclusionStatement == null)
            {
                if (ProblemDiagnosis == null && Procedures == null && MedicalHistoryItems == null)
                {
                    vb.AddValidationMessage(vb.PathName, null, "At least one ProblemDiagnosis, Procedures or MedicalHistoryItems is required");
                }
            }

            if (ProblemDiagnosis != null && ProblemDiagnosisExclusionStatement != null)
            {
                vb.AddValidationMessage(vb.PathName, null, "Only one ProblemDiagnosis or ProblemDiagnosisExclusionStatement can be included in medical history");
            }

            if (Procedures != null && ProceduresExclusionStatement != null)
            {
                vb.AddValidationMessage(vb.PathName, null, "Only one Procedures or ProceduresExclusionStatement can be included in medical history");
            }

            if (MedicalHistoryItems != null && (ProceduresExclusionStatement != null || ProblemDiagnosisExclusionStatement != null))
            {
               vb.AddValidationMessage(vb.PathName, null, "When 'Other Medical History Item' is present then exclusion statements for procedure and problem/diagnosis are not to be included");
            }
        }

       /// <summary>
        /// Validates this medical history
        /// </summary>
        /// <param name="path">The path to this object as a string</param>
        /// <param name="messages">the validation messages to date, these may be added to within this method</param>
        void IMedicalHistoryWithoutExclusions.Validate(string path, List<ValidationMessage> messages) 
        {
            var vb = new ValidationBuilder(path, messages);


            if (ProblemDiagnosis != null)
            {
                for (var x = 0; x < ProblemDiagnosis.Count; x++)
                {
                    ProblemDiagnosis[x].Validate(
                        vb.Path + string.Format("ProblemDiagnosis[{0}]", x), vb.Messages);
                }
            }

            if (Procedures != null)
            {
                for (var x = 0; x < Procedures.Count; x++)
                {
                    Procedures[x].Validate(
                        vb.Path + string.Format("Procedures[{0}]", x), vb.Messages);
                }
            }

            if (MedicalHistoryItems != null)
            {
                for (var x = 0; x < MedicalHistoryItems.Count; x++)
                {
                    MedicalHistoryItems[x].Validate(
                        vb.Path + string.Format("OtherMedicalHistory[{0}]", x), vb.Messages);
                }
            }
        }

        #endregion
    }

}