using Dashboard.Contexto.Data;
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 TacografoDigitalController : Controller
    {
        // GET: TacografoDigital
        public ActionResult Index(string id = "", int? width = null, int? height = null, string firebird = "", string mysql = "", string filtros = "", string titulo = "", string fontColor = "", string backgroundColor = "", string Position = "", string requisicaointerna = "", string Empresas = "")
        {
            try
            {
                Util.SetRequisicaoInterna = requisicaointerna.Trim().ToLower().Equals("1");

                if (String.IsNullOrEmpty(id))
                {
                    IBaseViewModel viewModel;
                    Filtro filtro = new Filtro(filtros);

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

                    // 10.150.0.100 - /inlog/db/firebird/avls_marechal_2019_06_25.fdb
                    //firebird = "10.150.0.100,/inlog/db/firebird/avls_marechal_2019_06_25.fdb,SYSDBA,,masterkey";
                    DbContext context;
                    ContextConfigService config = new ContextConfigService();

                    if (!String.IsNullOrEmpty(firebird.Replace(",", "")))
                        context = config.ConfigFirebird(firebird);
                    else
                        context = new ContextEntity("ConnectionEntity");

                    TacografoDigitalService service = new TacografoDigitalService(context, filtro);
                    service.setTotalWidth((int)width);
                    service.setGraphID(Position);
                    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] + "TacografoDigital/ExportPdf";
                    viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                    viewModel.DashboardTitle = titulo;
                    viewModel.FontColor = fontColor;
                    viewModel.BackgroundColor = backgroundColor;
                    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);

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

                    TacografoDigitalService tacografoDigital = new TacografoDigitalService(context, filtro);
                    JsonResult rtn = tacografoDigital.GetFilter();
                    return rtn;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return View("../Error/_404");
            }
        }
    }
}