using PushInfo.Core.Models.QueryResults;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushInfo.Core.Common
{
    public class PushValueHelp
    {
        #region 获得推送体数据属性名称
        public static List<string> GetValueProperty(string pushBody)
        {
            List<string> returnModel = new List<string>();
            if(string.IsNullOrEmpty(pushBody))
            {
                return returnModel;
            }
            int indexNums = 0;
            int startIndex = pushBody.IndexOf("[");
            int endIndex = 0;
            while (startIndex > -1)
            {
                endIndex = pushBody.IndexOf("]", startIndex + 1);
                if (endIndex > -1)
                {
                    string attribute = pushBody.Substring(startIndex + 1, endIndex - startIndex - 1);
                    if(!returnModel.Contains(attribute))
                    {
                        returnModel.Add(attribute);
                    }
                }
                else
                {
                    break;
                }
                startIndex = pushBody.IndexOf("[", endIndex);
                if (++indexNums > 100)  //防止陷入死循环
                {
                    break;
                }
            }
            return returnModel;
        }
        #endregion

        #region row转DataMessage
        public static DataMessage RowToDataMessage(DataTable tableValue, DataRow row)
        {
            DataMessage dataR = new DataMessage();
            if (tableValue.Columns.Contains("userId"))
            {
                dataR.userId = row["userId"].ToString();
            }
            if (tableValue.Columns.Contains("clientType"))
            {
                dataR.clientType = row["clientType"].ToString();
            }
            if (tableValue.Columns.Contains("clientID"))
            {
                dataR.clientID = row["clientID"].ToString();
            }
            if (tableValue.Columns.Contains("loginName"))
            {
                dataR.loginName = row["loginName"].ToString();
            }
            if (tableValue.Columns.Contains("realName"))
            {
                dataR.realName = row["realName"].ToString();
            }
            if (tableValue.Columns.Contains("mobile"))
            {
                dataR.mobile = row["mobile"].ToString();
            }
            if (tableValue.Columns.Contains("openId"))
            {
                dataR.openId = row["openId"].ToString();
            }
            if (tableValue.Columns.Contains("projectId"))
            {
                dataR.projectId = row["projectId"].ToString();
            }
            return dataR;
        }
        #endregion

        #region 根据数据源将模板字符串转换为数字字符串
        /// <summary>
        /// 数据源是对象
        /// </summary>
        /// <returns></returns>
        public static string TransfOfDataModel(string configStr, DataMessage data)
        {
            if(string.IsNullOrEmpty(configStr))
            {
                return configStr;
            }
            List<string> valueProList = GetValueProperty(configStr);
            if(data.dicValue == null)
            {
                return configStr;
            }
            foreach (string proStr in valueProList)
            {
                if (data.dicValue.ContainsKey(proStr))
                {
                    configStr = configStr.Replace("[" + proStr + "]", data.dicValue[proStr]);
                }
                else
                {
                    configStr = configStr.Replace("[" + proStr + "]", "");
                }
            }
            return configStr;
        }
        /// <summary>
        /// 数据源是DataTable 里面的Row
        /// </summary>
        /// <returns></returns>
        public static string TransfOfDataRow(string configStr,DataColumnCollection tableColumn, DataRow row)
        {
            if (string.IsNullOrEmpty(configStr))
            {
                return configStr;
            }
            List<string> valueProList = GetValueProperty(configStr);
            foreach (string proStr in valueProList)
            {
                if (tableColumn.Contains(proStr))
                {
                    configStr = configStr.Replace("[" + proStr + "]", row[proStr].ToString());
                }
                else
                {
                    configStr = configStr.Replace("[" + proStr + "]", "");
                }
            }
            return configStr;
        }
        #endregion


    }
}
