using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.Service;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.Tools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace ASHermed.SinoInterface.Api.IService
{
    /// <summary>
    /// 有赞服务类
    /// </summary>
    public class YouZanService : BaseService, IYouZanService
    {
        IOutService _outService;
        RedisHelperClient _redisHelper;
        IPubUserInfoRepository _pubUserInfoRepository;
        public YouZanService(IOutService outService, RedisHelperClient redisHelper, IPubUserInfoRepository pubUserInfoRepository)
        {
            _outService = outService;
            _redisHelper = redisHelper;
            _pubUserInfoRepository = pubUserInfoRepository;
        }

        /// <summary>
        /// 获取有赞access_token
        /// </summary>
        /// <returns></returns>
        public string GetYouZanToken()
        {
            var data = _outService.HttpPost<YouZanResponse<YouZanTokenData>>("https://open.youzanyun.com/auth/token", new YouZanTokenRequst().ToJson());
            if (data.success)
            {
                //_redisHelper.StringSet("YouZanToken", data.data.ToJson(), TimeSpan.FromDays(6));
                return data.data.access_token;
            }
            //var token = _redisHelper.Get("YouZanToken");
            //if (string.IsNullOrEmpty(token))
            //{
            //    var data = _outService.HttpPost<YouZanResponse<YouZanTokenData>>("https://open.youzanyun.com/auth/token", new YouZanTokenRequst().ToJson());
            //    if (data.success)
            //    {
            //        _redisHelper.StringSet("YouZanToken", data.data.ToJson(), TimeSpan.FromDays(6));
            //        return data.data.access_token;
            //    }
            //}
            //else
            //{
            //    var tokenData = JsonConvert.DeserializeObject<YouZanTokenData>(token);
            //    return tokenData.access_token;
            //}
            return "";
        }

        /// <summary>
        /// 暂不使用
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public YouZanUser GetYouZanUser(string openId)
        {
            var token = GetYouZanToken();
            var query = new UserQuery();
            query.yz_open_id = openId;
            var data = _outService.HttpPost<YouZanResponse<YouZanUser>>("https://open.youzanyun.com/api/youzan.users.info.query/1.0.0?access_token=" + token, query.ToJson());
            if (data.success)
            {

                return data.data;
            }
            return null;
        }

        /// <summary>
        /// 暂不使用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public YouZanResponse<bool> UpdateYouZanUser(UpdateUserModel model)
        {
            var token = GetYouZanToken();
            var data = _outService.HttpPost<YouZanResponse<bool>>("https://open.youzanyun.com/api/youzan.scrm.customer.update/3.0.0?access_token=" + token, model.ToJson());
            if (data.success)
            {
                return data;
            }
            return null;
        }

        /// <summary>
        /// 更新有赞用户基础信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<bool> YouZanUpdateUserInfo(string openId)
        {
            if (string.IsNullOrEmpty(openId))
            {
                return false;
            }
            var pubUserInfo = _pubUserInfoRepository.GetUserByYZOpenId(openId);
            if (pubUserInfo == null)
            {
                return false;
            }
            UpdateUserModel model = new UpdateUserModel();
            model.account = new AccountInfo();
            model.account.account_type = "YzOpenId";
            model.account.account_id = openId;
            //model.member_created_at = pubUserInfo.Create_Time.ToString("yyyy-MM-dd HH:mm:ss");
            model.scrm_channel_type = 2;
            model.customer_update = new UpdateInfo();
            model.customer_update.gender = pubUserInfo.Gender == 0 ? 2 : 1;
            model.customer_update.birthday = Convert.ToDateTime(pubUserInfo.Birthday).ToString("yyyy-MM-dd HH:mm:ss");
            model.customer_update.name = pubUserInfo.Nick_Name;
            model.create_date = pubUserInfo.Create_Time.ToString("yyyy-MM-dd HH:mm:ss");
            var res = UpdateYouZanUser(model);
            if (res.success)
            {
                return res.data;
            }
            return false;
        }

        /// <summary>
        /// 获取有赞客户基础信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public YouZanResponse<CustomerInfo> GetYouZanCustomer(CustomerGetRequest model)
        {
            var token = GetYouZanToken();
            var data = _outService.HttpPost<YouZanResponse<CustomerInfo>>("https://open.youzanyun.com/api/youzan.scrm.customer.detail.get/1.0.1?access_token=" + token, model.ToJson());
            if (data.success)
            {
                return data;
            }
            return null;
        }

        /// <summary>
        /// 发放权益卡 https://doc.youzanyun.com/detail/API/0/871
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public YouZanResponse<CustomerInfo> SendYouZanCard(SendCardRequest model)
        {
            var token = GetYouZanToken();
            var data = _outService.HttpPost<YouZanResponse<CustomerInfo>>("https://open.youzanyun.com/api/youzan.scrm.customer.card.grant/4.0.0?access_token=" + token, model.ToJson());
            if (data.success)
            {
                return data;
            }
            return null;
        }

        /// <summary>
        /// 获取权益卡  https://doc.youzanyun.com/detail/API/0/97
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public YouZanResponse<CardResp> GetYouZanCard(GetCard model)
        {
            var token = GetYouZanToken();
            var data = _outService.HttpPost<YouZanResponse<CardResp>>("https://open.youzanyun.com/api/youzan.scrm.card.list/3.0.0?access_token=" + token, model.ToJson());
            if (data.success)
            {
                return data;
            }
            return null;
        }

        /// <summary>
        /// 订单搜索接口 https://doc.youzanyun.com/detail/API/0/2971
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public YouZanResponse<GetTradesSoldResponse> GetTradesSold(GetTradesSoldRequest request)
        {

            return _outService.HttpPost<YouZanResponse<GetTradesSoldResponse>>("https://open.youzanyun.com/api/youzan.trades.sold.get/4.0.2?access_token=" + GetYouZanToken(), request.ToJson());
        }

        /// <summary>
        /// 单项解密
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public YouZanResponse<string> DecryptSingle(DecryptSingleRequest request)
        {
            return _outService.HttpPost<YouZanResponse<string>>("https://open.youzanyun.com/api/youzan.cloud.secret.decrypt.single/1.0.0?access_token=" + GetYouZanToken(), request.ToJson());
        }

        //https://doc.youzanyun.com/detail/API/0/2971    订单搜索接口
        //https://doc.youzanyun.com/detail/API/0/120     交易订单详情4.0接口
    }
}
