using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data.OracleClient;
using System.Runtime.InteropServices;
using System.Net.Mail;
using System.Collections;

namespace Merchant_Email
{
    class Helper
    {
        public static string dbConnection()
        {
            string tempConnectionString;
            // mode = "l" live
            // mode = "a" archive
            string mode = "1";
            if (mode.Equals("l"))
                tempConnectionString = ConfigurationSettings.AppSettings["DBConnection"];
            else
                tempConnectionString = ConfigurationSettings.AppSettings["DBAConnection"];

            return decryptConnectionString(tempConnectionString);
        }

        private static string decryptConnectionString(string connectionString)
        {
            string encryptedUserName_BAse64 = connectionString.Substring(connectionString.ToLower().IndexOf("user id=") + 8, connectionString.ToLower().IndexOf(";password") - connectionString.ToLower().IndexOf("user id=") - 8);
            string encryptedPassword_BAse64 = connectionString.Substring(connectionString.ToLower().IndexOf(";password=") + 10);
            encryptedPassword_BAse64 = encryptedPassword_BAse64.Substring(0, encryptedPassword_BAse64.Length - 1);

            string encryptedUserName = DecryptLibrary.DecryptLibrary.DecryptData(encryptedUserName_BAse64);
            string encryptedPassword = DecryptLibrary.DecryptLibrary.DecryptData(encryptedPassword_BAse64);

            //System.Environment.SetEnvironmentVariable("Path", @"D:\published\recon\bin" + ";" + oldPath);

            StringBuilder Dec_username = new StringBuilder();
            rijndael_decrypt(encryptedUserName, Dec_username);
            string userName = Dec_username.ToString();

            StringBuilder Dec_password = new StringBuilder();
            rijndael_decrypt(encryptedPassword, Dec_password);
            string password = Dec_password.ToString();

            string tempConnectionString = connectionString.Replace(encryptedUserName_BAse64, userName);
            tempConnectionString = tempConnectionString.Replace(encryptedPassword_BAse64, password);

            return tempConnectionString;
        }

        //private static string decryptConnectionString(string strEncryptedString)
        //{
        //    string src = DecryptLibrary.DecryptLibrary.DecryptData(strEncryptedString);
        //    StringBuilder string_filled_in_dll = new StringBuilder();
        //    EJCSLoader.rijndael_decrypt(src, string_filled_in_dll);
        //    return string_filled_in_dll.ToString();
        //}

        [DllImport("EncDecDLL.dll")]
        private static extern void rijndael_decrypt(string src, StringBuilder string_filled_in_dll);

        public static bool OracleError(string strError)
        {
            bool error = false;
            if (strError.Contains("ORA-"))
                error = true;
            else
                error = false;
            return error;
        }
    }
}
