using DotNetNuke.Data;
using ACLib.P2.AppContext;
using System.Data;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Core.MSSQL.SqlHelper;
using System.Collections.Generic;
using System.Data.Common;

namespace ACLib.P2.ACPI
{

    public class StudentManagerP1 : IDataContext
    {
        private readonly DatabaseContext _context;

        public StudentManagerP1(DatabaseContext databaseContext)
        {
            _context = databaseContext;
        }
        private T MapDataToObjectOfType<T>(IDataReader reader)
        {
            // Implement your mapping logic here based on the reader's columns
            // For simplicity, you can use a data mapper library like AutoMapper if needed

            // Example: Map columns to properties of T
            var result = Activator.CreateInstance<T>();

            // Assuming properties of T correspond to columns in the reader
            foreach (var property in typeof(T).GetProperties())
            {
                if (!reader.IsDBNull(reader.GetOrdinal(property.Name)))
                {
                    property.SetValue(result, reader[property.Name]);
                }
            }

            return result;
        }

        public void BeginTransaction()
        {
            throw new NotImplementedException();
        }

        public void Commit()
        {
            throw new NotImplementedException();
        }

        public DatabaseContext CreateDbContext()
        {
            return _context;
        }

        public void Dispose()
        {
            _context.Dispose();
        }


        public void Execute(CommandType type, string sql, params object[] args)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<T> ExecuteQuery<T>(CommandType commandType, string commandText, params object[] parameters)
        {

            using (var connection = _context.Database.GetDbConnection())
            {
                connection.Open();

                // Create a command
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = commandText;
                    command.CommandType = commandType;
                    if (parameters != null)
                    {
                        foreach (DbParameter parameter in parameters)
                        {
                            /*
                            DbParameter param = command.CreateParameter();
                            param.ParameterName = parameter.ParameterName;
                            param.Value = parameter.Value;
                            */
                            command.Parameters.Add(parameter);
                        }
                    }

                    // Execute the query
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            // Map the data to objects of type T
                            yield return MapDataToObjectOfType<T>(reader);
                        }
                    }
                }
            }
        }

        public T ExecuteScalar<T>(CommandType type, string sql, params object[] args)
        {
            throw new NotImplementedException();
        }

        public T ExecuteSingleOrDefault<T>(CommandType type, string sql, params object[] args)
        {
            throw new NotImplementedException();
        }

        public IRepository<T> GetRepository<T>() where T : class
        {
            throw new NotImplementedException();
        }

        public void RollbackTransaction()
        {
            throw new NotImplementedException();
        }
        public IDataReader ExecuteReader(CommandType commandType, string commandText, params SqlParameter[] parameters)
        {
            if (string.IsNullOrEmpty(commandText))
            {
                throw new ArgumentException("Command text cannot be null or empty.", nameof(commandText));
            }
            var database = _context.Database;
            var dbCommand = database.GetDbConnection().CreateCommand();

            dbCommand.CommandType = commandType;
            dbCommand.CommandText = commandText;
            dbCommand.Parameters.AddRange(parameters);

            // Open the database connection
            if (database.GetDbConnection().State != ConnectionState.Open)
            {
                database.GetDbConnection().Open();
            }

            // Execute the command and return the DataReader
            return dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }

    }

}
