using Common;
using Commonlib;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RestSharp;

namespace BLW_Log.Controllers
{
    public class EndPointMonitorController : Controller
    {
        public static string BaseUrl = "http://www.boonlive-rcu.com:7000/";
        public IActionResult Index()
        {
            return View();
        }
        public List<MonitorEndPoint> Query()
        {
            string Url = "api/Monitor/GetMonitorData";
            var options = new RestClientOptions(BaseUrl);
            var client = new RestClient(options);

            var request = new RestRequest(Url);

            string? hostlist = client.Post(request).Content;
            ReturnInfo info = JsonConvert.DeserializeObject<ReturnInfo>(hostlist ?? string.Empty);
            List<MonitorEndPoint> lll = JsonConvert.DeserializeObject<List<MonitorEndPoint>>(info.response?.ToString() ?? string.Empty);
            return lll;
        }


        [HttpPost()]
        public string SetData([FromBody] MonitorData ddd)
        {
            try
            {
                string Url = "api/Monitor/SetMonitor";
                var options = new RestClientOptions(BaseUrl);
                var client = new RestClient(options);
                var request = new RestRequest(Url);

                request.AddJsonBody(ddd);
                string? hostlist = client.Post(request).Content;
                return hostlist;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
