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 ChegadasRealizadasController : Controller
    {
        //
        // GET: /ChegadasRealizadas/

        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(id))
                    return new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

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

                filtro.AddFiltro(campo: "data", operador: Filtro.OPERADOR_HOJE);

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

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(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.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];

                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/GetData");
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/ExportExcel");
                viewModel.dados.Add("ViewAddresTerminal", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/GetTerminais");
                viewModel.dados.Add("ViewAddresLinhas", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/GetLinhas");

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

        public JsonResult GetTerminais(string firebird = "", string mysql = "", string Empresas = "", string horaInicial = "", string horaFinal = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", horaInicial, horaFinal, 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");

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(context), filtro);

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

        public JsonResult GetLinhas(string firebird = "", string mysql = "", string Empresas = "", string horaInicial = "", string horaFinal = "", string terminal = "")
        {
            try
            {
                Filtro filtro = new Filtro();

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

                filtro.AddFiltro("data", horaInicial, horaFinal, Filtro.OPERADOR_ENTRE);
                filtro.AddFiltro(campo: "terminal", valor1: terminal, operador: 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");

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(context), filtro);

                return Json(service.LoadLinhas(), 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 = "", int page = 0, string horaInicial = "", string horaFinal = "", string terminal = "", string linhas = "")
        {
            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(campo: "data", valor1: horaInicial + ":00", valor2: horaFinal + ":59", operador: Filtro.OPERADOR_ENTRE, nome: "Data");
                filtro.AddFiltro(campo: "terminal", valor1: terminal, operador: Filtro.OPERADOR_IGUAL, nome: "Terminal");
                filtro.AddFiltro(campo: "linhas", valor1: linhas, operador: Filtro.OPERADOR_LISTA, nome: "Linhas");

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

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(context), filtro);
                service.page = page;
                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 = "Chegadas Realizadas";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ChegadasRealizadas/ExportExcel");

                return PartialView("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");

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(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");

                ChegadasRealizadasService service = new ChegadasRealizadasService(new Repository<LinhasTerminaisOriginais>(context), filtro);

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