using BBWYB.Client.Models;
using BBWYB.Common.Http;
using BBWYB.Common.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

namespace BBWYB.Client.APIServices
{
    public class MdsApiService : BaseApiService, IDenpendency
    {
        public MdsApiService(RestApiService restApiService, GlobalContext globalContext) : base(restApiService, globalContext)
        {

        }

        public ApiResponse<MDSUserResponse> GetUserInfo(string userToken)
        {
            return SendRequest<MDSUserResponse>(globalContext.MDSApiHost,
                                                "/TaskList/User/GetUserInfo",
                                                null,
                                                new Dictionary<string, string>()
                                                {
                                                    { "Authorization", $"Bearer {userToken}" }
                                                }, HttpMethod.Get);
        }



        //public ApiResponse<IList<ShopResponse>> GetShopsByUserTeam(long userId)
        //{
        //    return SendRequest<IList<ShopResponse>>(globalContext.MDSApiHost, "TaskList/Shop/GetShopsByUserTeam", $"userId={userId}", null, System.Net.Http.HttpMethod.Get);
        //}

        public ApiResponse<IList<Department>> GetShopDetailList()
        {
            var response = new ApiResponse<IList<Department>>();
            var response2 = SendRequest<JArray>(globalContext.MDSApiHost, "TaskList/UserDepartment/GetShopDetailList", null, null, HttpMethod.Get);
            if (!response.Success)
            {
                response.Code = response2.Code;
                response.Msg = response2.Msg;
                return response;
            }

            response.Data = new List<Department>();
            foreach (var jDepartment in response2.Data)
            {
                var jayShops = jDepartment.Value<JArray>("ShopList");
                if (jayShops == null || !jayShops.HasValues)
                    continue;  //排除空店部门
                var d = new Department()
                {
                    Id = jDepartment.Value<string>("Id"),
                    Name = jDepartment.Value<string>("DepartmentName")
                };
                response.Data.Add(d);
                foreach (var jShop in jayShops)
                {
                    var shopId = jShop.Value<long?>("ShopId");
                    if (shopId == null || string.IsNullOrEmpty(jShop.Value<string>("AppToken")))
                        continue; //排除未授权
                    try
                    {
                        var jayAccounts = jShop.Value<JArray>("AccountList");
                        if ((jayAccounts == null || !jayAccounts.HasValues) && d.ShopList.Count(s => s.ShopId == shopId) > 0)
                        {
                            continue;
                        }
                        var shop = new Shop()
                        {
                            ShopId = shopId.Value,
                            AppKey = jShop.Value<string>("AppKey"),
                            AppSecret = jShop.Value<string>("AppSecret"),
                            AppToken = jShop.Value<string>("AppToken"),
                            ManagePwd = jShop.Value<string>("ManagePwd"),
                            Platform = (Platform)jShop.Value<int>("PlatformId"),
                            PlatformCommissionRatio = jShop.Value<decimal?>("PlatformCommissionRatio") ?? 0.05M,
                            ShopName = jShop.Value<string>("ShopName"),
                            VenderType = jShop.Value<string>("ShopType"),
                            TeamId = jShop.Value<string>("TeamId")
                        };
                        d.ShopList.Add(shop);

                        shop.PurchaseAccountList = new List<PurchaseAccount>();
                        foreach (var jPurchaseAccount in jayAccounts)
                        {
                            shop.PurchaseAccountList.Add(new PurchaseAccount()
                            {
                                Id = jPurchaseAccount.Value<long>("Id"),
                                AccountName = jPurchaseAccount.Value<string>("AccountName"),
                                AppKey = jPurchaseAccount.Value<string>("AppKey"),
                                AppSecret = jPurchaseAccount.Value<string>("AppSecret"),
                                AppToken = jPurchaseAccount.Value<string>("AppToken"),
                                ShopId = shop.ShopId,
                                PurchasePlatformId = (Platform)jPurchaseAccount.Value<int>("AppPlatformId")
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(jShop.ToString());
                        throw;
                    }
                }
            }

            return response;
        }
    }
}