using System.Configuration;
using System.Data;
using MySqlConnector;
using Newtonsoft.Json;
using RestSharp;

namespace CreateLabel_ML
{
   public class mdlIEnvios
   {
      public clsRespuestaIEnvios Cotiza(clsIEnvios envio, string reference, string so)
      {
         var log = new clsLog();
         var cn = new clsConexion();
         var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_iENVIOSQUOTE';", new MySqlParameter[] { }, CommandType.Text);
         var client = new RestClient(dt.Rows[0]["value"].ToString());
         var request = new RestRequest("", Method.POST, DataFormat.Json);
         request.AddHeader("Authorization", ConfigurationManager.AppSettings["AuthorizationIEnvios"]);
         request.AddJsonBody(envio);
         var response = client.Execute(request);
         log.AgregaPcgRequest(JsonConvert.SerializeObject(envio), JsonConvert.SerializeObject(response.Content), reference, so, "Creador .Net iEnvios");
         var respuesta = response.Content;
         var r = JsonConvert.DeserializeObject<clsRespuestaIEnvios>(respuesta);
         return r;
      }

      public clsRespuestaEnviaIEnvios CreaGuia(clsEnviaIEnvios envio, string reference, string so)
      {
         var log = new clsLog();
         var cn = new clsConexion();
         var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_iENVIOSLabel';", new MySqlParameter[] { }, CommandType.Text);
         var client = new RestClient(dt.Rows[0]["value"].ToString());
         var request = new RestRequest("", Method.POST, DataFormat.Json);
         request.AddHeader("Authorization", ConfigurationManager.AppSettings["AuthorizationIEnvios"]);
         request.AddJsonBody(envio);
         var response = client.Execute(request);
         log.AgregaPcgRequest(JsonConvert.SerializeObject(envio), JsonConvert.SerializeObject(response.Content), reference, so, "Creador .Net iEnvios");
         var respuesta = response.Content;
         var r = JsonConvert.DeserializeObject<clsRespuestaEnviaIEnvios>(respuesta);
         return r;
      }
   }
}
