package tiktok_store

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/tiktok_shop/tiktok_api"
	"sync"

	"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/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

	storeIDs []string
	locker   sync.RWMutex
}

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

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

func (c *PurchaseHandler) GetVendorID() int {
	return model.VendorIDDD
}

func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
	cats, err := getAPI("260477060", 0, "").GetShopCategory(0)
	if err != nil {
		return nil, err
	}

	vendorCats = skuCategory(cats)
	return vendorCats, nil
}

func skuCategory(param1 []*tiktok_api.RetailCategoryInfo) (vendorCats []*model.SkuVendorCategory) {
	size := make([]*model.SkuVendorCategory, 0)
	for _, v := range param1 {
		param := &model.SkuVendorCategory{
			VendorCategoryID: utils.Int64ToStr(v.Id),
			VendorID:         model.VendorIDDD,
			Name:             v.Name,
			Level:            int(v.Level),
			ParentID:         utils.Int64ToStr(v.ParentId),
		}
		if v.Enable == false {
			param.IsLeaf = 0
		} else {
			param.IsLeaf = 1
		}
		if param.Name != "" {
			size = append(size, param)
		}

		if len(v.Children) != 0 {
			kk := skuCategory(v.Children)
			vendorCats = append(vendorCats, kk...)
		}
	}
	vendorCats = append(vendorCats, size...)
	return
}

func bizStatusTiktok2JX(status int64) int {
	switch status {
	case tiktok_api.StoreStateNormalBusiness:
		return model.StoreStatusOpened
	case tiktok_api.StoreStateSuspendBusiness:
		return model.StoreStatusClosed
	}
	return model.StoreStatusDisabled
}

func (p *PurchaseHandler) UploadImg(ctx *jxcontext.Context, vendorOrgCode, imgURL string, imgData []byte, imgName string, imgType int) (imgHint string, err error) {
	poiCode4UploadImg := p.getUploadImgPoiCode()
	if poiCode4UploadImg == "" {
		return "", fmt.Errorf("找不到一个美团门店来上传图片")
	}
	if globals.EnableMtwmStoreWrite {
		if imgType > model.ImgTypeLocal {
			if imgData != nil {
				imgHint, err = api.MtwmAPI.ImageUpload(poiCode4UploadImg, imgName, imgData)
			} else {
				imgHint, err = api.MtwmAPI.ImageUploadByURL(poiCode4UploadImg, imgName, imgURL)
			}
		}
	} else {
		imgHint = utils.GetUpperUUID()
	}
	return imgHint, err
}

func getStoreIDFromList(storeIDs []string) (poiCode string) {
	if len(storeIDs) > 0 {
		poiCode = storeIDs[0]
	}
	return poiCode
}

func (p *PurchaseHandler) getUploadImgPoiCode() (poiCode string) {
	var storeIDs []string
	p.locker.RLock()
	storeIDs = p.storeIDs
	p.locker.RUnlock()
	if len(storeIDs) > 0 {
		return getStoreIDFromList(storeIDs)
	}

	p.locker.Lock()
	storeIDs = p.storeIDs
	if len(storeIDs) > 0 {
		p.locker.Unlock()
		return getStoreIDFromList(storeIDs)
	}

	defer p.locker.Unlock()
	storeIDs, err := api.MtwmAPI.PoiGetIDs()
	if err == nil {
		if len(storeIDs) > 0 {
			p.storeIDs = storeIDs
			poiCode = getStoreIDFromList(storeIDs)
		} else {
			// p.storeIDs = []string{""}
		}
	}
	return poiCode
}

func getAPI(appOrgCode string, storeID int, vendorStoreID string) (apiObj *tiktok_api.API) {
	return partner.CurAPIManager.GetAPI(model.VendorIDDD, appOrgCode).(*tiktok_api.API)
}

func GetTiktokApi(appOrgCode string, storeID int, vendorStoreID string) *tiktok_api.API {
	return getAPI(appOrgCode, storeID, vendorStoreID)
}
