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 TempoMedioFechamentoDemandasController : 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 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
                        };

                    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 TempoMedioFechamentoDemandasService(new Repository<Demandas>(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] + "TempoMedioFechamentoDemandas/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                    viewModel.DashboardTitle = titulo;
                    viewModel.FontColor = fontColor;
                    viewModel.BackgroundColor = backgroundColor;

                    return View("Index", viewModel);
                }
                else
                {
                    TempoMedioFechamentoDemandasService tempoMedioFechamentoDemandas = new TempoMedioFechamentoDemandasService();
                    JsonResult rtn = tempoMedioFechamentoDemandas.GetFilter();
                    return rtn;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }

        public ActionResult Detalhamento(string id = "", string filtros = "")
        {
            try
            {
                IBaseViewModel viewModel;

                Filtro filtro = new Filtro(filtros);

                if (!filtro.ContemValores())
                    return new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

                DbContext context = new ContextEntity("ConnectionEntity");

                filtro.AddFiltro("codigo_sub_tipo_servico", id, String.Empty, Filtro.OPERADOR_IGUAL);
                Services service = new TempoMedioFechamentoDemandasService(new Repository<Demandas>(context), filtro);
                viewModel = (IBaseViewModel)service.GetDataDetalhamento();

                viewModel.id = id;
                viewModel.filtros = filtros;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "TempoMedioFechamentoDemandas/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];

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

    }
}
