package ebai

import (
	"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
	"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/ddmsg"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/netprinter"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

func OnCallbackMsg(msg *ebaiapi.CallbackMsg) (response *ebaiapi.CallbackResponse) {
	if CurPurchaseHandler != nil {
		if orderID := GetOrderIDFromMsg(msg); orderID != "" {
			jxutils.CallMsgHandler(func() {
				switch msg.Cmd {
				case ebaiapi.CmdOrderCreate, ebaiapi.CmdOrderStatus, ebaiapi.CmdOrderUserCancel, ebaiapi.CmdOrderPartRefund, ebaiapi.CmdOrderReversePush:
					response = CurPurchaseHandler.onOrderMsg(msg)
				case ebaiapi.CmdOrderDeliveryStatus:
					response = CurPurchaseHandler.onWaybillMsg(msg)
				}
			}, jxutils.ComposeUniversalOrderID(orderID, model.VendorIDEBAI))
		}
		if /*msg.Cmd == ebaiapi.CmdOrderPartRefund || msg.Cmd == ebaiapi.CmdOrderUserCancel || */ msg.Cmd == ebaiapi.CmdOrderDeliveryStatus {
			response = CurPurchaseHandler.OnFinancialMsg(msg)
		} else if msg.Cmd == ebaiapi.CmdShopMsgPush {
			response = CurPurchaseHandler.onShopMsgPush(msg)
		} else if msg.Cmd == ebaiapi.CmdShopUnbindMsg {
			// 门店授权发生变化 17,失去授权
			vendorStoreID := msg.Body["shop_list"].([]*ebaiapi.EBaiShopList)[0].ShopID
			storeDetail, err := dao.GetStoreDetailByVendorStoreID(dao.GetDB(), vendorStoreID, model.VendorIDEBAI, "")
			_, err = netprinter.PrintStoreStatus(jxcontext.AdminCtx, storeDetail, model.VendorIDMTWM, -9)
			response = api.EbaiAPI.Err2CallbackResponse(msg.Cmd, err, msg.Cmd)
		} else if msg.Cmd == ebaiapi.CmdImMessageSendEvent /*|| msg.Cmd == ebaiapi.CmdImMessageReadEvent*/ {
			response = CurPurchaseHandler.OnImMessage(msg)
		} else if msg.Cmd == ebaiapi.CmdImInvoicApply || msg.Cmd == ebaiapi.CmdImInvoicEmail {
			globals.SugarLogger.Debugf("-ebai-----Invoice:= %s", utils.Format4Output(msg, false))
			ddmsg.SendUserMessage(dingdingapi.MsgTyeText, "2452A93EEB9111EC9B06525400E86DC0", "EbaiController发票推送", utils.Format4Output(msg, false))
			response = CurPurchaseHandler.InvoiceApply(msg)
		}
	}
	return response
}

func GetOrderIDFromMsg(msg *ebaiapi.CallbackMsg) string {
	return GetOrderIDFromMap(msg.Body)
}

func GetOrderIDFromMap(orderMap map[string]interface{}) string {
	if orderID := orderMap["order_id"]; orderID != nil {
		if tryOrderID, ok := orderID.(string); ok {
			return tryOrderID
		}
		return utils.Int64ToStr(utils.MustInterface2Int64(orderID))
	}
	return ""
}

func GetAfsIDFromMap(orderMap map[string]interface{}) string {
	//if orderID := orderMap["refund_order_id"]; orderID != nil {
	//	if tryOrderID, ok := orderID.(string); ok {
	//		return tryOrderID
	//	}
	//	return utils.Int64ToStr(utils.MustInterface2Int64(orderID))
	//}
	//return ""

	ReverseOderList := orderMap["reverse_order_list"].([]interface{})
	if len(ReverseOderList) != 0 {
		afsInfo := ReverseOderList[len(ReverseOderList)-1].(map[string]interface{})
		return afsInfo["refund_order_id"].(string)
	}
	return ""
}
