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 Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace RelatorioIntegracao.Controllers
{
    public class ResumoHodometroController : Controller
    {
        // GET: ResumoHodometro
        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
            {
                Filtro filtro = new Filtro(filtros);

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

                filtro.AddFiltro("data", string.Empty, string.Empty, Filtro.OPERADOR_MES);

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

                ResumoHodometroService service = new ResumoHodometroService(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.filtros = filtros;
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.DashboardTitle = titulo;
                viewModel.FontColor = fontColor;
                viewModel.BackgroundColor = backgroundColor;
                viewModel.Title = "Resumo Hodometro Mensal";
                viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                viewModel.Position = Position;
                viewModel.Requisicao = Util.GetRequisicaoInterna;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/ExportPdf";
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/ExportExcel");
                viewModel.dados.Add("ViewAddresVeiculos", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/LoadVeiculos");
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/GetData");

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

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

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

                filtro.AddFiltro("data", data, null, Filtro.OPERADOR_IGUAL);

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

                ResumoHodometroService service = new ResumoHodometroService(context, filtro);

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

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

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

                filtro.AddFiltro("data", data, string.Empty, Filtro.OPERADOR_IGUAL);
                filtro.AddFiltro("veiculos", veiculos, string.Empty, Filtro.OPERADOR_LISTA);
                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");

                ResumoHodometroService service = new ResumoHodometroService(context, filtro);
                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 = "RPM x Tempo";
                    viewModel.Empresas = HttpUtility.HtmlDecode(Empresas);
                    viewModel.Position = Position;
                    viewModel.Requisicao = requisicao;
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/ExportPdf";
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/ExportExcel");
                    viewModel.dados.Add("ViewAddresVeiculos", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/LoadVeiculos");
                    viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoHodometro/GetData");

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

        public JsonResult ExportExcel(string firebird = "", string mysql = "", string tipo = "", string datainicio = "", string datafim = "", string tipodata = "", string horainicio = "",
                            string horafim = "", string diaSemana = "", string agrupamento = "", string requisicao = "", float? variacao = null, string tpVariacao = "",
                            float? media = null, string tpMedia = "", string subtipo1 = "", string subtipo2 = "", string subtipo3 = "", string Empresas = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("tipo", tipo, string.Empty, Filtro.OPERADOR_IGUAL, "Tipo");
                filtro.AddFiltro("agrupamento", agrupamento, string.Empty, Filtro.OPERADOR_IGUAL, "Agrupamento");
                filtro.AddFiltro("data", datainicio, datafim, tipodata, "Data");
                filtro.AddFiltro("diaSemana", diaSemana, string.Empty, Filtro.OPERADOR_LISTA, "Dia Semana");
                filtro.AddFiltro("hora", horainicio, horafim, Filtro.OPERADOR_ENTRE, "Hora");
                filtro.AddFiltro("subtipo1", subtipo1, string.Empty, Filtro.OPERADOR_LISTA, "SubTipo1");
                filtro.AddFiltro("subtipo2", subtipo2, string.Empty, Filtro.OPERADOR_LISTA, "SubTipo2");
                filtro.AddFiltro("subtipo3", subtipo3, string.Empty, Filtro.OPERADOR_LISTA, "SubTipo3");
                filtro.AddFiltro("variacao", variacao.ToString(), string.Empty, tpVariacao, "Variação");
                filtro.AddFiltro("media", media.ToString(), string.Empty, tpMedia, "Média");

                DbContext context;
                ContextConfigService config = new ContextConfigService();
                context = config.ConfigMySql(mysql);
                ConsumoService service = new ConsumoService(context, filtro);

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

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string data = "", string Empresas = "", string veiculos = "", string requisicao = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", data, string.Empty, Filtro.OPERADOR_IGUAL);
                filtro.AddFiltro("veiculos", veiculos, string.Empty, Filtro.OPERADOR_LISTA);
                string strFiltro = filtro.ToString();


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

                ResumoHodometroService service = new ResumoHodometroService(context, filtro);

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