using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataPoolingService
{
    class ConnectionClass
    {
        public class Config
        {
            public string MySqlConnection { get; set; }
            public string SqlConnection { get; set; }

        }

        public SqlConnection connection = null;
        public MySqlConnection MySqlConnection = null;

        public string connectionStringMySql;
        public string connectionStringSql;



        public ConnectionClass()
        {
            LoadConnectionStrings(); // Load connection strings when the object is created
        }

        public void LoadConnectionStrings()
        {
            // Read the configuration file
            string configFile = "config.json";
            string jsonString = File.ReadAllText(configFile);
            var config = JsonConvert.DeserializeObject<Config>(jsonString);

            // Retrieve the connection strings
            connectionStringMySql = config.MySqlConnection;
            connectionStringSql = config.SqlConnection;

        }
        public void OpenConnection()
        {
            try
            {
                connection = new SqlConnection(connectionStringSql);
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening connection: " + ex.Message);
            }
        }

        public void CloseConnection()
        {
            try
            {
                connection = new SqlConnection(connectionStringSql);
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error closing connection: " + ex.Message);
            }
        }

        public void OpenConnectionMysql()
        {
            try
            {
                MySqlConnection = new MySqlConnection(connectionStringMySql);
                MySqlConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening connection: " + ex.Message);
            }
        }

        public void CloseConnectionMysql()
        {
            try
            {
                MySqlConnection = new MySqlConnection(connectionStringMySql);
                MySqlConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error closing connection: " + ex.Message);
            }
        }


    }
}
