using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;
using SolarPower.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace SolarPower.Helper
{

    /// <summary>
    /// Database 介面
    /// </summary>
    public interface IDatabaseHelper
    {
        /// <summary>
        /// 取得連線
        /// </summary>
        /// <returns></returns>
        IDbConnection GetConnection();
    }

    public class DatabaseHelper : IDatabaseHelper
    {
        private readonly string _connectionString;

        public DatabaseHelper(DBConfig dbConfig)
        {
            EDFunction ed = new EDFunction();

            var serverStr = ed.AESDecrypt(dbConfig.Server);
            var portStr = ed.AESDecrypt(dbConfig.Port);
            var databaseStr = ed.AESDecrypt(dbConfig.Database);
            var rootStr = ed.AESDecrypt(dbConfig.Root);
            var passwordStr = ed.AESDecrypt(dbConfig.Password);

            var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;";

            this._connectionString = connStr;
        }

        public IDbConnection GetConnection()
        {
            var conn = new MySqlConnection(this._connectionString);
            return conn;
        }
    }
}
