using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace CSharDemoFQB
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string url = "http://open.faqianbei.com/sop/gateway";
            string value = "";


            //settle.register.api.register 接口参数
            Register register = new Register();
            register.name = "王宏娟";
            register.mobile = "13631784438";
            register.idType = "IDENTITY";
            register.idNo = "211422199401102341";
            register.gender = 0;
            register.birthday = "1994-01-10";

            string dir = "File";
            string fileDir = Application.StartupPath;
            string fileName1 = "front.jpg";
            string fileName2 = "back.jpg";
            string filePath = Path.Combine(fileDir, dir);
            Bitmap bmp1 = new Bitmap(Path.Combine(filePath, fileName1));
            Bitmap bmp2 = new Bitmap(Path.Combine(filePath, fileName2));

            register.idCardFront = ConvertImageToBase64(bmp1);
            register.idCardBack = ConvertImageToBase64(bmp2);


            //基础参数
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            list.Add(new KeyValuePair<string, string>("app_id", "10220420983801"));
            list.Add(new KeyValuePair<string, string>("method", "settle.register.api.register"));
            list.Add(new KeyValuePair<string, string>("sign_type", "RSA2"));
            //list.Add(new KeyValuePair<string, string>("sign", ""));
            list.Add(new KeyValuePair<string, string>("timestamp", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
            list.Add(new KeyValuePair<string, string>("version", "1.0"));
            list.Add(new KeyValuePair<string, string>("merchant_request_no", "1000000"));
            list.Add(new KeyValuePair<string, string>("biz_content", JSONHelper.DataContractJsonSerialize(register)));
            //参数排序
            list.Sort((x, y) => (x.Key.CompareTo(y.Key)));


            //拼接参数post与sign参数
            var collParam = new System.Collections.Specialized.NameValueCollection();
            StringBuilder sbSign = new StringBuilder();
            string sign;
            foreach (var item in list)
            {
                //post参数
                collParam.Add(item.Key, item.Value);
                //拼接sign参数
                if (item.Key != "sign")
                    sbSign.Append(string.Format("&{0}={1}", item.Key, item.Value));
            }
            //删除首个&
            if (sbSign.Length > 0)
                sbSign.Remove(0, 1);

            //Print("原始Sign",originalSign.ToString());

            //根据私钥生成和参数生成sign
            sign = EncryptHelper.Signature(sbSign.ToString(), EncryptHelper.privateKey_pkcs8, "utf-8",false);
            Print("sign", sign);

            //补全post参数
            collParam.Add("sign", sign);

            //Print("post参数", string.Join(Environment.NewLine, list.Select(l => l.ToString()).ToArray()));

            //提交
            using (var webClient = new WebClient())
            {
                byte[] byRemoteInfo = webClient.UploadValues(url, "POST", collParam);
                value = System.Text.Encoding.UTF8.GetString(byRemoteInfo);
            }

            Print("返回结果", value);
            Console.ReadLine();
        }

        private static void Print(string tit, string mes)
        {
            Console.WriteLine(tit + ":");
            Console.WriteLine(mes);
            Console.WriteLine();
        }

        public static string ConvertImageToBase64(Bitmap bmp)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageBytes = memoryStream.ToArray();
                return Convert.ToBase64String(imageBytes);
            }
        }
    }
}