using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using DeviceRepair.Models.Common;
using Newtonsoft.Json;

namespace TsSFCDeviceSvc
{
    /// <summary>
    /// MainService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://www.TechScan.cn/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class MainService : WebService
    {
        public App_Code.SFCAuthorize auth;
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public MainService()
        {

        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="module"></param>
        /// <param name="inParams"></param>
        /// <param name="btResults"></param>
        /// <returns></returns>
        public APIResponseData GetDatas(SysModelType module, string inParams, out byte[] btResults)
        {
            HttpAuthtication();
            btResults = new byte[] { };
            Dictionary<string, string> Parameters = GetParameters(inParams);
            DataSet dsResults = new DataSet("DataResults");
            try
            {
                switch (module)
                {
                    default:
                        break;
                }
                return new APIResponseData() { Code = 0, Message = "数据获取成功！" };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return new APIResponseData() { Code = -1, Message = ex.Message };
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        [WebMethod]
        [SoapHeader("auth")]
        public string HelloWorld()
        {
            return "Hello World";
        }

        private void HttpAuthtication()
        {
            #region 校验

            if (auth == null || !auth.ValidUser(auth.Username, auth.Password))
            {
                HttpException soapEx = new HttpException(403, "服务不允许访问此页面，也不允许匿名访问。");

                log.Warn(string.Format("客户端[{0}]我们试图执行远程匿名访问Func[{1}]!这是被禁止的！", HttpContext.Current.Request.UserHostAddress, "GetDatas"), soapEx);
                throw soapEx;
            }

            #endregion 校验
        }

        private Dictionary<string, string> GetParameters(string inParameter)
        {
            return JsonConvert.DeserializeObject<Dictionary<string, string>>(inParameter);
        }

    }
}
