package apimanager

import (
	"git.rosy.net.cn/baseapi/platformapi/jdapi"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"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/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

const (
	FakeJdOrgCode = "fakejd"
)

type VendorOrgCodePair struct {
	VendorID   int
	AppOrgCode string
}

type APIManager struct {
}

var (
	CurAPIManager *APIManager
)

func init() {
	globals.SugarLogger.Debug("init apimanager")
	CurAPIManager = &APIManager{}
	partner.InitAPIManager(CurAPIManager)
}

func (a *APIManager) GetAPI(vendorID int, appOrgCode string) (pfAPI interface{}) {
	var (
		db = dao.GetDB()
	)
	switch vendorID {
	case model.VendorIDJD:
		if appOrgCode == FakeJdOrgCode {
			pfAPI = api.FakeJdAPI
		} else {
			codes, _ := dao.GetVendorOrgCode(db, vendorID, appOrgCode, "platform")
			code := codes[0]
			api.JdAPI = jdapi.New(code.Token, code.AppKey, code.AppSecret)
			pfAPI = api.JdAPI
			if configs, err := dao.QueryConfigs(dao.GetDB(), "jdStorePageCookie", model.ConfigTypeCookie, ""); err == nil {
				api.JdAPI.SetCookieWithStr(configs[0].Value)
			}
			if configs, err := dao.QueryConfigs(dao.GetDB(), "jdStorePageEarning", model.ConfigTypeCookie, ""); err == nil {
				api.JdAPI.SetCookie("user", configs[0].Value)
			}
		}
	case model.VendorIDMTWM:
		pfAPI = api.MtwmAPI
		if appOrgCode == globals.Mtwm2Code {
			pfAPI = api.Mtwm2API
		}
	case model.VendorIDEBAI:
		pfAPI = api.EbaiAPI
	case model.VendorIDJDShop:
		pfAPI = api.JdShopAPI
	}
	return pfAPI
}

// TODO
func (a *APIManager) GetAppOrgCodeList(vendorID int) (appOrgCodeList []string) {
	switch vendorID {
	case model.VendorIDJD:
		vendorOrgCodes, _ := dao.GetVendorOrgCode(dao.GetDB(), model.VendorIDJD, "", model.VendorOrgTypePlatform)
		for _, v := range vendorOrgCodes {
			appOrgCodeList = append(appOrgCodeList, v.VendorOrgCode)
		}
	case model.VendorIDMTWM:
		appOrgCodeList = []string{api.MtwmAPI.GetAppID(), api.Mtwm2API.GetAppID()}
	case model.VendorIDEBAI:
		appOrgCodeList = []string{api.EbaiAPI.GetSource()}
	case model.VendorIDJDShop:
		appOrgCodeList = []string{"2"}
	}
	return appOrgCodeList
}

func (a *APIManager) GetVendorOrgCodeList(vendorIDs []int, appOrgCodes []string) (vendorOrgCodeList []*VendorOrgCodePair) {
	if len(vendorIDs) == 0 {
		vendorIDs = partner.GetMultiStoreVendorIDs()
	}
	appOrgCodeMap := jxutils.StringList2Map(appOrgCodes)
	for _, vendorID := range vendorIDs {
		tmpOrgCodeList := a.GetAppOrgCodeList(vendorID)
		for _, v := range tmpOrgCodeList {
			if len(appOrgCodes) == 0 || appOrgCodeMap[v] == 1 {
				vendorOrgCodeList = append(vendorOrgCodeList, &VendorOrgCodePair{
					VendorID:   vendorID,
					AppOrgCode: v,
				})
			}
		}
	}
	return vendorOrgCodeList
}

func GetVendorOrgCodeMap(ctx *jxcontext.Context) (vendorOrgCodeMap map[int][]string) {
	vendorOrgCodeMap = make(map[int][]string)
	for _, vendorID := range partner.GetPurchasePlatformVendorIDs() {
		vendorOrgCodeMap[vendorID] = CurAPIManager.GetAppOrgCodeList(vendorID)
	}
	return vendorOrgCodeMap
}
