using System.Collections.Generic;
using System.Configuration;
using System.Data;
using MySqlConnector;
using Newtonsoft.Json;
using RestSharp;
using System.IO;
using System.Threading;
using System.Net;
using System;


namespace CreateLabel_ML
{ 

public class mdl99min
    {
        public AttributesRespuestaCotizacion99Minutos Cotiza(ClsCotizacion99Minutos envio, string reference, string so)
        {
            var log = new CreateLabel_ML.clsLog();
            var respuesta="";
            var cn = new CreateLabel_ML.clsConexion();
            try
            { 
            var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS';", new MySqlParameter[] { }, CommandType.Text);
            var dt2 = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS_APIKEY';", new MySqlParameter[] { }, CommandType.Text);
            var client = new RestClient(dt.Rows[0]["value"].ToString() + "shipping/rates");
            var api_key = dt2.Rows[0]["value"].ToString();
            var request = new RestRequest("", Method.POST, DataFormat.Json);
            api_key = "Bearer " + api_key;
            request.AddHeader("Authorization", api_key);
            request.AddHeader("cache-control", "no-cache");
            request.AddJsonBody(JsonConvert.SerializeObject(envio));
                
            var response = client.Execute(request);
            log.AgregaPcgRequest(JsonConvert.SerializeObject(envio), JsonConvert.SerializeObject(response.Content), reference, so, "Creador .Net 99Minutos");
            respuesta = response.Content;
            var r = JsonConvert.DeserializeObject<CreateLabel_ML.AttributesRespuestaCotizacion99Minutos>(respuesta);
            return r;
            }
            catch (Exception ex)
            {
                log.EscribeLog("Error al consumir el ws de 99 Minutos o Sin cubertura, respuesta ws: " + respuesta.ToString() + " error:" + ex.Message.ToString());
                return null;
            }


        }

        public AttributesRespuestaEtiqueta99Minutos CreaGuia(ClsEnvia99Minutos envio, string reference, string so)
        {
            var log = new CreateLabel_ML.clsLog();
            var respuesta = "";
            var cn = new CreateLabel_ML.clsConexion();
            try
            {
                var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS';", new MySqlParameter[] { }, CommandType.Text);
                var dt2 = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS_APIKEY';", new MySqlParameter[] { }, CommandType.Text);
                var client = new RestClient(dt.Rows[0]["value"].ToString() + "autorization/order");
                var api_key = dt2.Rows[0]["value"].ToString();
                var request = new RestRequest("", Method.POST, DataFormat.Json);
                envio.Apikey = api_key;
                request.AddHeader("cache-control", "no-cache");
                request.AddJsonBody(JsonConvert.SerializeObject(envio));
                var response = client.Execute(request);
                log.AgregaPcgRequest(JsonConvert.SerializeObject(envio), JsonConvert.SerializeObject(response.Content), reference, so, "Creador .Net 99Minutos");
                respuesta = response.Content;
                var r = JsonConvert.DeserializeObject<AttributesRespuestaEtiqueta99Minutos>(respuesta);
                return r;
            }
            catch (Exception ex)
            {
                log.EscribeLog("Error al consumir el ws de 99 Minutos o Sin cubertura o Error al crear la Guia, respuesta ws: " + respuesta.ToString() + " error:" + ex.Message.ToString());
                return null;
            }
        }


    public CreateLabel_ML.AttributesRespuestaEtiquetaPdf99Minutos CreaPDF(CreateLabel_ML.ClsEnvioPdf99Minutos envio, string reference, string so)
    {
        var log = new CreateLabel_ML.clsLog();
        var respuesta = "";
        var cn = new CreateLabel_ML.clsConexion();
        try
        {
            var dt = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS';", new MySqlParameter[] { }, CommandType.Text);
            var dt2 = cn.TraeDataTable("SELECT value FROM m_configuration WHERE name = 'ENDPOINT_99MINUTOS_APIKEY';", new MySqlParameter[] { }, CommandType.Text);
            var client = new RestClient(dt.Rows[0]["value"].ToString() + "guide/order");
            var api_key = dt2.Rows[0]["value"].ToString();
            var request = new RestRequest("", Method.POST, DataFormat.Json);
            api_key = "Bearer " + api_key;
            request.AddHeader("Authorization", api_key);
            var strParams = "{" + "\"counter\": [" + envio.Counter + "],\"base64\":true,\"size\": \"zebra\",\"orientation\": \"vertical\"}";
            request.AddJsonBody(strParams);
            var response = client.Execute(request);
            log.AgregaPcgRequest(JsonConvert.SerializeObject(envio), JsonConvert.SerializeObject(response.Content), reference, so, "Creador .Net 99Minutos");
            respuesta = response.Content;
            var r = JsonConvert.DeserializeObject<CreateLabel_ML.AttributesRespuestaEtiquetaPdf99Minutos>(respuesta);
            return r;
        }
        catch (Exception ex)
        {
            log.EscribeLog("Error al consumir el ws de 99 Minutos o Sin cubertura o Error al crear la Guia, respuesta ws: " + respuesta.ToString() + " error:" + ex.Message.ToString());
            return null;
        }
    }
}

}