using Dashboard.Contexto.Data;
using Dashboard.Contexto.Models;
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.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 PassageirosPorFCVController : Controller
    {
        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
            {
                if (!String.IsNullOrEmpty(id))
                    return new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

                Filtro filtro = new Filtro(filtros);

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

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);

                IBaseViewModel viewModel = new BaseViewModel();
                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 = "Passageiros por FCV";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/GetData");
                viewModel.dados.Add("ViewAddresFCV", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/LoadFCV");
                viewModel.dados.Add("ViewAddresTerminal", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/LoadTerminal");
                viewModel.dados.Add("ViewAddresVeiculo", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/LoadVeiculo");
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/ExportExcel");

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

        public JsonResult LoadFCV(string firebird = "", string mysql = "", string Empresas = "", string dataInicio = "", string dataFim = "", string veiculos = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio + " 00:00:00", dataFim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculos", veiculos, String.Empty, Filtro.OPERADOR_SELECIONAR);
                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");

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<LinhasTerminais>(context), filtro);

                return Json(service.LoadFCV(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        public JsonResult LoadTerminal(string firebird = "", string mysql = "", string Empresas = "", string dataInicio = "", string dataFim = "", string fcvs = "", string veiculos = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio + " 00:00:00", dataFim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("fcvs", fcvs, String.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("veiculos", veiculos, String.Empty, Filtro.OPERADOR_SELECIONAR);
                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");

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);

                return Json(service.LoadTerminais(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        public JsonResult LoadVeiculo(string firebird = "", string mysql = "", string Empresas = "", string dataInicio = "", string dataFim = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio + " 00:00:00", dataFim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                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");

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);

                return Json(service.LoadVeiculos(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        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 fcv = "", string terminais = "", string veiculos = "")
        {
            try
            {
                IBaseViewModel viewModel;
                Filtro filtro = new Filtro();

                filtro.AddFiltro("data", dataInicio + " 00:00:00", dataFim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("fcvs", fcv, String.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("terminais", terminais, String.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("veiculos", veiculos, String.Empty, Filtro.OPERADOR_SELECIONAR);

                string strFiltro = filtro.ToString();

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

                if (!filtro.ContemValores())
                    return new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

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

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);
                viewModel = (IBaseViewModel)service.GetData();

                viewModel.id = id;
                viewModel.width = width;
                viewModel.height = height;
                viewModel.firebird = firebird;
                viewModel.mysql = mysql;
                viewModel.filtros = strFiltro;
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.DashboardTitle = titulo;
                viewModel.FontColor = fontColor;
                viewModel.BackgroundColor = backgroundColor;
                viewModel.Title = "Passageiros Por FCV";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/ExportExcel");
                viewModel.dados.Add("ViewAddressGetAddress", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PassageirosPorFCV/GetAddress");

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

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

                if (!String.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", Empresas.Replace("&quot;", "\""), 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");

                Services service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);

                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 Empresas = "")
        {
            try
            {
                Filtro filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));

                if (!String.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", Empresas.Replace("&quot;", "\""), 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");

                Services service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<LinhasTerminais>(context), filtro);

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

        public JsonResult GetAddress(string id = "", string firebird = "", string mysql = "", string latitude = "", string longitude = "")
        {
            try
            {
                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");

                PassageirosPorFCVService service = new PassageirosPorFCVService(new Repository<ApcEventos>(context), new Repository<RequisicoesGeocodeDiarias>(context));

                return new JsonResult()
                {
                    Data = service.getAddress(Convert.ToInt32(id), Convert.ToInt32(latitude), Convert.ToInt32(longitude)),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }
    }
}
