using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Hosting;

namespace boilerplate.Models
{
    public class SmsManagment
    {
        bool sendSms = bool.Parse(ConfigurationManager.AppSettings["SmsSend"]);
        string fromNum = ConfigurationManager.AppSettings["SmsFromNumber"];
        string user = ConfigurationManager.AppSettings["SmsUsername"];
        string pass = ConfigurationManager.AppSettings["SmsPassword"];

        public void SendSms(List<KeyValuePair<string, string>> KeyValue, string[] Numbers, string Pattern)
        {
            if (!sendSms) return;
            try
            {

                JObject inputData = new JObject();
                for (int i = 0; i < KeyValue.Count; i++)
                {
                    inputData.Add(KeyValue[i].Key, KeyValue[i].Value);
                }
                foreach (var Number in Numbers)
                {
                    // string res = new smsserver().sendPatternSms(fromNum, new string[] { Number }, user, pass, Pattern, data);
                    var options = new RestClientOptions("https://ippanel.com") { MaxTimeout = -1, };
                    var client = new RestClient(options);
                    var request = new RestRequest("/api/select", RestSharp.Method.Post);
                    request.AddHeader("cache-control", "no-cache");
                    request.AddHeader("Content-Type", "application/json");
                    var Data = JsonConvert.SerializeObject(new SmsSendModel()
                    {
                        op = "pattern",
                        user = user,
                        pass = pass,
                        fromNum = fromNum,
                        toNum = Number,
                        patternCode = Pattern,
                        inputData = new JArray(inputData)
                    });
                    request.AddStringBody(Data, DataFormat.Json);
                    var res = client.Execute(request);

                }


            }
            catch { }
        }
    }
}