using fatd_faturamento.ENTIDADE;
using fatd_faturamento.BLL.Repositorio;
using System;
using System.Configuration;

namespace fatd_faturamento.UI.API.Service
{
    public class UserSecurityService
    {
        public bool Login(entidadeUsuarioAPI objUsuarioAPI)
        {
            bool response;
            string userBD = string.Empty;
            string passwordBD = string.Empty;
            string serverBD = string.Empty;
            string portBD = string.Empty;
            string tableBD = string.Empty;

            repositorioUsuarioAPI repositorioUsuarioAPI = null;

            try
            {
                response = false;

                repositorioUsuarioAPI = new repositorioUsuarioAPI();

                var vlist = repositorioUsuarioAPI.Select(objUsuarioAPI);

                if (vlist.Count != 0)
                {

                    foreach (entidadeUsuarioAPI objUsuarioAPIFor in vlist)
                    {
                        userBD = objUsuarioAPIFor.USER_BD;
                        passwordBD = objUsuarioAPIFor.PASSWORD_BD;
                        serverBD = objUsuarioAPIFor.SERVER_BD;
                        portBD = objUsuarioAPIFor.PORT_BD;
                        tableBD = objUsuarioAPIFor.TABLE_BD;
                    }

                    objUsuarioAPI.USER_BD = userBD;
                    objUsuarioAPI.PASSWORD_BD = passwordBD;
                    objUsuarioAPI.SERVER_BD = serverBD;
                    objUsuarioAPI.PORT_BD = portBD;
                    objUsuarioAPI.TABLE_BD = tableBD;

                    UpdateAppSettingsBD(objUsuarioAPI);

                    response = true;
                }

            }
            catch (Exception)
            {
                response = false;
            }

            return response;
        }

        public void UpdateAppSettings(string vChave, string vValor)
        {

            Configuration config = null;

            if (System.Web.HttpContext.Current != null)
            {
                config =
                    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            }
            else
            {
                System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = $"{System.AppDomain.CurrentDomain.BaseDirectory}Web.Config" };
                config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            }

            try
            {
                config.AppSettings.Settings.Remove(vChave);
                config.AppSettings.Settings.Add(vChave, vValor);
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }

            catch (Exception)
            {
                return;
            }

        }

        public void UpdateAppSettingsBD(entidadeUsuarioAPI objUsuarioAPI)
        {

            try
            {

                string[] nomeBD = objUsuarioAPI.TABLE_BD.Split('_');

                // FAT Update App.Config
                UpdateAppSettings("u_fat", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_fat", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_fat", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_fat", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_fat", objUsuarioAPI.TABLE_BD);

                // NSS Update App.Config
                UpdateAppSettings("u_nss", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_nss", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_nss", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_nss", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_nss", "NSS");

                // LinkCenter Update App.Config
                UpdateAppSettings("u_lc", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_lc", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_lc", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_lc", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_lc", "FATD_" + nomeBD[1].ToString());

                // Contabil Update App.Config
                UpdateAppSettings("u_ctb", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_ctb", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_ctb", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_ctb", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_ctb", "CTB");

                // Contabil Empresa Update App.Config
                UpdateAppSettings("u_ctbempresa", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_ctbempresa", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_ctbempresa", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_ctbempresa", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_ctbempresa", "CTB_" + nomeBD[1].ToString());

                // Cep NSS Update App.Config
                UpdateAppSettings("u_cepnss", objUsuarioAPI.USER_BD);
                UpdateAppSettings("p_cepnss", objUsuarioAPI.PASSWORD_BD);
                UpdateAppSettings("server_cepnss", objUsuarioAPI.SERVER_BD);
                UpdateAppSettings("port_cepnss", objUsuarioAPI.PORT_BD);
                UpdateAppSettings("table_cepnss", "CEP_NSS");

            }

            catch (Exception)
            {
                return;
            }

        }

    }
}