using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Narvaling.monitor.service
{
    internal class conexionService
    {
        private static int GetPermisoSRV(string sURL)
        {
            var resf = -1;
            
            try
            {
                HttpClient client = new HttpClient();
                int.Parse(ConfigurationManager.AppSettings["tiempoComprovacion"]);
                var nombre = System.Environment.MachineName;
                client.BaseAddress = new Uri(sURL);
                client.DefaultRequestHeaders.Accept.Clear();
                var res = client.GetAsync(
                    "?nombre="+ nombre + "&mei=" + IdService.GetMEI()).Result;
                
                if (res.IsSuccessStatusCode)
                {
                    if (res.StatusCode == HttpStatusCode.OK)
                    {
                        var jsonResponse = res.Content.ReadAsStringAsync().Result;
                        if (jsonResponse != null && jsonResponse.Contains("OK"))
                            resf=1;
                        else
                            resf= 0;
                    }
                }
                client.Dispose();
                res.Dispose();
            }
            catch (Exception e)
            {
                var ex = e.ToString();
                return -1;
            }


            return resf;
        }
        public static int ConectLanSRV()
        {
            try
            {

                var sURL = ConfigurationManager.AppSettings["url"];
                return GetPermisoSRV(sURL);
            }
            catch(Exception e)
            {
                var ex = e.ToString();
                return -1;
            }
        }

        public static int ConectExternSRV()
        {
            try
            {
                var sURL = ConfigurationManager.AppSettings["urlExtern"];
                return GetPermisoSRV(sURL);
            }
            catch (Exception e)
            {
                var ex = e.ToString();
                return -1;
            }
        }
    }
}
