using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Library;
// using MongoDB.Bson;
// using MongoDB.Driver;
// using MongoDB.Driver.Linq;

namespace MySystem
{
    public class IdCardCheckForThree
    {
        public readonly static IdCardCheckForThree Instance = new IdCardCheckForThree();
        private IdCardCheckForThree()
        {
        }

        private const String host = "https://dfphone3.market.alicloudapi.com";
        private const String path = "/verify_id_name_phone";
        private const String method = "POST";
        private const String appcode = "8e5704921ca3422f80f0deb935a7ddc6";

        public string Do(string mobile, string idcard, string name)
        {
            String querys = "";
            function.WriteLog(DateTime.Now.ToString(), "IdCard验证三要素日志");
            function.WriteLog(querys, "IdCard验证三要素日志");
            String bodys = "id_number=" + idcard + "&name=" + name + "&phone_number=" + mobile + "";
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            if (host.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            //根据API的要求,定义相对应的Content-Type
            httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            if (0 < bodys.Length)
            {
                byte[] data = Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            string result = reader.ReadToEnd();
            function.WriteLog(result, "IdCard验证三要素日志");
            function.WriteLog("\n\n", "IdCard验证三要素日志");
            return result;
        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}