using System;
using System.Collections.Generic;
using System.Data;
using MySqlConnector;
using Newtonsoft.Json;
using RestSharp;

namespace ActualizarEstatusPaqueteria
{
   public class clsElektra
   {
      public string access_token { get; set; }
      public string token_type { get; set; }
      public int expires_in { get; set; }
      public string scope { get; set; }
      public List<string> organization { get; set; }
      public string jti { get; set; }
   }
   public class mdlElektra
   {
      public string Token { get; set; }
      public string Login()
      {
         try
         {
            var cn = new clsConexion();
            var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_EK_LOGIN'", new MySqlParameter[] { }, CommandType.Text);
            var dr = dt.Rows[0];
            var client = new RestClient(dr["value"].ToString());
            var request = new RestRequest("", Method.POST, DataFormat.Json);
            request.AddHeader("Authorization", "Basic c2VsbGVyY2VudGVyY2xpZW50aWQ6dm5sNDBqNWIycXNieGkwbg==");
            var response = client.Execute(request);
            var respuesta = JsonConvert.DeserializeObject<clsElektra>(response.Content);
            Token = respuesta.access_token;
         }
         catch (Exception ex)
         {
            Token = "0";
         }
         return Token;
      }
      
      public void ConfirmDelivered(string pedido)
      {
         var respuesta = string.Empty;
         try
         {
            var client = new RestClient("https://elektra.mysellercenter.com/sellercenter/api/v1/orders/"+pedido+"/invoice/inv-"+pedido+"-0/tracking");
            var request = new RestRequest("", Method.PUT, DataFormat.Json);
            request.AddHeader("Authorization", "Bearer " + Token);
            var response = client.Execute(request);
            var r = response.Content;
         }
         catch (Exception ex)
         {
            throw ex;
         }
      }

   }
}
