using Com.FirmLib.Common;
using Com.FirmLib.Entity;
using FirmHttpDao;
using FWindSoft.Net.Http;
using Newtonsoft.Json.Linq;
using SAGA.DotNetUtils.Service;
using SAGA.RevitUtils.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.Bll
{
    /// <summary>
    /// 设备信息点问题
    /// </summary>
    public class EquipmentInfoPointBll : BaseHttpBll
    {
        private EquipmentInfoPointBll() : base(@"")
        {

        }
        /// <summary>
        /// 获取信息点定义信息
        /// </summary>
        /// <returns></returns>
        public List<EquipmentInfoPointItem> GetPointInfoDefinitions(string code)
        {
            return SaasSysOrEqInfos(code);
        }

        public List<EquipmentInfoPointItem> SaasSysOrEqInfos(string code, string type = "equip")
        {
            List<EquipmentInfoPointItem> items = new List<EquipmentInfoPointItem>();
            string url = BllHttpSetting.Instance.EquipmentUri + $"/infocode/query_property?type=" + code;
            //string app_type = "godhand";
            //JObject jObject = new JObject();
            //jObject.Add("user_id", "default");
            //jObject.Add("code", code);
            //jObject.Add("type", type);
            //jObject.Add("app_type", app_type);
            ////获取所有的信息点
            //jObject.Add("is_all", "1");
            HttpClient client = HttpClientManager.CreateClient();
            var result = client.GetAsync(new Uri(url, UriKind.RelativeOrAbsolute));
            var stringResult = HandlerTaskResponse(result, new RequestInfo(url));
            IInputService inputService = ServiceLocator.Current.GetService<IInputService>();
            var flag = HandlerResult(stringResult,
                (jo) => {
                    var content = jo["Content"];
                    foreach (JObject subj in content)
                    {
                        EquipmentInfoPointItem item = new EquipmentInfoPointItem();
                        item.Category = (string)subj["firstTag"];
                        item.SubCategory = (string)subj["secondTag"];
                        item.Name = (string)subj["infoPointName"];
                        item.CodeName = (string)subj["infoPointCode"];
                        item.Unit = (string)subj["unit"];
                        item.Type = (string)subj["dataType"];
                        item.InputType = (string)subj["inputMode"];
                        item.EnumSource = subj.GetCheckValue("dataSource");
                        item.Remark = (string)subj["note"];
                        item.Classification = (string)subj["classification"];
                        item.IsShow = true;// (string)subj["is_mapped"] == "1";
                        item.CollectionCmptCode = inputService?.GetInputCode(item.InputType) ?? item.InputType;//(string)subj["collection_cmpt_code"];
                        items.Add(item);
                    }

                }
            , (jo) => { this.ErrorMessage = jo["ResultMsg"].ToString(); });

            return items;//.Where(i=>i.Classification== "4").ToList();


        }
        /// <summary>
        /// 获取自定义信息点
        /// </summary>
        /// <returns></returns>
        public List<EquipmentInfoPointItem> UserDefineSysOrEqInfos(string projectId, string code)
        {
            List<EquipmentInfoPointItem> items = new List<EquipmentInfoPointItem>();
            //由于使用了相关的项目，是不是可以不过滤这个
            string url = BllHttpSetting.Instance.EquipmentUri + $"/data-platform-3/infocode/complex_query?projectId={projectId}&type={code}&combine=false";

            HttpClient client = HttpClientManager.CreateClient();
            var result = client.GetAsync(new Uri(url, UriKind.RelativeOrAbsolute));
            var stringResult = HandlerTaskResponse(result, new RequestInfo(url));
            var flag = HandlerResult(stringResult,
                (jo) => {
                    var content = jo["Content"];
                    foreach (JObject subj in content)
                    {
                        EquipmentInfoPointItem item = new EquipmentInfoPointItem();
                        item.Category = (string)subj["firstTag"];
                        item.SubCategory = (string)subj["secondTag"];
                        item.Name = (string)subj["infoPointName"];
                        item.CodeName = (string)subj["infoPointCode"];
                        item.Unit = (string)subj["unit"];
                        item.Type = (string)subj["dataType"];
                        item.InputType = (string)subj["inputMode"];
                        item.EnumSource = subj.GetCheckValue("dataSource");
                        item.Remark = (string)subj["note"];
                        item.IsShow = true;
                        items.Add(item);
                    }

                }
            , (jo) => { this.ErrorMessage = jo["ResultMsg"].ToString(); });
            return items;
        }
    }
}
