﻿using System;
using umbraco.interfaces;

namespace Umbraco.Core.Models
{
    public static class PropertyTypeExtensions
    {
        /// <summary>
        /// Resolves the IDataType for a PropertyType.
        /// </summary>
        /// <param name="propertyType">PropertyType that references a DataType</param>
        /// <param name="propertyId">Id of the Property which references this DataType through its PropertyType</param>
        /// <returns><see cref="IDataType"/></returns>
        /// <remarks>
        /// This extension method is left internal because we don't want to take
        /// a hard dependency on the IDataType, as it will eventually go away and
        /// be replaced by PropertyEditors. It is however needed to generate xml
        /// for a property/propertytype when publishing.
        /// </remarks>
        internal static IDataType DataType(this PropertyType propertyType, int propertyId)
        {
            Mandate.ParameterNotNull(propertyType, "propertyType");
            var dataType = ApplicationContext.Current.Services.DataTypeService.GetDataTypeById(propertyType.DataTypeId);
            
            if (dataType == null)
                throw new InvalidOperationException("No IDataType found for control ID " + propertyType.DataTypeId);
            
            dataType.DataTypeDefinitionId = propertyType.DataTypeDefinitionId;
            dataType.Data.PropertyId = propertyId;
            return dataType;
        }
    }
}