﻿using System;
using System.Linq;
using Composite.Core.Extensions;
using Composite.Core.Routing;
using Composite.Core.Types;
using Composite.Data;

namespace Demo
{
    class CustomDataUrlProvider : IInternalUrlProvider
    {
        private readonly string _shortTypeName;
        private readonly Type _type;
        private readonly Type _keyType;

        public CustomDataUrlProvider(string shortTypeName, Type type)
        {
            _type = type;
            _keyType = type.GetKeyProperties().Single().PropertyType;
            _shortTypeName = shortTypeName;
        }

        public string BuildInternalUrl(IDataReference reference)
        {
            string serializedKey = ValueTypeConverter.Convert<string>(reference.KeyValue);
            if (string.IsNullOrEmpty(serializedKey))
            {
                return null;
            }
            return "~/{0}({1})".FormatWith(_shortTypeName, serializedKey);
        }
    }
}
