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

using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Repositories
{
    public class NavigationColumnRepository : Repository<NavigationColumn>
    {
        private readonly string _connectionString;

        public NavigationColumnRepository(string connectionString)
        {
            _connectionString = connectionString;
        }

        public override List<NavigationColumn> FindAll()
        {
            const string query = "SELECT Id, Name FROM dbo.Dzial ORDER BY SortOrder";

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

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(_connectionString);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                        navigationColumns.Add(new NavigationColumn(reader.GetInt32(0), reader.GetString(1)));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            return navigationColumns;
        }

        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>();

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(_connectionString);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                        navigationColumns.Add(new NavigationColumn(reader.GetInt32(0), reader.GetString(1)));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            return navigationColumns;
        }

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

        public List<NavigationSubColumn> FindActiveNavigationSubColumnsByNavigationColumnId(int navigationColumnId)
        {
            NavigationSubColumnRepository navigationSubColumnRepository = new NavigationSubColumnRepository(_connectionString);
            return navigationSubColumnRepository.FindAllActiveByNavigationColumnId(navigationColumnId, null);
        }
    }
}