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

namespace RelatorioIntegracao.Controllers
{
    public class ResultadoGCEPTController : Controller
    {

        public GCEPTPeriodosService GetService(string mysql, string firebird, IFiltro filtro)
        {
            try
            {
                GCEPTPeriodosService service = null;
                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");

                service = new GCEPTPeriodosService(
                    new Repository<Dashboard.Contexto.Models.GCEPTPeriodos>(context),
                    new Repository<Dashboard.Contexto.Models.TelemetriaHrom>(context),
                    new Repository<Dashboard.Contexto.Models.AlarmesHromTelemetria>(context),
                    new Repository<Dashboard.Contexto.Models.Motoristas>(context),
                    new Repository<Dashboard.Contexto.Models.DescricaoAlarmesOperacao>(context),
                    new Repository<Dashboard.Contexto.Models.GCEPTCriterioDesempate>(context),
                    filtro);

                return service;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        public ActionResult Index(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string filtros = "", string Empresas = "", string requisicaointerna = "")
        {
            try
            {
                Filtro filtro = new Filtro(filtros);

                if (!string.IsNullOrEmpty(Empresas))
                {
                    Empresas = Empresas.Replace("&quot;", "\"");
                    filtro.AddFiltro("empresas", Empresas, String.Empty, Filtro.OPERADOR_LISTA);
                }

                if (!string.IsNullOrEmpty(id))
                    return new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

                filtro.AddFiltro("data", "", "", "hoje");

                var service = GetService(mysql, firebird, 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.Empresas = Empresas;
                viewModel.filtros = filtros;


                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResultadoGCEPT/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];

                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResultadoGCEPT/GetData");
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResultadoGCEPT/ExportExcel");
                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 = "", 
            int page = 0,
            string tipoRelatorio = "",
            string periodos = "",
            string kilometragemMinima = "",
            string pontuacaoMinima = "",
            string Empresas = "",
            string requisicao = "")
        {
            try
            {
                IBaseViewModel viewModel = null;
                Filtro filtro = new Filtro();

                if (!string.IsNullOrEmpty(Empresas))
                {
                    Empresas = Empresas.Replace("&quot;", "");
                    filtro.AddFiltro("empresas", Empresas, string.Empty, Filtro.OPERADOR_LISTA);
                }

                filtro.AddFiltro("periodos", periodos, string.Empty, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("tipoRelatorio", tipoRelatorio);

                if (!string.IsNullOrEmpty(kilometragemMinima))
                    filtro.AddFiltro("kilometragemMinima", kilometragemMinima, string.Empty, Filtro.OPERADOR_MAIOR);

                if (!string.IsNullOrEmpty(pontuacaoMinima))
                    filtro.AddFiltro("pontuacaoMinima", pontuacaoMinima, string.Empty, Filtro.OPERADOR_MAIOR);

                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");

                var service = new GCEPTPeriodosService(
                    new Repository<Dashboard.Contexto.Models.GCEPTPeriodos>(context),
                    new Repository<Dashboard.Contexto.Models.TelemetriaHrom>(context),
                    new Repository<Dashboard.Contexto.Models.AlarmesHromTelemetria>(context),
                    new Repository<Dashboard.Contexto.Models.Motoristas>(context),
                    new Repository<Dashboard.Contexto.Models.DescricaoAlarmesOperacao>(context),
                    new Repository<Dashboard.Contexto.Models.GCEPTCriterioDesempate>(context),
                    filtro);

                viewModel = (IBaseViewModel)service.GetData();

                if (viewModel != null)
                {
                    viewModel.mysql = mysql;
                    viewModel.filtros = strFiltro;
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.Empresas = Empresas;
                    viewModel.Requisicao = requisicao;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[requisicao] + "ResultadoGCEPT/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResultadoGCEPT/ExportExcel");

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

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string filtros = "", string requisicao = "")
        {
            try
            {
                var filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));
                var service = GetService(mysql, firebird, filtro);

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

        public JsonResult ExportExcel(string firebird = "", string mysql = "", string filtros = "", string requisicao = "")
        {
            try
            {
                var filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));
                var service = GetService(mysql, firebird, filtro);

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

