package elmapi

import (
	"time"

	"git.rosy.net.cn/baseapi/utils"
)

const (
	OrderStatusPending     = "pending"
	OrderStatusUnprocessed = "unprocessed"
	OrderStatusRefunding   = "refunding"
	OrderStatusValid       = "valid"
	OrderStatusInvalid     = "invalid"
	OrderStatusSettled     = "settled"
)

const (
	CancelOrderTypeOthers                          = "others"
	CancelOrderTypeFakeOrder                       = "fakeOrder"
	CancelOrderTypeContactUserFailed               = "contactUserFailed"
	CancelOrderTypeFoodSoldOut                     = "foodSoldOut"
	CancelOrderTypeRestaurantClosed                = "restaurantClosed"
	CancelOrderTypeDistanceTooFar                  = "distanceTooFar"
	CancelOrderTypeRestaurantTooBusy               = "restaurantTooBusy"
	CancelOrderTypeForceRejectOrder                = "forceRejectOrder"
	CancelOrderTypeDeliveryFault                   = "deliveryFault"
	CancelOrderTypeNotSatisfiedDeliveryRequirement = "notSatisfiedDeliveryRequirement"
)

const (
	RefundStatusNoRefund    = "noRefund"
	RefundStatusApplied     = "applied"
	RefundStatusRejected    = "rejected"
	RefundStatusArbitrating = "arbitrating"
	RefundStatusFailed      = "failed"
	RefundStatusSuccessful  = "successful"
)

const (
	RoleOrderUser       = 1
	RoleSystem          = 2
	RoleMerchert        = 3
	RoleCustomerService = 4
	RoleOpenPlatform    = 5
	RoleSMS             = 6
	RolePrinter         = 7
	RoleRiskController  = 8
	RoleOrderFinished   = 9
)

const (
	MaxGetAllOrdersPageSize = 50
)

func (a *API) GetOrder(orderID string) (map[string]interface{}, error) {
	result, err := a.AccessAPI("eleme.order.getOrder", map[string]interface{}{
		"orderId": orderID,
	})
	if err == nil {
		innerResult := result.Result.(map[string]interface{})
		return innerResult, nil
	}
	return nil, err
}

func (a *API) ConfirmOrder(orderID string) error {
	_, err := a.AccessAPI("eleme.order.confirmOrderLite", map[string]interface{}{
		"orderId": orderID,
	})
	return err
}

func (a *API) CancelOrder(orderID string, cancelType string, remark string) error {
	_, err := a.AccessAPI("eleme.order.cancelOrderLite", map[string]interface{}{
		"orderId": orderID,
		"type":    cancelType,
		"remark":  remark,
	})
	return err
}

// 配送异常或者物流拒单后选择自行配送(推荐)
func (a *API) DeliveryBySelfLite(orderID string) error {
	_, err := a.AccessAPI("eleme.order.deliveryBySelfLite", map[string]interface{}{
		"orderId": orderID,
	})
	return err
}

// 订单确认送出(自配送)
func (a *API) StartDeliveryBySelf(orderID, courierPhone string) error {
	_, err := a.AccessAPI("eleme.order.startDeliveryBySelf", map[string]interface{}{
		"orderId": orderID,
		"phone":   courierPhone,
	})
	return err
}

// 订单确认送达(自配送)
func (a *API) CompleteDeliveryBySelf(orderID, courierPhone string) error {
	_, err := a.AccessAPI("eleme.order.completeDeliveryBySelf", map[string]interface{}{
		"orderId": orderID,
		"phone":   courierPhone,
	})
	return err
}

func (a *API) GetAllOrders(shopId string, pageNo, pageSize int, date time.Time) (orderList []map[string]interface{}, err error) {
	result, err := a.AccessAPI("eleme.order.getAllOrders", map[string]interface{}{
		"shopId":   shopId,
		"pageNo":   pageNo,
		"pageSize": pageSize,
		"date":     date.Format("2006-01-02"),
	})
	if err == nil {
		innerResult := result.Result.(map[string]interface{})
		return utils.Slice2MapSlice(innerResult["list"].([]interface{})), nil
	}
	return nil, err
}
