using System.Collections.Generic;
using System.Data.SqlClient;

using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Repositories
{
    public class NavigationColumnRepository : Repository<NavigationColumn>
    {
        public NavigationColumnRepository(string connectionString)
            : base(connectionString)
        {
        }

        public List<NavigationColumn> FindAllActiveByTitle(Title title)
        {
            string query = "SELECT Id, Name FROM dbo.Dzial WHERE Active=1 AND TitleId={0} ORDER BY SortOrder";
            query = string.Format(query, title.Id);

            List<NavigationColumn> navigationColumns = new List<NavigationColumn>();

            try
            {
                _connection.Open();
                _command = new SqlCommand(query, _connection);
                _reader = _command.ExecuteReader();
                if (_reader != null)
                {
                    while (_reader.Read())
                        navigationColumns.Add(new NavigationColumn(_reader.GetInt32(0), _reader.GetString(1)));
                }
            }
            finally
            {
                CleanUp();
            }
            return navigationColumns;
        }

        public List<NavigationSubColumn> FindActiveNavigationSubColumnsByNavigationColumn(NavigationColumn navigationColumn)
        {
            NavigationSubColumnRepository navigationSubColumnRepository = new NavigationSubColumnRepository(_connectionString);
            return navigationSubColumnRepository.FindAllActiveByNavigationColumn(navigationColumn);
        }
    }
}