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

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

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

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

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

            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())
                    {
                        NavigationSubColumn navigationSubColumn = new NavigationSubColumn(reader.GetInt32(0), reader.GetString(1));
                        navigationSubColumns.Add(navigationSubColumn);
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return navigationSubColumns;
        }

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

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

            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())
                    {
                        NavigationSubColumn navigationSubColumn = new NavigationSubColumn(reader.GetInt32(0), reader.GetString(1));
                        navigationSubColumn.NavigationColumn = navigationColumn;

                        navigationSubColumns.Add(navigationSubColumn);
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return navigationSubColumns;
        }
    }
}
