using Dashboard.Contexto.Data;
using Dashboard.Dominio.Components;
using Dashboard.Dominio.Interfaces;
using Dashboard.Dominio.Servicos.Relatorios;
using Dashboard.Dominio.ViewModel;
using Infraestrutura.ElmahLog;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace RelatorioIntegracao.Controllers
{
    public class ContestacaoTempoAvaliacaoRespostaController : Controller
    {
        // GET: ContestacaoTempoAvaliacaoResposta
        public ActionResult Index(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string filtros = "", string titulo = "", string fontColor = "", string backgroundColor = "", string Empresas = "",
                                  string Position = "", string requisicaointerna = "")
        {
            try
            {
                Filtro filtro = new Filtro(filtros);

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(Empresas), string.Empty, Filtro.OPERADOR_LISTA);

                DbContext context;
                ContextConfigService config = new ContextConfigService();

                if (!string.IsNullOrEmpty(mysql.Replace(",", "")))
                    context = config.ConfigMySql(mysql);
                else if (!string.IsNullOrEmpty(firebird.Replace(",", "")))
                    context = config.ConfigFirebird(firebird);
                else
                    context = new ContextEntity("ConnectionEntity");

                ContestacaoTempoAvaliacaoRespostaService service = new ContestacaoTempoAvaliacaoRespostaService(context, filtro);
                IBaseViewModel viewModel = (IBaseViewModel)service.GetParams();
                Util.SetRequisicaoInterna = requisicaointerna.Trim().ToLower().Equals("1");

                viewModel.id = id;
                viewModel.width = width;
                viewModel.height = height;
                viewModel.firebird = firebird;
                viewModel.mysql = mysql;
                viewModel.filtros = filtros;
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.DashboardTitle = titulo;
                viewModel.FontColor = fontColor;
                viewModel.BackgroundColor = backgroundColor;
                viewModel.Title = "Contestação Pontos Justificados";
                viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                viewModel.Position = Position;
                viewModel.Requisicao = Util.GetRequisicaoInterna;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ContestacaoTempoAvaliacaoResposta/ExportPdf";
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ContestacaoTempoAvaliacaoResposta/ExportExcel");
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ContestacaoTempoAvaliacaoResposta/GetData");

                return View("Params", viewModel);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

        public ActionResult GetData(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string titulo = "", string fontColor = "", string backgroundColor = "", string Empresas = "", string Position = "",
                                    string datainicio = "", string datafim = "", string empresasRelatorio = "", string setores = "", string requisicao = "", string temporesposta = "")
        {
            try
            {
                IBaseViewModel viewModel;
                Filtro filtro = new Filtro();

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(Empresas), string.Empty, Filtro.OPERADOR_LISTA);

                filtro.AddFiltro("data", datainicio, datafim, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("empresasRelatorio", empresasRelatorio, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("setores", setores, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("temporesposta", temporesposta, string.Empty, Filtro.OPERADOR_IGUAL);

                string strFiltro = filtro.ToString();

                DbContext context;
                ContextConfigService config = new ContextConfigService();

                if (!string.IsNullOrEmpty(mysql.Replace(",", "")))
                    context = config.ConfigMySql(mysql);
                else if (!string.IsNullOrEmpty(firebird.Replace(",", "")))
                    context = config.ConfigFirebird(firebird);
                else
                    context = new ContextEntity("ConnectionEntity");

                ContestacaoTempoAvaliacaoRespostaService service = new ContestacaoTempoAvaliacaoRespostaService(context, filtro);
                service.setTotalWidth((int)width);
                viewModel = (IBaseViewModel)service.GetData();

                if (viewModel != null)
                {
                    viewModel.id = id;
                    viewModel.width = width;
                    viewModel.height = height;
                    viewModel.firebird = firebird;
                    viewModel.mysql = mysql;
                    viewModel.filtros = strFiltro;
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.DashboardTitle = titulo;
                    viewModel.FontColor = fontColor;
                    viewModel.BackgroundColor = backgroundColor;
                    viewModel.Title = "RPM x Tempo";
                    viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                    viewModel.Position = Position;
                    viewModel.Requisicao = requisicao;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[requisicao] + "ContestacaoTempoAvaliacaoResposta/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ContestacaoTempoAvaliacaoResposta/ExportExcel");

                    return PartialView("Index", viewModel);
                }
                return null;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

        public JsonResult ExportExcel(string firebird = "", string mysql = "", string datainicio = "", string datafim = "", string empresasRelatorio = "",
                                      string setores = "", string requisicao = "", string Empresas = "", string temporesposta = "", string login = "")
        {
            try
            {
                Filtro filtro = new Filtro();

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(Empresas), string.Empty, Filtro.OPERADOR_LISTA);

                filtro.AddFiltro("data", datainicio, datafim, Filtro.OPERADOR_ENTRE, "Período");
                filtro.AddFiltro("empresasRelatorio", empresasRelatorio, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("setores", setores, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("temporesposta", temporesposta, string.Empty, Filtro.OPERADOR_IGUAL);
                filtro.AddFiltro("login", login, string.Empty, Filtro.OPERADOR_IGUAL);

                DbContext context;
                ContextConfigService config = new ContextConfigService();

                if (!string.IsNullOrEmpty(mysql.Replace(",", "")))
                    context = config.ConfigMySql(mysql);
                else if (!string.IsNullOrEmpty(firebird.Replace(",", "")))
                    context = config.ConfigFirebird(firebird);
                else
                    context = new ContextEntity("ConnectionEntity");

                ContestacaoTempoAvaliacaoRespostaService service = new ContestacaoTempoAvaliacaoRespostaService(context, filtro);

                return new JsonResult()
                {
                    Data = service.GenerateExcel(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string datainicio = "", string datafim = "", string empresasRelatorio = "",
                                    string setores = "", string requisicao = "", string Empresas = "", string temporesposta = "", string login = "")
        {
            try
            {
                Filtro filtro = new Filtro();

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(Empresas), string.Empty, Filtro.OPERADOR_LISTA);

                filtro.AddFiltro("data", datainicio, datafim, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("empresasRelatorio", empresasRelatorio, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("setores", setores, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("temporesposta", temporesposta, string.Empty, Filtro.OPERADOR_IGUAL);
                filtro.AddFiltro("login", login, string.Empty, Filtro.OPERADOR_IGUAL);

                DbContext context;
                ContextConfigService config = new ContextConfigService();
                context = config.ConfigMySql(mysql);

                ContestacaoTempoAvaliacaoRespostaService service = new ContestacaoTempoAvaliacaoRespostaService(context, filtro);

                return new JsonResult()
                {
                    Data = service.GeneratePDF(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }
    }
}