package partner

import (
	"fmt"
	"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"
)

const (
	WaybillFeeErrCodeCourierNotOpen      = 1  //配送门店没有启用
	WaybillFeeErrCodeCourierNotSupported = 2  //配送门店不被系统支持
	WaybillFeeErrCodeCourierForbidden    = 3  //配送门店内部禁用
	WaybillFeeErrCodeCourierOthers       = 10 //其它错误
)

type WaybillFeeInfo struct {
	ErrCode        int            `json:"errCode"`
	ErrStr         string         `json:"errStr"`
	RefDeliveryFee int64          `json:"refDeliveryFee"` // 优惠后
	RefAddFee      int64          `json:"refAddFee"`      // 原始配送费
	DeliveryFee    int64          `json:"deliveryFee"`    // 优惠后
	TimeoutSecond  int            `json:"timeoutSecond"`  // 系统会自动发运单的倒计时
	Waybill        *model.Waybill `json:"waybill"`
}

type CreateWaybillPolicyFunc func(refDeliveryFee, refAddFee, deliveryFee int64) (errStr string)

type IDeliveryPlatformHandler interface {
	GetVendorID() int

	CreateStore(ctx *jxcontext.Context, storeDetail *dao.StoreDetail2) (vendorStoreID string, status int, err error)
	GetStore(ctx *jxcontext.Context, storeID int, vendorStoreID string) (storeDetail *dao.StoreDetail2, err error)
	IsErrStoreNotExist(err error) bool
	IsErrStoreExist(err error) bool

	CreateWaybill(order *model.GoodsOrder, maxDeliveryFee int64) (bill *model.Waybill, err error)
	CancelWaybill(bill *model.Waybill, cancelReasonID int, cancelReason string) (err error)
	GetWaybillFee(order *model.GoodsOrder) (deliveryFeeInfo *WaybillFeeInfo, err error)
	//投诉骑手
	ComplaintRider(bill *model.Waybill, resonID int, resonContent string) (err error)
	// GetRiderInfo 获取骑手信息
	GetRiderInfo(orderId string, deliveryId int64, mtPeisongId string) (rider *utils.RiderInfo, err error)
	// 三方配送时,呼叫骑手在取消.可能存在违约赔付!获取每单的违约金额!
	// 蜂鸟:骑手接单后-取餐之前,每单扣除2元,超过20分钟不扣款
	// 达达:骑手接单后-到店之前,接单1-15分钟内,扣款2元,超过不扣款
	// uu :接单后超过一定时间取消会产生违约金，取消后扣除费用退回剩余订单金额
	// 美团配送: 暂无

	// GetDeliverLiquidatedDamages 获取配送单的违约金:京西订单id:orderId,配送方id:deliverId
	GetDeliverLiquidatedDamages(orderId string, deliverId string) (money int64, err error)
	// GetPlatformBalance 获取平台账户余额
	GetPlatformBalance() (balance int64, err error)
	// BalanceRecharge 充值配送平台
	BalanceRecharge(param *utils.RechargeBalance) (url string, err error)
}

type IDeliveryUpdateStoreHandler interface {
	UpdateStore(ctx *jxcontext.Context, storeDetail *dao.StoreDetail2) (err error)
}

type DeliveryPlatformHandlerInfo struct {
	Handler           IDeliveryPlatformHandler
	Use4CreateWaybill bool
}

var (
	DeliveryPlatformHandlers map[int]*DeliveryPlatformHandlerInfo
	UseableDeliveryVendorIDs []int
)

func init() {
	DeliveryPlatformHandlers = make(map[int]*DeliveryPlatformHandlerInfo)
}

func RegisterDeliveryPlatform(handler IDeliveryPlatformHandler, isUse4CreateWaybill bool) {
	vendorID := handler.GetVendorID()
	if !(model.IsDeliveryVendorExist(vendorID)) {
		panic(fmt.Sprintf("delivery vendor:%d is illegal", vendorID))
	}
	if _, ok := DeliveryPlatformHandlers[vendorID]; ok {
		panic(fmt.Sprintf("delivery vendor:%d, already exists", vendorID))
	}
	DeliveryPlatformHandlers[vendorID] = &DeliveryPlatformHandlerInfo{
		Handler:           handler,
		Use4CreateWaybill: isUse4CreateWaybill,
	}
	UseableDeliveryVendorIDs = append(UseableDeliveryVendorIDs, vendorID)
}

func GetDeliveryPlatformFromVendorID(vendorID int) *DeliveryPlatformHandlerInfo {
	return DeliveryPlatformHandlers[vendorID]
}
