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 ControleEnvioConsumoController : Controller
    {
        //
        // GET: /ControleEnvioConsumo/

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

                ControleEnvioConsumoService service = new ControleEnvioConsumoService(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 = "RPM x Tempo";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.Requisicao = Util.GetRequisicaoInterna;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ControleEnvioConsumo/ExportPdf";
                viewModel.dados.Add("ViewAddresExportJSon", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ControleEnvioConsumo/ExportJSon");
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "ControleEnvioConsumo/GetData");

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

        public JsonResult ExportJSon(string firebird = "", string mysql = "", string Empresas = "", string tipo = "", string datainicio = "", string datafim = "", string chave = "", string horainicio = "", string horafim = "",
            string diaSemana = "", string agrupamento = "")
        {
            try
            {
                if (chave.Equals("ControleConsumo"))
                {
                    Filtro filtro = new Filtro()    ;

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

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

                    ControleEnvioConsumoService service = new ControleEnvioConsumoService(context, filtro);

                    return Json(service.ExportJSon(), JsonRequestBehavior.AllowGet);
                }
                else
                    return null;
            }
            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 tipo = "", string datainicio = "", string datafim = "", string tipodata = "", string horainicio = "", string horafim = "", string diaSemana = "", string agrupamento = "", string requisicao = "", int? variacao = null,
            string tpVariacao = "", int? media = null, string tpMedia = "", string subtipo1 = "", string subtipo2 = "", string subtipo3 = "")
        {
            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", datainicio, datafim, tipodata);

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

                ControleEnvioConsumoService service = new ControleEnvioConsumoService(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 = Empresas;
                    viewModel.Position = Position;
                    viewModel.Requisicao = requisicao;
                    viewModel.ViewAddress = WebConfigurationManager.AppSettings[requisicao] + "ControleEnvioConsumo/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[requisicao];

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

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string tipo = "", string datainicio = "", string datafim = "", string tipodata = "", string horainicio = "", 
                                    string horafim = "", string diaSemana = "", string agrupamento = "", string requisicao = "", int? variacao = null, string tpVariacao = "", 
                                    int? 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("data", datainicio, datafim, tipodata);

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

                ControleEnvioConsumoService service = new ControleEnvioConsumoService(context, filtro);

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