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 PontosDeHistoricoController : Controller
    {
        //
        // GET: /PontosDeHistorico/

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

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

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

                PontosDeHistoricoService service = new PontosDeHistoricoService(filtro, new Repository<ConfiguracaoMineracao>(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.filtros = filtros;
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.DashboardTitle = titulo;
                viewModel.FontColor = fontColor;
                viewModel.BackgroundColor = backgroundColor;
                viewModel.Title = "Relatório de Histórico";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/GetData");
                viewModel.dados.Add("ViewAddresSubtipo", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/LoadSubtipo");
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/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 = "", string titulo = "", string fontColor = "", string backgroundColor = "", string Empresas = "", string Position = "", int page = 0, string horaInicial = "", string horaFinal = "", string operacao = "", string tipo = "", string veiculo = "", string operacaoPagina = "", string tipo_relatorio = "", bool georeferenciar = false)
        {
            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("veiculo", veiculo, null, Filtro.OPERADOR_LISTA);
                filtro.AddFiltro("data", horaInicial, horaFinal, Filtro.OPERADOR_ENTRE);
                string strFiltro = filtro.ToString();

                PontosDeHistoricoService service = new PontosDeHistoricoService(filtro) { Georeferenciar = georeferenciar };
                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[Util.GetRequisicaoInterna];
                    viewModel.DashboardTitle = titulo;
                    viewModel.FontColor = fontColor;
                    viewModel.BackgroundColor = backgroundColor;
                    viewModel.Title = "Relatório de Histórico";
                    viewModel.Empresas = Empresas;
                    viewModel.Position = Position;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/ExportExcel");
                    viewModel.dados.Add("ViewAddressGetAddress", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "PontosDeHistorico/GetAddress");

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

        public JsonResult GetAddress(string id = "", string firebird = "", string mysql = "", string latitude = "", string longitude = "")
        {
            try
            {
                Filtro filtro = new Filtro();
                PontosDeHistoricoService service = new PontosDeHistoricoService(filtro);
                return new JsonResult()
                {
                    Data = service.GetAddress(Convert.ToDateTime(id), Convert.ToInt32(latitude), Convert.ToInt32(longitude)),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

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

                PontosDeHistoricoService service = new PontosDeHistoricoService(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);

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

    }
}
