using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using Library;
using LitJson;

namespace MySystem.Areas.Api.Controllers
{
    [Area("Api")]
    [Route("Api/[controller]/[action]")]
    public class UserAuthController : BaseController
    {
        public UserAuthController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }
        
        #region 小程序-获取secret

        public JsonResult GetSecret(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            string AppId = data["appId"].ToString(); //小程序AppId
            string AuthCode = data["authCode"].ToString(); //小程序授权码
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Obj.Add("appSecret", "QJHQJ45G6FPFDAG66JBA5XXX2VTT9APT"); //小程序密钥
            Obj.Add("appSalt", "12345678"); //小程序IV
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }

        #endregion

        #region 小程序-授权

        public JsonResult Auth(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            string AppId = data["appId"].ToString(); //小程序AppId
            string Code = data["code"].ToString(); //加密Code
            // string Data = AppDesDecrypt(Code, "fghfghghjghjghfghjfgfgh", "12345678");
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            Obj.Add("appToken", "77V3ULHBCE5PV9KXPWL5P7346HJPHUR3"); //小程序获取用户信息的Token
            Obj.Add("openId", "wnjasnjdjjghjhjn"); //小程序openId
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }

        #endregion

        #region 小程序-获取用户信息

        public JsonResult GetUserInfo(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            string AppToken = data["appToken"].ToString(); //小程序获取用户信息的Token
            string OpenId = data["openId"].ToString(); //小程序openId
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            // Models.Users user = db.Users.FirstOrDefault(m => m.Id == 1) ?? new Models.Users();
            Obj.Add("mobile", "138xxxxxxxx");
            Obj.Add("nickName", "测试昵称"); //昵称
            Obj.Add("headPhoto", SourceHost + "/xxx/xxx.jpg");  //头像
            Obj.Add("province", "四川省"); //省
            Obj.Add("city", "成都市"); //市
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }

        #endregion




        #region 3DES解密

        private string AppDesDecrypt(string encryptedText, string key, string iv)
        {
            byte[] keyArray;
            byte[] ivArray;
            byte[] encryptedTextArray;
            byte[] decryptedTextArray;
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();

            keyArray = UTF8Encoding.UTF8.GetBytes(key.Substring(0, 24));
            ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            encryptedTextArray = Convert.FromBase64String(encryptedText);

            tripleDES.Key = keyArray;
            tripleDES.IV = ivArray;

            ICryptoTransform decryptor = tripleDES.CreateDecryptor(tripleDES.Key, tripleDES.IV);

            using (MemoryStream ms = new MemoryStream(encryptedTextArray))
            {
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader sr = new StreamReader(cs))
                    {
                        decryptedTextArray = Encoding.UTF8.GetBytes(sr.ReadToEnd());
                    }
                }
            }

            string decryptedText = Encoding.UTF8.GetString(decryptedTextArray);
            return decryptedText;
        }

        #endregion
    }
}