using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pivotal.Discovery.Client;
using System.Collections.Generic;

namespace Api.SignToken
{
    public class SignTokenService : ISignTokenService
    {
        DiscoveryHttpClientHandler _handler;
        ILogger<SignTokenService> _logger;

        /// <summary>
        /// URL
        /// </summary>
        private const string URL = "http://signtokenapi/api/";


        public SignTokenService(IDiscoveryClient client, ILoggerFactory logFactory)
        {
            _handler = new DiscoveryHttpClientHandler(client, logFactory.CreateLogger<DiscoveryHttpClientHandler>());
            _logger = logFactory.CreateLogger<SignTokenService>();
        }

        private HttpClient GetClient()
        {
            var client = new HttpClient(_handler, false);
            return client;
        }

        public async Task<string> Wechat_Admin_LoginAsync(string usercode, string password, string weixin, string weixin_name, string weixin_img, int channel = 4, string returnUrl = null)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"usercode", usercode},
               {"password", password},
               {"weixin", weixin},
               {"weixin_name", weixin_name},
               {"weixin_img", weixin_img},
               {"channel", channel.ToString()},
               {"returnUrl", returnUrl}
            });
            var response = await client.PostAsync(URL + "token/wechat_admin_login", postContent);
            return await response.Content.ReadAsStringAsync();
            //_logger.LogInformation("返回的权限信息为: {0}", result);

        }

        public async Task<string> Wechat_LoginAsync(string weixin, string weixin_name, string weixin_img, int channel = 4, string returnUrl = null)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"weixin", weixin},
               {"weixin_name", weixin_name},
               {"weixin_img", weixin_img},
               {"channel", channel.ToString()},
               {"returnUrl", returnUrl}
            });
            var response = await client.PostAsync(URL + "token/wechat_login", postContent);
            return await response.Content.ReadAsStringAsync();
            //_logger.LogInformation("返回的权限信息为: {0}", result);

        }

        public string GetInfo()
        {
            var client = GetClient();
            var result = client.GetStringAsync(URL + "info").Result;
            _logger.LogInformation("返回的权限信息为: {0}", result);
            return result;
        }

        public async Task<string> GetUserInfoAsync(string usercode)
        {
            var client = GetClient();
            var response = await client.GetAsync(URL + "useraccount/getuserinfo?usercode=" + usercode);
            return await response.Content.ReadAsStringAsync();
        }

        public async Task<string> Wechat_BindAsync(string usercode, string openid, int channel = 4)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"usercode", usercode},
               {"openid", openid},
               {"channel", channel.ToString()}
            });
            var response = await client.PostAsync(URL + "token/wechat_bind", postContent);
            return await response.Content.ReadAsStringAsync();
        }
    }
}
