using System.Collections.Generic;
using System.Data.SqlClient;
using Baza_Reklam.Classes.Model;

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

        public override List<NavigationSubColumn> FindAll()
        {
            const string query = "SELECT Id, Name FROM Rozdzial ORDER BY SortOrder";

            List<NavigationSubColumn> navigationSubColumns = new List<NavigationSubColumn>();

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

            }
            return navigationSubColumns;
        }

        public List<NavigationSubColumn> FindAllActiveByNavigationColumn(NavigationColumn navigationColumn)
        {
            return FindAllActiveByNavigationColumnId(navigationColumn.Id, navigationColumn);
        }

        public List<NavigationSubColumn> FindAllActiveByNavigationColumnId(int navigationColumnId, NavigationColumn navigationColumn)
        {
            string query = "SELECT Id, Name FROM Rozdzial WHERE Active=1 AND DzialId={0} ORDER BY SortOrder";
            query = string.Format(query, navigationColumnId);

            List<NavigationSubColumn> navigationSubColumns = new List<NavigationSubColumn>();

            try
            {
                _connection.Open();
                _command = new SqlCommand(query, _connection);
                _reader = _command.ExecuteReader();
                if (_reader != null)
                {
                    while (_reader.Read())
                    {
                        NavigationSubColumn navigationSubColumn = new NavigationSubColumn(_reader.GetInt32(0), _reader.GetString(1));
                        if (navigationColumn != null)
                            navigationSubColumn.NavigationColumn = navigationColumn;

                        navigationSubColumns.Add(navigationSubColumn);
                    }
                }
            }
            finally
            {
                CleanUp();

            }
            return navigationSubColumns;
        }
    }
}
