using Dashboard.Dominio.Components;
using Dashboard.Dominio.Interfaces;
using Dashboard.Dominio.Servicos.Relatorios;
using Dashboard.Dominio.ViewModel;
using Infraestrutura.ElmahLog;
using System;
using System.Data.Entity;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace RelatorioIntegracao.Controllers
{
    public class RotasPlanejadasController : Controller
    {
        //
        // GET: /RotasPlanejadas/

        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
            {
                if (!string.IsNullOrEmpty(id))
                {
                    return new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                }

                Filtro filtro = new Filtro(filtros);

                var config = new ContextConfigService();
                var context = config.ConfigFirebird(firebird);

                RotasPlanejadasService service = new RotasPlanejadasService(filtro);
                IBaseViewModel viewModel = (IBaseViewModel)service.GetParams(context.Database.Connection.ConnectionString, Empresas);
                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 = "Rotas Planejadas";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.dados.Add("ViewAddressData", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "RotasPlanejadas/GetData");
                viewModel.dados.Add("ViewAddresSetoresRotas", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "RotasPlanejadas/LoadSetoresRotas");

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

        public ActionResult LoadSetoresRotas(string firebird = "", string mysql = "", string Empresas = "", string setores = "")
        {
            try
            {
                var filtro = new Filtro();
                var config = new ContextConfigService();
                var service = new RotasPlanejadasService(filtro);
                var context = config.ConfigFirebird(firebird);

                return Json(service.LoadSetoresRotas(context.Database.Connection.ConnectionString, setores), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }

        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 = "", int page = 0, string setoresrotas = "")
        {
            try
            {
                var filtro = new Filtro();
                filtro.AddFiltro("setoresrotas", setoresrotas, null, Filtro.OPERADOR_LISTA);

                var config = new ContextConfigService();
                var context = config.ConfigFirebird(firebird);
                var service = new RotasPlanejadasService(filtro, context.Database.Connection.ConnectionString) { Page = page };
                var viewModel = (IBaseViewModel)service.GetData();

                viewModel.id = id;
                viewModel.width = width;
                viewModel.height = height;
                viewModel.firebird = firebird;
                viewModel.mysql = mysql;
                viewModel.filtros = filtro.ToString();
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.DashboardTitle = titulo;
                viewModel.FontColor = fontColor;
                viewModel.BackgroundColor = backgroundColor;
                viewModel.Title = "Rotas Planejadas";
                viewModel.Empresas = Empresas;
                viewModel.Position = Position;
                viewModel.ViewAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "RotasPlanejadas/ExportPdf";
                viewModel.ServerAddress = WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna];
                viewModel.dados.Add("ViewAddressExcel", WebConfigurationManager.AppSettings[Util.GetRequisicaoInterna] + "RotasPlanejadas/ExportExcel");

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

        public JsonResult ExportPdf(string firebird = "", string mysql = "", string filtros = "", string Empresas = "")
        {
            try
            {
                var filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));
                var config = new ContextConfigService();
                var context = config.ConfigFirebird(firebird);

                RotasPlanejadasService service = new RotasPlanejadasService(filtro, context.Database.Connection.ConnectionString);
                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 = "")
        {
            try
            {
                var filtro = new Filtro(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(filtros)));
                var config = new ContextConfigService();
                var context = config.ConfigFirebird(firebird);

                RotasPlanejadasService service = new RotasPlanejadasService(filtro, context.Database.Connection.ConnectionString);
                return new JsonResult()
                {
                    Data = service.GenerateExcel(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return null;
            }
        }
    }
}
