package fnpsapi

import (
	"time"

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

const (
	OrderCancelReson1  = 1  // 1:物流原因：订单长时间未分配骑手,
	OrderCancelReson2  = 2  // 2:物流原因：分配骑手后，骑手长时间未取件 ,
	OrderCancelReson3  = 3  // 3:物流原因：骑手告知不配送，让取消订单,
	OrderCancelReson4  = 4  // 4:商品缺货/无法出货/已售完,
	OrderCancelReson5  = 5  // 5:商户联系不上门店/门店关门了,
	OrderCancelReson6  = 6  // 6:商户发错单,
	OrderCancelReson7  = 7  // 7:商户/顾客自身定位错误,
	OrderCancelReson8  = 8  // 8:商户改其他第三方配送,
	OrderCancelReson9  = 9  // 9:顾客下错单/临时不想要了,
	OrderCancelReson10 = 10 // 10:顾客自取/不在家/要求另改时间配送）（0类型已下线）

	OrderStatusAccept     = 1  //系统已接单
	OrderStatusAssigned   = 20 //已分配骑手
	OrderStatusArrived    = 80 //已到店
	OrderStatusDelivering = 2  //配送中
	OrderStatusDelivered  = 3  //已送达
	OrderStatusException  = 5  //异常
)

type CreateOrderParam struct {
	PartnerRemark       string         `json:"partner_remark,omitempty"`
	PartnerOrderCode    string         `json:"partner_order_code,omitempty"`
	NotifyURL           string         `json:"notify_url,omitempty"`
	OrderType           int            `json:"order_type,omitempty"`
	ChainStoreCode      string         `json:"chain_store_code,omitempty"`
	TransportInfo       *TransportInfo `json:"transport_info,omitempty"`
	OrderAddTime        int64          `json:"order_add_time,omitempty"`
	OrderTotalAmount    float64        `json:"order_total_amount,omitempty"`
	OrderActualAmount   float64        `json:"order_actual_amount,omitempty"`
	OrderWeight         float64        `json:"order_weight,omitempty"`
	OrderRemark         string         `json:"order_remark,omitempty"`
	IsInvoiced          int            `json:"is_invoiced"`
	Invoice             string         `json:"invoice,omitempty"`
	OrderPaymentStatus  int            `json:"order_payment_status,omitempty"`
	OrderPaymentMethod  int            `json:"order_payment_method,omitempty"`
	IsAgentPayment      int            `json:"is_agent_payment"`
	RequirePaymentPay   float64        `json:"require_payment_pay,omitempty"`
	GoodsCount          int            `json:"goods_count,omitempty"`
	RequireReceiveTime  int64          `json:"require_receive_time,omitempty"`
	SerialNumber        string         `json:"serial_number,omitempty"`
	ReceiverInfo        *ReceiverInfo  `json:"receiver_info,omitempty"`
	ItemsJSON           []*ItemsJSON   `json:"items_json,omitempty"`
	OrderSource         string         `json:"order_source,omitempty"` //饿百订单传109
	ChannelOrderCode    string         `json:"channel_order_code,omitempty"`
	CookingTime         int64          `json:"cooking_time,omitempty"`
	PlatformPaidTime    int64          `json:"platform_paid_time,omitempty"`
	PlatformCreatedTime int64          `json:"platform_created_time,omitempty"`
	MerchantCode        string         `json:"merchant_code,omitempty"`
}

type ReceiverInfo struct {
	ReceiverName         string  `json:"receiver_name,omitempty"`
	ReceiverPrimaryPhone string  `json:"receiver_primary_phone,omitempty"`
	ReceiverSecondPhone  string  `json:"receiver_second_phone,omitempty"`
	ReceiverAddress      string  `json:"receiver_address,omitempty"`
	ReceiverLongitude    float64 `json:"receiver_longitude,omitempty"`
	ReceiverLatitude     float64 `json:"receiver_latitude,omitempty"`
	PositionSource       int     `json:"position_source,omitempty"`
}

type TransportInfo struct {
	TransportName      string  `json:"transport_name,omitempty"`
	TransportAddress   string  `json:"transport_address,omitempty"`
	TransportLongitude float64 `json:"transport_longitude,omitempty"`
	TransportLatitude  float64 `json:"transport_latitude,omitempty"`
	PositionSource     int     `json:"position_source,omitempty"`
	TransportTel       string  `json:"transport_tel,omitempty"`
	TransportRemark    string  `json:"transport_remark,omitempty"`
}

type ItemsJSON struct {
	ItemID             string  `json:"item_id,omitempty"`
	ItemName           string  `json:"item_name,omitempty"`
	ItemQuantity       int     `json:"item_quantity,omitempty"`
	ItemPrice          float64 `json:"item_price"`
	ItemActualPrice    float64 `json:"item_actual_price"`
	ItemSize           int     `json:"item_size,omitempty"`
	ItemRemark         string  `json:"item_remark,omitempty"`
	IsNeedPackage      int     `json:"is_need_package"`
	IsAgentPurchase    int     `json:"is_agent_purchase"`
	AgentPurchasePrice float64 `json:"agent_purchase_price,omitempty"`
}

//https://open.ele.me/documents/%E5%88%9B%E5%BB%BA%E8%9C%82%E9%B8%9F%E8%AE%A2%E5%8D%95
func (a *API) CreateOrder(createOrderParam *CreateOrderParam) (err error) {
	params := utils.Struct2FlatMap(createOrderParam)
	_, err = a.AccessAPI("v2/order", URL, params, true)
	return err
}

//order_cancel_reason_code 订单取消原因代码(1:用户取消,2:商家取消)
// order_cancel_code	订单取消编码（
// 1:物流原因：订单长时间未分配骑手,
// 2:物流原因：分配骑手后，骑手长时间未取件 ,
// 3:物流原因：骑手告知不配送，让取消订单,
// 4:商品缺货/无法出货/已售完, 5:商户联系不上门店/门店关门了, 6:商户发错单,
// 7:商户/顾客自身定位错误, 8:商户改其他第三方配送, 9:顾客下错单/临时不想要了,
// 10:顾客自取/不在家/要求另改时间配送）（0类型已下线）

type CancelOrderParam struct {
	PartnerOrderCode       string `json:"partner_order_code,omitempty"`
	OrderCancelReasonCode  int    `json:"order_cancel_reason_code,omitempty"`
	OrderCancelCode        int    `json:"order_cancel_code,omitempty"`
	OrderCancelDescription string `json:"order_cancel_description,omitempty"`
	OrderCancelTime        int64  `json:"order_cancel_time,omitempty"`
}

func (a *API) CancelOrder(cancelOrderParam *CancelOrderParam) (err error) {
	params := utils.Struct2FlatMap(cancelOrderParam)
	_, err = a.AccessAPI("v2/order/cancel", URL, params, true)
	return err
}

func (a *API) ComplaintOrder(cancelOrderParam *CancelOrderParam) (err error) {
	params := utils.Struct2FlatMap(cancelOrderParam)
	_, err = a.AccessAPI("v2/order/complaint", URL, params, true)
	return err
}

type QueryOrderResult struct {
	AbnormalCode         string      `json:"abnormal_code"`
	AbnormalDesc         string      `json:"abnormal_desc"`
	CarrierDriverID      int         `json:"carrier_driver_id"`
	CarrierDriverName    string      `json:"carrier_driver_name"`
	CarrierDriverPhone   string      `json:"carrier_driver_phone"`
	ComplaintDescription interface{} `json:"complaintDescription"`
	ComplaintStatus      interface{} `json:"complaintStatus"`
	EstimateArriveTime   int64       `json:"estimate_arrive_time"`
	EventLogDetails      []struct {
		CarrierDriverName  string `json:"carrier_driver_name"`
		CarrierDriverPhone string `json:"carrier_driver_phone"`
		OccurTime          int64  `json:"occur_time"`
		OrderStatus        int    `json:"order_status"`
	} `json:"event_log_details"`
	HandleID                   interface{} `json:"handleId"`
	OrderID                    interface{} `json:"orderId"`
	OrderStatus                int         `json:"order_status"`
	OrderTotalDeliveryCost     float64     `json:"order_total_delivery_cost"`
	OrderTotalDeliveryDiscount interface{} `json:"order_total_delivery_discount"`
	OvertimeCompensationCost   float64     `json:"overtime_compensation_cost"`
	Reason                     string      `json:"reason"`
	SupportClaims              bool        `json:"support_claims"`
	TrackingID                 int64       `json:"trackingId"`
	TrackingID2                int64       `json:"tracking_id"`
	TransportStationID         string      `json:"transport_station_id"`
	TransportStationTel        string      `json:"transport_station_tel"`
}

func (a *API) QueryOrder(partnerOrderCode string) (queryOrderResult *QueryOrderResult, err error) {
	result, err := a.AccessAPI("v2/order/query", URL, map[string]interface{}{
		"partner_order_code": partnerOrderCode,
	}, true)
	if err == nil {
		utils.Map2StructByJson(result["data"], &queryOrderResult, false)
	}
	return queryOrderResult, err
}

func (a *API) ComplaintRider(partnerOrderCode string, orderComplaintCode int) (err error) {
	_, err = a.AccessAPI("v2/order/complaint", URL, map[string]interface{}{
		"partner_order_code":   partnerOrderCode,
		"order_complaint_code": orderComplaintCode,
		"order_complaint_time": time.Now().UnixNano() / 1e6,
	}, true)
	return err
}
