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.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace RelatorioIntegracao.Controllers
{
    public class ConvocacoesController : Controller
    {
        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", DateTime.Now.ToString("dd/MM/yyyy"), DateTime.Now.ToString("dd/MM/yyyy"), Filtro.OPERADOR_ENTRE);
                ContextConfigService config = new ContextConfigService();
                var context = config.ConfigMySql(mysql);

                ConvocacoesService service = new ConvocacoesService(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.Empresas = Empresas;
                viewModel.filtros = filtros;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.dados.Add("ViewAddressVeiculos", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/LoadVeiculos");
                viewModel.dados.Add("ViewAddressMotoristas", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/LoadMotoristas");
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/GetData");
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/ExportExcel");
                return View("Params", viewModel);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

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

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

                filtro.AddFiltro("data", dataInicio, dataFim, Filtro.OPERADOR_ENTRE);

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

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

                ConvocacoesService service = new ConvocacoesService(context, filtro);
                return Json(service.LoadVeiculos("todos", ""));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        public JsonResult LoadMotoristas(string firebird = "", string mysql = "", string Empresas = "", string operacoes = "", string dataInicio = "", string dataFim = "", string veiculos = "", int codigoUsuarioDashboard = 0)
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio, dataFim, Filtro.OPERADOR_ENTRE);

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

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

                ConvocacoesService service = new ConvocacoesService(context, filtro);
                return Json(service.LoadMotoristas(codigoUsuarioDashboard), 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 empresas  = "", string dataInicio = "", string dataFim = "", string veiculos = "", 
                                    string motoristas = "", string tiporelatorio = "")
        {
            try
            {
                IBaseViewModel viewModel;
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio, dataFim, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculos", veiculos, "", Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("motoristas", motoristas, "", Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("tiporelatorio", tiporelatorio, "", Filtro.OPERADOR_IGUAL);

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

                ConvocacoesService service = new ConvocacoesService(context, filtro);
                viewModel = (IBaseViewModel)service.GetData();
                viewModel.id = id;
                viewModel.width = width;
                viewModel.height = height;
                viewModel.firebird = firebird;
                viewModel.mysql = mysql;
                viewModel.filtros = filtro.ToString();
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "Convocacoes/ExportExcel");
                return View("Index", viewModel);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string empresas = "", string dataInicio = "", string dataFim = "", string veiculos = "",
                                    string motoristas = "", string tiporelatorio = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", dataInicio, dataFim, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro("veiculos", veiculos, "", Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("motoristas", motoristas, "", Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("tiporelatorio", tiporelatorio, "", Filtro.OPERADOR_IGUAL);

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

                ConvocacoesService service = new ConvocacoesService(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 = "")
        {
            try
            {
                Filtro filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));
                ContextConfigService config = new ContextConfigService();
                var context = config.ConfigMySql(mysql);

                PontosDeHistoricoService service = new PontosDeHistoricoService(filtro, new Repository<ConfiguracaoMineracao>(context));
                return new JsonResult()
                {
                    Data = service.GenerateExcel(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }
    }
}