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);
            // FIC 測試機 1
            //var serverStr = ed.AESEncrypt("localhost");       MWAxcj1mgmbZ8tB6NgApnQ==
            //var portStr = ed.AESEncrypt("3306");              CY1x+1WYXRCBab3wKnBCOQ==
            // var databaseStr = ed.AESEncrypt("solar_master"); CEyYZnO8B5+yTXQcFSsiBA==
            //var rootStr = ed.AESEncrypt("root");              4koIJVzfp6veImONCwQy3A==
            //var passwordStr = ed.AESEncrypt("P@ssw0rd");      y4uPqlH9ncTgR/I07qpwaA==

            // FIC 測試機 2
           //var serverStr = ed.AESEncrypt("localhost"); // MWAxcj1mgmbZ8tB6NgApnQ==
           //var portStr = ed.AESEncrypt("3306");  // CY1x+1WYXRCBab3wKnBCOQ==
           //var databaseStr = ed.AESEncrypt("solar_master"); // CEyYZnO8B5+yTXQcFSsiBA==
           //var rootStr = ed.AESEncrypt("root"); // 4koIJVzfp6veImONCwQy3A==
           //var passwordStr = ed.AESEncrypt("P@ssw0rd"); // bOlWkSFsV3qNLkZTJ3UPog==
            // server=MWAxcj1mgmbZ8tB6NgApnQ==;port=CY1x+1WYXRCBab3wKnBCOQ==;database=CEyYZnO8B5+yTXQcFSsiBA==;user=pBX64+ALGFnLiHGRFXNh7w==;password=bOlWkSFsV3qNLkZTJ3UPog==;charset=utf8;Allow User Variables=True;

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

            this._connectionString = connStr;
        }


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