using System.Data;
using System.ServiceModel;
using CreateLabel_ML.wsRedPack;
using MySqlConnector;
using Newtonsoft.Json;

namespace CreateLabel_ML
{
   public class mdlRedPack
   {
      public Guia Cotiza(Guia guia, string reference, string so)
      {
         var log = new clsLog();
         var cn =new clsConexion();
         var id = 0;
         var pin = string.Empty;
         var cliente = string.Empty;
         id = int.Parse(cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_ID'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString());
         pin = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_PIN'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         cliente = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_Cliente'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var folio = cn.TraeDataTable("SELECT folio FROM sale_order_folio WHERE estatus=1 AND carrier_id=6;", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var ws = new RedpackWSPortTypeClient(new BasicHttpsBinding(BasicHttpsSecurityMode.Transport), new EndpointAddress("https://ws.redpack.com.mx/RedpackAPI_WS/services/RedpackWS.RedpackWSHttpsSoap11Endpoint"));
         guia.moneda = cliente;
         guia.numeroDeGuia = folio;
         guia.flag = 0;
         guia.flagSpecified = true;
         guia.tipoEntrega = new IdDesc
         {
            id = 2,
            descripcion = string.Empty,
            idSpecified = true
         };
         guia.tipoEnvio = new IdDesc
         {
            id = 1,
            descripcion = string.Empty,
            idSpecified = true
         };
         guia.tipoIdentificacion = new IdDesc
         {
            id = 0,
            descripcion = string.Empty,
            idSpecified = true
         };
         var cotizacion = ws.cotizacionNacionalAsync(pin, id, new[]
         {
            guia
         }).Result;
         log.AgregaPcgRequest(JsonConvert.SerializeObject(guia), JsonConvert.SerializeObject(cotizacion.@return[0]), reference, so, "Creador .Net Cotizacion RedPack");
         return cotizacion.@return[0];
      }

      public Guia CreaGuia(Guia guia, string reference, string so)
      {
         var log = new clsLog();
         var cn =new clsConexion();
         var id = 0;
         var pin = string.Empty;
         var cliente = string.Empty;
         id = int.Parse(cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_ID'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString());
         pin = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_PIN'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         cliente = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_Cliente'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var folio = cn.TraeDataTable("SELECT folio FROM sale_order_folio WHERE estatus=1 AND carrier_id=6;", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var ws = new RedpackWSPortTypeClient(new BasicHttpsBinding(BasicHttpsSecurityMode.Transport), new EndpointAddress("https://ws.redpack.com.mx/RedpackAPI_WS/services/RedpackWS.RedpackWSHttpsSoap11Endpoint"));
         guia.numeroDeGuia = folio;
         guia.cotizaciones = null;
         guia.auxiliar = null;
         guia.resultadoConsumoWS = null;
         guia.tipoServicio = new IdDesc
         {
            id = 1,
            descripcion = string.Empty,
            idSpecified = true
         };
         var cotizacion = ws.documentacionAsync(pin, id, new[]
         {
            guia
         }).Result;
         log.AgregaPcgRequest(JsonConvert.SerializeObject(guia), JsonConvert.SerializeObject(cotizacion.@return[0]), reference, so, "Creador .Net RedPack");
         return cotizacion.@return[0];
      }
      public Guia Cobertura(Guia guia, string reference, string so)
      {
         var log = new clsLog();
         var cn = new clsConexion();
         var id = 0;
         var pin = string.Empty;
         var cliente = string.Empty;
         id = int.Parse(cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_ID'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString());
         pin = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_PIN'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         cliente = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name='RedPack_Cliente'", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var folio = cn.TraeDataTable("SELECT folio FROM sale_order_folio WHERE estatus=1 AND carrier_id=6;", new MySqlParameter[] { }, CommandType.Text).Rows[0][0].ToString();
         var ws = new RedpackWSPortTypeClient(new BasicHttpsBinding(BasicHttpsSecurityMode.Transport), new EndpointAddress("https://ws.redpack.com.mx/RedpackAPI_WS/services/RedpackWS.RedpackWSHttpsSoap11Endpoint"));
         var cotizacion = ws.cotizacionNacionalAsync(pin, id, new[]
         {
            guia
         }).Result;
         log.AgregaPcgRequest(JsonConvert.SerializeObject(guia), JsonConvert.SerializeObject(cotizacion.@return[0]), reference, so, "Creador .Net Guia RedPack");
         return cotizacion.@return[0];
      }
   }
}