using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.IDAL;

namespace Txgy.EWS.Client.DAL
{
    public class WebDataAccess : IWebDataAccess
    {
        //protected string domain = "http://101.43.141.10:8090/api/v1/";
        protected string domain => BusinessConfigManager.Current.Endpoints.ApiDomain;
        public async Task<string> GetDatas(string url)
        {
            using (var client = new HttpClient())
            {
                var resp = await client.GetAsync(url);
                return await resp.Content.ReadAsStringAsync();
            }
        }

        public MultipartFormDataContent GetFormData(Dictionary<string, HttpContent> contents)
        {
            var postContent = new MultipartFormDataContent();
            string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
            postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");

            foreach (var item in contents)
            {
                postContent.Add(item.Value, item.Key);
            }

            return postContent;
        }
        public async Task<string> PostDatas(string url, HttpContent content)
        {
            using (var client = new HttpClient())
            {
                var resp = await client.PostAsync(url, content);
                return await resp.Content.ReadAsStringAsync();
            }
        }

        public async Task<string> GetFileList()
        {
            //return await this.GetDatas($"{domain}file/list");
            
            using (var client = new HttpClient())
            {
                var resp = await client.GetAsync($"{domain}file/check");
                return await resp.Content.ReadAsStringAsync();
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="un">userName</param>
        /// <param name="pwd">passWord</param>
        /// <returns></returns>
        public async Task<string> Login(string un, string pwd)
        {
            //创建FormData请求
            var postContent = new MultipartFormDataContent();
            //设置时间格式
            string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
            //添加头信息
            postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
            //[HttpPost]
            //[Route("login")]
            //public IActionResult Login([FromForm] string userName, [FromForm] string pwd)
            //对应server中LoginController下的Login方法中的userName参数
            postContent.Add(new StringContent(un), "username");
            //对应server中LoginController下的Login方法中的pwd参数
            postContent.Add(new StringContent(pwd), "pwd");
            using (var client = new HttpClient())
            {
                //提交Post请求
                var resp = await client.PostAsync($"{domain}login/login", postContent);
                //返回字符串数据
                var data = await resp.Content.ReadAsStringAsync();
                return data;
            }
        }
        public async Task<string> Login(string un, string pwd,string md5)
        {
            //创建FormData请求
            var postContent = new MultipartFormDataContent();
            //设置时间格式
            string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
            //添加头信息
            postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
            //[HttpPost]
            //[Route("login")]
            //public IActionResult Login([FromForm] string userName, [FromForm] string pwd)
            //对应server中LoginController下的Login方法中的userName参数
            postContent.Add(new StringContent(un), "username");
            //对应server中LoginController下的Login方法中的pwd参数
            postContent.Add(new StringContent(pwd), "pwd");
            postContent.Add(new StringContent(md5), "md5");
            using (var client = new HttpClient())
            {
                var resp = await client.PostAsync($"{domain}login/login", postContent);
                var data = await resp.Content.ReadAsStringAsync();
                return data;
            }
        }
    }
}
