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

namespace Baza_Reklam
{
    public class InvoiceProviderFactory
    {
        private static InvoiceProviderFactory _instance;
        private readonly List<IInvoiceProvider> _invoiceProviders = new List<IInvoiceProvider>();

        private InvoiceProviderFactory()
        {
            SqlConnection conn = null;
            SqlDataReader reader = null;
            SqlCommand cmd = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand("SELECT * FROM dbo.InvoiceProvider", conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        InvoiceProvider invoiceProvider = new InvoiceProvider();
                        invoiceProvider.Id = Convert.ToInt32(reader["Id"]);
                        invoiceProvider.ShortName = Convert.ToString(reader["ShortName"]);
                        invoiceProvider.Name = Convert.ToString(reader["Name"]);
                        invoiceProvider.Street = Convert.ToString(reader["Street"]);
                        invoiceProvider.City = Convert.ToString(reader["City"]);
                        invoiceProvider.PostCode = Convert.ToString(reader["PostCode"]);
                        invoiceProvider.Country = Convert.ToString(reader["Country"]);
                        invoiceProvider.Phone = Convert.ToString(reader["Phone"]);
                        invoiceProvider.TaxNumber = Convert.ToString(reader["TaxNumber"]);
                        invoiceProvider.TaxCountryPrefix = Convert.ToString(reader["TaxCountryPrefix"]);
                        invoiceProvider.SystemKsiegowyId = Convert.ToInt32(reader["SystemKsiegowyId"]);
                        _invoiceProviders.Add(invoiceProvider);
                    }
                }
            }
// ReSharper disable RedundantCatchClause
            catch (Exception)
            {
                //TODO: log exception
                throw;
            }
// ReSharper restore RedundantCatchClause
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }

        public static InvoiceProviderFactory Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new InvoiceProviderFactory();
                }
                return _instance;
            }
        }

        public static List<IInvoiceProvider> GetInvoiceProviders()
        {
            return Instance._invoiceProviders;
        }

        public static IInvoiceProvider GetInvoiceProviderById(int invoiceProviderId)
        {
            return
                Instance._invoiceProviders.Find(
                    delegate(IInvoiceProvider invoiceProvider) { return invoiceProvider.Id == invoiceProviderId; });
        }

        public static List<IInvoiceProvider> GetInvoiceProvidersById(int invoiceProviderId)
        {
            return
                Instance._invoiceProviders.FindAll(
                    delegate(IInvoiceProvider invoiceProvider) { return invoiceProvider.Id == invoiceProviderId; });
        }
    }
}