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 VisitasVeiculoController : Controller
    {
        public ActionResult Index(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string filtros = "", string Empresas = "", string requisicaointerna = "")
        {
            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 VisitasVeiculoService(new Repository<Visitas>(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] + "VisitasVeiculo/ExportPdf";
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "VisitasVeiculo/ExportExcel");
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                    viewModel.Empresas = Empresas;

                    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 VisitasVeiculoService(filtro).GetFilter();
                    return filter;
                }
            }
            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");

                Services service = new VisitasVeiculoService(new Repository<Visitas>(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");

                Services service = new VisitasVeiculoService(new Repository<Visitas>(context), filtro);

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