using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using OpenAuth.App;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using Org.BouncyCastle.Ocsp;
using Yitter.IdGenerator;
using Infrastructure.Helpers;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 举报信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DroneClueReportingController : ControllerBase
    {
        DroneClueReportingApp clueReportingApp;
        public DroneClueReportingController(DroneClueReportingApp clueReportingApp)
        {
            this.clueReportingApp = clueReportingApp;
        }

        /// <summary>
        /// 举报查询
        /// </summary>
        /// <param name="content"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Response<PageInfo<List<DroneClueReporting>>> QueryClueReporting(string content, string beginDate, string endDate, int page, int limit)
        {
            var response = new Response<PageInfo<List<DroneClueReporting>>>();
            try
            {
                response.Result = clueReportingApp.QueryClueReporting(content, beginDate, endDate, page, limit);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取摄像头信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<List<dynamic>> GetCamerInfos()
        {
            var response = new Response<List<dynamic>>();
            try
            {
                response.Result = clueReportingApp.GetCamerInfos();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 通过海康平台添加摄像头信息
        /// </summary>
        /// <param name="rtuid">配置信息id</param>
        /// <param name="url">平台地址</param>
        /// <param name="appkey"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddCameraInfos(long rtuid, string url, string appkey, string secret)
        {
            Response<bool> response = new Response<bool>();

            try
            {
                string path = "/artemis/api/resource/v1/cameras";
                var obj = new
                {
                    pageNo = 1,
                    pageSize = 1000
                };
                string body = JsonConvert.SerializeObject(obj);


                var result = HIKOpenAPI.HttpPost(url, path, body, appkey, secret);
                var res = JsonConvert.DeserializeObject<JObject>(result);
                List<DroneCameraInfo> cflist = new List<DroneCameraInfo>();
                foreach (var item in res["data"]["list"])
                {
                    DroneCameraInfo cf = new DroneCameraInfo();
                    cf.deviceId = YitIdHelper.NextId();
                    cf.deviceNum = item["cameraIndexCode"].ToString();
                    cf.deviceName = item["cameraName"].ToString();
                    cf.createTime = DateTime.Now;
                    cf.rtuId = rtuid;
                    cf.address = item["installLocation"].ToString();
                    if (item["longitude"].ToString() == "")
                    {
                        cf.lnglat = "";
                    }
                    else
                    {
                        cf.lnglat = item["longitude"].ToString() + "," + item["latitude"].ToString();
                    }
                    if (item["status"].ToString() == "")
                    {
                        cf.isonline = null;
                    }
                    else
                    {
                        if ((int)item["status"] == 1)
                        {
                            cf.isonline = true;
                        }
                        else
                        {
                            cf.isonline = false;
                        }
                    }
                    cflist.Add(cf);
                }

                response.Result = clueReportingApp.AddCameraInfo(cflist);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 根据坐标点获取摄像头信息
        /// </summary>
        /// <param name="lng">经度</param>
        /// <param name="lat">维度</param>
        /// <param name="distince">距离</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<dynamic>> GetCamerInfoByLngLat(string lng, string lat, double distince)
        {
            var response = new Response<List<dynamic>>();
            try
            {
                response.Result = clueReportingApp.GetCamerInfoByLngLat(lng, lat, distince);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
    }
}
