using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Com.FirmLib.Entity;
using FirmHttpDao;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Com.FirmLib.Bll
{
    public class ProjectBll : BaseHttpBll
    {
        private ProjectBll() : base(@"")
        {

        }
        /// <summary>
        /// 获取基础地址
        /// </summary>
        /// <returns></returns>
        protected override string BaseEndUri()
        {
            return BllHttpSetting.Instance.DpUri;
        }
        /// <summary>
        /// 获取项目信息，通过项目Id
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public ProjectItem GetProjectItem(string projectId)
        {
            string tempUrlExtension = BaseEndUri() + @"/project/names/query";
            List<ProjectItem> items = new List<ProjectItem>();
            JObject jObject = new JObject();
            JArray jArray = new JArray(projectId);
            jArray.Add(projectId);
            jObject.Add("project", jArray);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                JArray array = JArray.Parse(listStr);
                foreach (var item in array)
                {
                    JObject tempJo = (JObject)item;
                    if (tempJo != null)
                    {
                        ProjectItem projectItem = new ProjectItem();
                        projectItem.Id = tempJo["id"].ToString();
                        projectItem.ProjectNo = projectItem.Id;
                        projectItem.Name = tempJo["name"].ToString();
                        items.Add(projectItem);
                    }
                }
                //items = JsonConvert.DeserializeObject<List<ProjectItem>>(listStr);
            });
            return items.FirstOrDefault();
        }
        /// <summary>
        /// 项目信息同步
        /// </summary>
        /// <param name="projectId"></param>
        public void ProjectSync(string projectId)
        {
            string tempUrlExtension = BaseEndUri() + @"/project/sync";
            JObject jObject = new JObject();
            JArray jArray = new JArray();
            jArray.Add(projectId);
            jObject.Add("project", jArray);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            //不需要等待处理结果
            //HandlerResult(result, null,null);//处理result变成同步
        }
        /// <summary>
        /// 获取项目密码
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public string GetProjectSecret(string projectId)
        {
            string url = BllHttpSetting.Instance.EquipmentUri +
                         $"/mng/project/query?projectId={projectId}&secret=A123456";
            var items = new List<PlatformProjectItem>();
            HttpClient client = HttpClientManager.CreateClient();
            var result = client.GetAsync(new Uri(url, UriKind.RelativeOrAbsolute));
            var stringResult = HandlerTaskResponse(result, new RequestInfo(url));
            HandlerResult(stringResult, (jo) =>
            {
                var listStr = jo["Content"].ToString();
                items = JsonConvert.DeserializeObject<List<PlatformProjectItem>>(listStr);
            });
            return (items.FirstOrDefault()?.password)??string.Empty;
        }
  
    }

    #region 相关基础信息

    public class PlatformProjectItem
    {
        /// <summary>
        /// 项目Id
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 项目密码
        /// </summary>
        public string password { get; set; }
    }

    #endregion
}

