package elm

import (
	"fmt"

	"git.rosy.net.cn/baseapi/platformapi/elmapi"
	"git.rosy.net.cn/baseapi/utils"
	"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/partner"
	"git.rosy.net.cn/jx-callback/globals/api"
)

var (
	curPurchaseHandler *PurchaseHandler
)

type PurchaseHandler struct {
	partner.BasePurchasePlatform
}

func init() {
	if api.ElmAPI != nil {
		curPurchaseHandler = new(PurchaseHandler)
		// partner.RegisterPurchasePlatform(curPurchaseHandler)
	}
}

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

func OnCallbackMsg(msg *elmapi.CallbackMsg) (retVal *elmapi.CallbackResponse) {
	if curPurchaseHandler != nil {
		retVal = curPurchaseHandler.OnCallbackMsg(msg)
	}
	return retVal
}

func (c *PurchaseHandler) OnCallbackMsg(msg *elmapi.CallbackMsg) (retVal *elmapi.CallbackResponse) {
	if msg.Type == elmapi.MsgTypeOrderValid {
		innerMsg := make(map[string]interface{})
		err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
		if err != nil {
			retVal = elmapi.Err2CallbackResponse(err, "")
		} else {
			innerMsg["msgType"] = msg.Type
			retVal = c.OnOrderNewMsg(innerMsg)
		}
	} else if msg.Type > elmapi.MsgTypeOrderValid && msg.Type < elmapi.MsgTypeUserApplyCancel {
		var innerMsg elmapi.CallbackOrderStatusMsg
		err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
		if err != nil {
			retVal = elmapi.Err2CallbackResponse(err, "")
		} else {
			innerMsg.MsgType = msg.Type
			retVal = c.OnOrderStatusMsg(&innerMsg)
		}
	} else if msg.Type >= elmapi.MsgTypeUserApplyCancel && msg.Type < elmapi.MsgTypeUserUrgeOrder {
		var innerMsg elmapi.CallbackOrderCancelRefundMsg
		err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
		if err != nil {
			retVal = elmapi.Err2CallbackResponse(err, "")
		} else {
			innerMsg.MsgType = msg.Type
			retVal = c.OnOrderCancelRefundMsg(&innerMsg)
		}
	} else if msg.Type == elmapi.MsgTypeUserUrgeOrder {
		var innerMsg elmapi.CallbackOrderUrgeMsg
		err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
		if err != nil {
			retVal = elmapi.Err2CallbackResponse(err, "")
		} else {
			innerMsg.MsgType = msg.Type
			jxutils.CallMsgHandler(func() {
				retVal = c.onOrderUserUrgeOrder(&innerMsg)
			}, jxutils.ComposeUniversalOrderID(innerMsg.OrderID, model.VendorIDELM))
		}
	} else if msg.Type >= elmapi.MsgTypeWaybillWait4DeliveryVendor && msg.Type <= elmapi.MsgTypeRejectedSystemError {
		var innerMsg elmapi.CallbackWaybillStatusMsg
		err := utils.UnmarshalUseNumber([]byte(msg.Message), &innerMsg)
		if err != nil {
			retVal = elmapi.Err2CallbackResponse(err, "")
		} else {
			innerMsg.MsgType = msg.Type
			retVal = c.OnWaybillStatusMsg(&innerMsg)
		}
	} else {
		retVal = elmapi.SuccessResponse
	}
	return retVal
}

func (p *PurchaseHandler) UploadImg(ctx *jxcontext.Context, vendorOrgCode, imgURL string, imgData []byte, imgName string, imgType int) (imgHint string, err error) {
	return imgHint, err
}

func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
	return nil, fmt.Errorf("平台%s不支持此操作", jxutils.GetVendorName(model.VendorIDELM))
}
