using System.Collections.Generic;
using System.Linq;
using Microsoft.SqlServer.Management.Smo;

namespace Adform.SqlScripter.Script.Engines
{
    public class UserDefinedDataTypeScriptEngine : SqlObjectScriptEngine<UserDefinedDataType>, ISchemaObjectScriptEngine
    {
        private readonly Server _server;

        public UserDefinedDataTypeScriptEngine(Server server, ISqlObjectConverter converter, ISqlScriptEngine engine)
            : base(converter, engine)
        {
            _server = server;
        }

        public IEnumerable<SqlScriptObject> Script(string database, string schema)
        {
            var sqlObjects = _server.Databases[database]
                                    .UserDefinedDataTypes
                                    .Cast<UserDefinedDataType>()
                                    .Where(udDataType => udDataType.Schema == schema);

            return Script(sqlObjects);
        }
    }
}