/*
 * Copyright 2011 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 imp201112131459lied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

using Nehta.VendorLibrary.CDA.SCSModel.Common;
using Nehta.VendorLibrary.Common;

namespace Nehta.VendorLibrary.CDA.Common
{
    /// <summary>
    /// Formats the interval for the narrative.
    /// </summary>
    public static class CdaIntervalFormatter
    {
        /// <summary>
        /// Formats the interval.
        /// </summary>
        /// <param name="cdaInterval">Interval</param>
        /// <returns>Narrative text.</returns>
        public static string Format(CdaInterval cdaInterval)
        {
            if (cdaInterval == null)
            {
                return null;
            }

            if (cdaInterval.Type == CdaInterval.IntervalType.Low)
            {
                // Low
                return cdaInterval.Low.NarrativeText() + " (low)";
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.Width)
            {
                // Width
                return Format(cdaInterval.IntervalWidth);
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.LowWidth)
            {
                // Low/Width
                return cdaInterval.Low.NarrativeText() + " (low) " + Format(cdaInterval.IntervalWidth) + " (width)";
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.LowHigh)
            {
                // Low/High
                return cdaInterval.Low.NarrativeText() + " - " + cdaInterval.High.NarrativeText();
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.Center)
            {
                // Center
                return cdaInterval.Center.NarrativeText();
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.CenterWidth)
            {
                // Center/Width
                return cdaInterval.Center.NarrativeText() + " (center) " + Format(cdaInterval.IntervalWidth)  + " (width)";
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.High)
            {
                // High
                return cdaInterval.High.NarrativeText() + " (high)";
            }
            if (cdaInterval.Type == CdaInterval.IntervalType.HighWidth)
            {
                // High/Width
                return cdaInterval.High.NarrativeText() + " (high) " + Format(cdaInterval.IntervalWidth) + " (width)";
            }

            return null;
        }

        /// <summary>
        /// Formats the interval width for the narrative.
        /// </summary>
        /// <param name="intervalWidth">CdaIntervalWidth</param>
        /// <returns>Narrative text.</returns>
        public static string Format(CdaIntervalWidth intervalWidth)
        {
            if (intervalWidth == null)
            {
                return null;
            }

            return intervalWidth.Value + " " + intervalWidth.Unit.GetAttributeValue<NameAttribute, string>(x => x.Name);
        }
    }
}
