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

using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Repositories
{
    public class TitleRepository : Repository<Title>
    {
        public TitleRepository(string connectionString)
            : base(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>();

            try
            {
                _connection.Open();
                _command = new SqlCommand(query, _connection);
                _reader = _command.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
            {
               CleanUp();
            }
            return titles;
        }

        public List<Section> FindSectionsByTitle(Title title)
        {
            SectionRepository sectionRepository = new SectionRepository(_connectionString);
            return sectionRepository.FindAllActiveByTitle(title); 
        }
    }
}