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

using Baza_Reklam.Classes.Model;

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

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

        public List<Title> FindAllActive()
        {
            const string query = "SELECT Id, Symb, SYMB2, Max_Rabat, [Tyt Full Name], InvoiceProviderId from [lista tytułów] where active=1 ORDER BY Id";

            List<Title> titles = new List<Title>();

            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())
                    {
                        titles.Add(new Title(reader.GetInt32(0), reader.GetString(1), reader.GetString(2).Trim(), reader.GetDouble(3), reader.GetInt32(5)));
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return titles;
        }
    }
}