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

namespace Baza_Reklam.Classes.Repositories
{
    public class SectionRepository : Repository<Section>
    {
        public override Section Find(int sectionId)
        {
            string query = "SELECT S.Id, S.Nazwa, T.Id, T.Symb, T.Symb2, T.max_rabat AS MaxDiscount, T.InvoiceProviderId FROM dbo.Grzbiety S INNER JOIN dbo.[Lista Tytułów] T ON S.TytulId=T.Id WHERE S.Id={0}";
            query = string.Format(query, sectionId);

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                        return new Section(reader.GetInt32(0),
                                           new Title(reader.GetInt32(2), reader.GetString(3), reader.GetString(4), reader.GetDouble(5), reader.GetInt32(6)), 
                                           reader.GetString(1));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return null;

        }


        public List<Section> FindAllActiveByTitle(Title title)
        {
            string query = "SELECT Id, Nazwa, Active FROM Grzbiety WHERE tytulid={0} AND Active=1 ORDER BY Id";
            query = string.Format(query, title.Id);

            List<Section> sections = new List<Section>();

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        sections.Add(new Section(reader.GetInt32(0), title, reader.GetString(1)));
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return sections;
        }
    }
}