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

namespace RelatorioIntegracao.Controllers
{
    public class PontosDeParadaController : Controller
    {
        public ActionResult Index(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string filtros = "", string Empresas = "", string requisicaointerna = "", string titulo = "", 
                                  string fontColor = "", string backgroundColor = "", string Position = "")
        {
            try
            {
                /*Util.SetRequisicaoInterna = requisicaointerna.Trim().ToLower().Equals("1");
                if (String.IsNullOrEmpty(id))
                {
                    IBaseViewModel viewModel;
                    Filtro filtro = new Filtro(filtros);

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

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

                    Services service = new PontosDeParadaService(new Repository<PontosParada>(context), filtro);
                    viewModel = (IBaseViewModel)service.GetData();

                    viewModel.id = id;
                    viewModel.width = width;
                    viewModel.height = height;
                    viewModel.firebird = firebird;
                    viewModel.mysql = mysql;
                    viewModel.filtros = filtros;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/ExportPdf";
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/ExportExcel");
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                    viewModel.Empresas = Empresas;

                    viewModel.dados.Add("ViewAddressGetAddress", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/GetAddress");

                    return View("Index", viewModel);
                }
                else
                {
                    Filtro filtro = new Filtro(filtros);

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

                    JsonResult filter = new PontosDeParadaService(filtro).GetFilter();
                    return filter;
                }*/

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

                PontosDeParadaService service = new PontosDeParadaService(new Repository<PontosParada>(context), filtro, context);
                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 = "Pontos de Parada";
                viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                viewModel.Position = Position;
                viewModel.Requisicao = Util.GetRequisicaoInterna;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/ExportPdf";
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/ExportExcel");
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/GetData");
                viewModel.dados.Add("ViewAddressVeiculos", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/GetVeiculos");
                viewModel.dados.Add("ViewAddressGetAddress", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/GetAddress");

                return View("Params", viewModel);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }
        public JsonResult GetVeiculos(string firebird = "", string mysql = "", string Empresas = "", string datainicio = "", string datafim = "", string tipo_veiculo = "", string operacoes = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("tipo_veiculo", tipo_veiculo, null, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("operacoes", operacoes, null, 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");

                PontosDeParadaService service = new PontosDeParadaService(new Repository<PontosParada>(context), filtro, context);

                return Json(service.LoadVeiculos(tipo_veiculo, operacoes), 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 veiculos = "", string requisicao = "", string referencias = "")
        {
            try
            {
                IBaseViewModel viewModel;
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("chegada", datainicio, datafim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculo", veiculos, string.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("referencia", referencias, string.Empty, Filtro.OPERADOR_SELECIONAR);
                
                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");

                PontosDeParadaService service = new PontosDeParadaService(new Repository<PontosParada>(context), filtro, context);
                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 = "Pontos de Parada";
                    viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                    viewModel.Position = Position;
                    viewModel.Requisicao = requisicao;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[requisicao] + "PontosDeParada/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[requisicao] + "PontosDeParada/ExportExcel");
                    viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[requisicao] + "PontosDeParada/GetData");
                    viewModel.dados.Add("ViewAddressVeiculos", WebConfigurationManager.AppSettings[requisicao] + "PontosDeParada/Geticulos");
                    viewModel.dados.Add("ViewAddressGetAddress", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeParada/GetAddress");

                    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 Empresas = "", string Position = "", string datainicio = "", string datafim = "", string veiculos = "", string requisicao = "", 
                                    string referencias = "")
        {
            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);

                string strFiltro = filtro.ToString();
                filtro.AddFiltro("chegada", datainicio, datafim + " 23:59:59", Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculo", veiculos, string.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("referencia", referencias, 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");

                Services service = new PontosDeParadaService(new Repository<PontosParada>(context), filtro, context);

                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 = "", string Position = "", string datainicio = "", string datafim = "", string veiculos = "", string requisicao = "",
                                      string referencias = "")
        {
            try
            {
                Filtro filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));

                filtro.AddFiltro("chegada", datainicio, datafim, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculo", veiculos, string.Empty, Filtro.OPERADOR_SELECIONAR);
                filtro.AddFiltro("referencia", referencias, string.Empty, Filtro.OPERADOR_SELECIONAR);

                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 PontosDeParadaService(new Repository<PontosParada>(context), filtro, context);

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

                PontosDeParadaService service = new PontosDeParadaService(new Repository<PontosParada>(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;
            }
        }
    }
}
