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

namespace Baza_Reklam.Classes.Repositories
{
    public class Repository<T> : IRepository<T>, IDisposable
    {
        protected string _connectionString;
        protected SqlConnection _connection;
        protected SqlCommand _command;
        protected SqlDataReader _reader;
        protected SqlTransaction _transaction;

        public Repository(string connectionString)
        {
            _connectionString = connectionString;
            _connection = new SqlConnection(connectionString);
        }

        public virtual T Find(int id)
        {
            throw new NotImplementedException();
        }

        public virtual List<T> FindAll()
        {
            throw new NotImplementedException();
        }

        public virtual void Save(T entity)
        {
            throw new NotImplementedException();
        }

        public virtual void Delete(T entity)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            if (_connection == null) return;
            
            if (_connection.State == ConnectionState.Open)
                _connection.Close();
                
            _connection.Dispose();
        }

        protected void CleanUp()
        {
            if (_reader != null) _reader.Close();

            if (_command != null) _command.Dispose();

            if (_connection != null && _connection.State == ConnectionState.Open)
                _connection.Close();
        }
    }
}