
using System.Text.Json;
using System.Data.OleDb;
using TarifasANEEL;
using System.Data;
using WinRT;
using System.Text;
using Windows.Media.Capture;
using System.Configuration;
using System.ComponentModel;
using System.Data.SqlClient;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Windows.System;
using System.Security.Cryptography;

public class Program
{
    private static async Task Main(string[] args)
    {
        string AneelURL = "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search?resource_id=fcf2906c-7c32-4b9b-a637-054e7a5234f4&limit=32000";
        //string AccessPath = @"C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\ANEEL.accdb";

        Rootobject? rootObject = await PullDataAsync(AneelURL);

        List<Record>? records = rootObject?.result?.records?.ToList();
        List<Record>? tempRecords = rootObject?.result?.records?.ToList();
        Console.Clear();
        Console.WriteLine(rootObject?.result?.records?.Last()._id);

        if (records != null && (records.Last()._id != rootObject?.result.total))
        {
            while (rootObject?.result.total != records.Last()._id)
            //while (records.Last()._id != 32000)
            {
                rootObject = await PullDataAsync(AneelURL + "&offset=" + records.Last()._id);
                Console.WriteLine(rootObject?.result?.records?.Last()._id);

                tempRecords = rootObject?.result?.records?.ToList();
                if (tempRecords != null)
                {
                    records.AddRange(tempRecords);
                }
            }
        }
        if (records != null)
        {
            UpdateData(records, "tblTarifas");
        }

    }

    private static async Task<Rootobject?> PullDataAsync(string sURL)
    {
        var client = new HttpClient();
        var request = new HttpRequestMessage(HttpMethod.Post, sURL);
        var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();

        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true,
        };

        Rootobject? rootObject = JsonSerializer.Deserialize<Rootobject>(await response.Content.ReadAsStringAsync(), options);

        return rootObject;
    }
    public static DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }
    public static void UpdateData<T>(List<T> list, string TableName)
    {
        DataTable dt = new DataTable("MyTable");
        dt = ConvertToDataTable(list);

        using (SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\Tarifas.mdf; Integrated Security = True; Connect Timeout = 30"))
        {
            using (SqlCommand command = new SqlCommand("", conn))
            {
                try
                {
                    conn.Open();

                    //Creating temp table on database
                    command.CommandText = "CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT, DscREH TEXT, SigAgente TEXT, NumCNPJDistribuidora TEXT, DatInicioVigencia TEXT, DatFimVigencia TEXT, DscBaseTarifaria TEXT, DscSubGrupo TEXT, DscModalidadeTarifaria TEXT, DscClasse TEXT, DscSubClasse TEXT, DscDetalhe TEXT, NomPostoTarifario TEXT, DscUnidadeTerciaria TEXT, SigAgenteAcessante TEXT, VlrTUSD TEXT, VlrTE TEXT)";
                    command.ExecuteNonQuery();

                    //Bulk insert into temp table
                    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn))
                    {
                        bulkcopy.BulkCopyTimeout = 660;
                        bulkcopy.DestinationTableName = "TmpTable";
                        bulkcopy.WriteToServer(dt);
                        bulkcopy.Close();
                    }

                    // Updating destination table, and dropping temp table
                    command.CommandTimeout = 300;
                    command.CommandText = @"UPDATE P SET
                                            P.[DatGeracaoConjuntoDados] = T.[DatGeracaoConjuntoDados],
                                            P.[DscREH] = T.[DscREH],
                                            P.[SigAgente] = T.[SigAgente],
                                            P.[NumCNPJDistribuidora] = T.[NumCNPJDistribuidora],
                                            P.[DatInicioVigencia] = T.[DatInicioVigencia],
                                            P.[DatFimVigencia] = T.[DatFimVigencia],
                                            P.[DscBaseTarifaria] = T.[DscBaseTarifaria],
                                            P.[DscSubGrupo] = T.[DscSubGrupo],
                                            P.[DscModalidadeTarifaria] = T.[DscModalidadeTarifaria],
                                            P.[DscClasse] = T.[DscClasse],
                                            P.[DscSubClasse] = T.[DscSubClasse],
                                            P.[DscDetalhe] = T.[DscDetalhe],
                                            P.[NomPostoTarifario] = T.[NomPostoTarifario],
                                            P.[DscUnidadeTerciaria] = T.[DscUnidadeTerciaria],
                                            P.[SigAgenteAcessante] = T.[SigAgenteAcessante],
                                            P.[VlrTUSD] = T.[VlrTUSD],
                                            P.[VlrTE] = T.[VlrTE]
                                            FROM[TmpTable] AS P INNER JOIN TmpTable AS T ON P.[_id] = T.[_id]; DROP TABLE TmpTable; ";
                    command.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    // Handle exception properly
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
    private static void AdicionarDadosAoBanco(List<Record> records, string accessPath)
    {
        try
        {
            using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessPath};Persist Security Info=False;"))
            {
                connection.Open();

                // Verificar se a tabela temporária já existe
                if (!TmpTableExiste(connection))
                {
                    CriarTmpTable(connection);
                }

                using (OleDbTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        foreach (var record in records)
                        {
                            using (OleDbCommand command = new OleDbCommand("INSERT INTO TmpTable VALUES (@_id, @DatGeracaoConjuntoDados, @DscREH, @SigAgente, @NumCNPJDistribuidora, @DatInicioVigencia, @DatFimVigencia, @DscBaseTarifaria, @DscSubGrupo, @DscModalidadeTarifaria, @DscClasse, @DscSubClasse, @DscDetalhe, @NomPostoTarifario, @DscUnidadeTerciaria, @SigAgenteAcessante, @VlrTUSD, @VlrTE)", connection, transaction))
                            {
                                // Adicionar parâmetros
                                command.Parameters.AddWithValue("@_id", record._id);
                                command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados);
                                command.Parameters.AddWithValue("@DscREH", record.DscREH);
                                command.Parameters.AddWithValue("@SigAgente", record.SigAgente);
                                command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora);
                                command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia);
                                command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
                                command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
                                command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria);
                                command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo);
                                command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
                                command.Parameters.AddWithValue("@DscClasse", record.DscClasse);
                                command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse);
                                command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe);
                                command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario);
                                command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria);
                                command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante);
                                command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD);
                                command.Parameters.AddWithValue("@VlrTE", record.VlrTE);

                                command.ExecuteNonQuery();
                                Console.WriteLine(record._id);
                            }
                        }

                        transaction.Commit();
                        Console.WriteLine("Dados adicionados com sucesso à tabela temporária!");
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Console.WriteLine($"Erro ao adicionar dados: {ex.Message}");
                    }
                }
            }

            Console.WriteLine("Dados adicionados com sucesso à tabela temporária!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Ocorreu um erro: {ex.Message}");
        }
    }

    private static bool TmpTableExiste(OleDbConnection connection)
    {
        try
        {
            using (OleDbCommand command = new OleDbCommand("SELECT COUNT(*) FROM TmpTable", connection))
            {
                command.ExecuteScalar();
                return true;
            }
        }
        catch (OleDbException)
        {
            // Se ocorrer uma exceção, a tabela provavelmente não existe
            return false;
        }
    }

    private static void CriarTmpTable(OleDbConnection connection)
    {
        using (OleDbCommand command = new OleDbCommand("CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT, DscREH TEXT, SigAgente TEXT, NumCNPJDistribuidora TEXT, DatInicioVigencia TEXT, DatFimVigencia TEXT, DscBaseTarifaria TEXT, DscSubGrupo TEXT, DscModalidadeTarifaria TEXT, DscClasse TEXT, DscSubClasse TEXT, DscDetalhe TEXT, NomPostoTarifario TEXT, DscUnidadeTerciaria TEXT, SigAgenteAcessante TEXT, VlrTUSD TEXT, VlrTE TEXT)", connection))
        {
            command.ExecuteNonQuery();
        }
    }

    private static void AdicionarDadosATmpTable(OleDbConnection connection, List<Record> records)
    {
        foreach (var record in records)
        {
            using (OleDbCommand command = new OleDbCommand("INSERT INTO TmpTable VALUES (@_id, @DatGeracaoConjuntoDados, @DscREH, @SigAgente, @NumCNPJDistribuidora, @DatInicioVigencia, @DatFimVigencia, @DscBaseTarifaria, @DscSubGrupo, @DscModalidadeTarifaria, @DscClasse, @DscSubClasse, @DscDetalhe, @NomPostoTarifario, @DscUnidadeTerciaria, @SigAgenteAcessante, @VlrTUSD, @VlrTE)", connection))
            {
                // Adicionar parâmetros
                command.Parameters.AddWithValue("@_id", record._id);
                command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados);
                command.Parameters.AddWithValue("@DscREH", record.DscREH);
                command.Parameters.AddWithValue("@SigAgente", record.SigAgente);
                command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora);
                command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia);
                command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
                command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
                command.Parameters.AddWithValue("@DscBaseTarifaria",record.DscBaseTarifaria);
                command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo);
                command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
                command.Parameters.AddWithValue("@DscClasse", record.DscClasse);
                command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse);
                command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe);
                command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario);
                command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria);
                command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante);
                command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD);
                command.Parameters.AddWithValue("@VlrTE", record.VlrTE);

                // Adicionar outros parâmetros conforme necessário

                command.ExecuteNonQuery();
            }
        }
    }
}