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

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

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

        public IEnumerable<SqlScriptObject> Script(string database, string schema)
        {
            var sqlObjects = _server.Databases[database]
                                    .Views
                                    .Cast<View>()
                                    .Where(view => !view.IsSystemObject)
                                    .Where(view => view.Schema == schema);

            return Script(sqlObjects);
        }
    }
}