package gomei

import (
	"encoding/base64"
	gomei "git.rosy.net.cn/baseapi/platformapi/gome_live_show"
	"git.rosy.net.cn/baseapi/platformapi/jdshopapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/business/partner/putils"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

var (
	CurPurchaseHandler *PurchaseHandler
)

type PurchaseHandler struct {
	partner.BasePurchasePlatform
	putils.DefSingleStorePlatform
}

func init() {
	if api.GuoMeiApi != nil {
		CurPurchaseHandler = New()
		partner.RegisterPurchasePlatform(CurPurchaseHandler)
	}
}

func New() (obj *PurchaseHandler) {
	obj = new(PurchaseHandler)
	obj.ISingleStoreStoreSkuHandler = obj
	return obj
}

func getAPI(appOrgCode string) (apiObj *gomei.API) {
	if appOrgCode == "" {
		globals.SugarLogger.Warnf("getAPI appOrgCode is empty")
	}

	return partner.CurAPIManager.GetAPI(model.VendorGoMei, appOrgCode).(*gomei.API)
}

func GetAPI(appOrgCode string) (apiObj *jdshopapi.API) {
	apiObj = partner.CurAPIManager.GetAPI(model.VendorGoMei, appOrgCode).(*jdshopapi.API)
	if configs, err := dao.QueryConfigs(dao.GetDB(), "jdsCookie2", model.ConfigTypeCookie, ""); err == nil {
		apiObj.SetCookieWithStr(configs[0].Value)
	}
	return apiObj
}

func (p *PurchaseHandler) GetVendorID() int {
	return model.VendorGoMei
}

func (p *PurchaseHandler) UploadImg(ctx *jxcontext.Context, vendorOrgCode, imgURL string, imgData []byte, imgName string, imgType int) (imgHint string, err error) {
	if globals.EnableJdShopWrite {
		if imgType > model.ImgTypeLocal {
			result, err := getAPI(vendorOrgCode).UploadPicture(base64.StdEncoding.EncodeToString(imgData), 0, imgName)
			if err == nil {
				imgHint = result.Data.Url
			}
		}
	} else {
		imgHint = utils.GetUpperUUID()
	}
	return imgHint, err
}

// 获取国美商户的分类标签,官方未提供接口.->查询所有门店分类,去重复!目前国美分类只有一级
func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
	storeList, err := getAPI(string(model.VendorGoMei)).QueryStoreList(&gomei.GetStoreListReq{
		Page: gomei.Page{
			PageSize:   model.BrandBalanceLimit,
			PageNumber: 1,
		},
	})
	if err != nil {
		return nil, err
	}

	if storeList.Data.Total == 0 {
		return nil, nil
	}

	result := make(map[string]*model.SkuVendorCategory, 0)
	for _, v := range storeList.Data.Records {
		storeCategoryList, err := GetAllStoreCategories(string(model.VendorGoMei), v.StoreCode)
		//storeCategoryList, err := api.GuoMeiApi.QueryStoreCategoryList(&gomei.QueryStoreGoodsCategoryListReq{
		//	Page:      gomei.Page{PageNumber: 1, PageSize: model.BrandBalanceLimit},
		//	StoreCode: v.StoreCode,
		//})
		if err != nil || len(storeCategoryList) <= 0 {
			continue
		}
		for _, c := range storeCategoryList {
			cat := &model.SkuVendorCategory{
				VendorID:         model.VendorGoMei,
				Name:             c.CategoryName,
				Level:            1,
				VendorCategoryID: c.CategoryCode,
				ParentID:         "",
				IsLeaf:           0,
			}
			result[cat.Name] = cat
		}
	}

	for _, v := range result {
		vendorCats = append(vendorCats, v)
	}
	return vendorCats, err
}

func (p *PurchaseHandler) GetOrderRider(vendorOrgCode, vendorStoreID string, param map[string]interface{}) (err error) {
	return nil
}
