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 ResumoColetaController : Controller
    {
        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
            {
                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", HttpUtility.HtmlDecode(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 ResumoColetaService(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] + "ResumoColeta/ExportPdf";
                    viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ResumoColeta/ExportExcel");
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];

                    return View("Index", viewModel);
                }
                else
                    return new ResumoColetaService().GetFilter();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

        public JsonResult ExportPdf(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(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(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 ResumoColetaService(context, filtro);

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

        public JsonResult ExportExcel(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(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));

                if (!string.IsNullOrEmpty(Empresas))
                    filtro.AddFiltro("empresas", HttpUtility.HtmlDecode(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 ResumoColetaService(context, filtro);

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