using System;
using System.Data;
using System.IO;
using System.Web;
using System.Xml;
using CallCenter.Utility;

namespace CallCenterApi.Common
{
    public class SysConfigHelper
    {
        public class DemoKeys
        {
            /// <summary>
            /// ApiID
            /// </summary>
            public string ID;

            /// <summary>
            /// api域名
            /// </summary>
            public string Domain;

            /// <summary>
            /// Api名称
            /// </summary>
            public string Name;

            /// <summary>
            /// ApiKey
            /// </summary>
            public string Keys;


            public DemoKeys(string Key)
            {
                var configPath = GetFilePath("~/Content/Config/DemoKeys.config");
                var myxml = new XmlHelper();
                var stream = new StringReader(myxml.InnerXml(configPath));
                var reader = new XmlTextReader(stream);
                var ds = new DataSet();
                ds.ReadXml(reader);
                var dt = ds.Tables.Count > 0 ? ds.Tables[0] : null;
                if (dt != null)
                {
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        if (Key.ToLower() == dt.Rows[i]["ID"].ToString().ToLower() || Key.ToLower() == dt.Rows[i]["Domain"].ToString().ToLower())
                        {
                            ID = dt.Rows[i]["ID"].ToString();
                            Domain = dt.Rows[i]["Domain"].ToString();
                            Name = dt.Rows[i]["Name"].ToString();
                            Keys = dt.Rows[i]["Keys"].ToString();
                            break;
                        }
                    }
                }
            }
        }

        public class SysConfigPara
        {
            /// <summary>
            /// Api地址请求超时秒数
            /// </summary>
            public int ApiTimeOut;

            /// <summary>
            /// 广播连接用户
            /// </summary>
            public string InterfaceUsername;

            /// <summary>
            /// 广播连接密码
            /// </summary>
            public string InterfacePassword;

            /// <summary>
            /// 广播连接Key
            /// </summary>
            public string LoginSecretKey;

            /// <summary>
            /// 广播连接地址
            /// </summary>
            public string BoardcastUrl;
          
            public SysConfigPara()
            {
                string configPath = GetFilePath("~/Content/Config/ConfigPara.config");
                var myxml = new XmlHelper();
                myxml.LoadXml(configPath);
                ApiTimeOut = Convert.ToInt32(myxml.GetValue("ApiTimeOut"));
                InterfaceUsername = myxml.GetValue("InterfaceUsername");
                InterfacePassword = myxml.GetValue("InterfacePassword");
                LoginSecretKey = myxml.GetValue("LoginSecretKey");
                BoardcastUrl = myxml.GetValue("BoardcastUrl");
            }
        }

        private static string GetFilePath(string fileName)
        {
            return HttpContext.Current.Server.MapPath(fileName);
        }
    }
}
