package wsc

import (
	"errors"
	"fmt"
	"time"

	"git.rosy.net.cn/baseapi/platformapi/weimobapi"
	"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/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/business/partner/delivery/dada"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

const (
	FakeOrderStatusAccepted       = "fakeaccepted"
	FakeOrderStatusFinishedPickup = "fakefinishedpickup"
)

const (
	maxAddFee = 300 // 最大增加费用，单位为分，超过不发三方配送了
)

var (
	VendorStatus2StatusMap = map[string]int{
		utils.Int2Str(weimobapi.OrderStatusWait4Pay):   model.OrderStatusWait4Pay,
		utils.Int2Str(weimobapi.OrderStatusPayed):      model.OrderStatusNew,
		FakeOrderStatusAccepted:                        model.OrderStatusAccepted,
		FakeOrderStatusFinishedPickup:                  model.OrderStatusFinishedPickup,
		utils.Int2Str(weimobapi.OrderStatusDelivering): model.OrderStatusDelivering,
		utils.Int2Str(weimobapi.OrderStatusFinished):   model.OrderStatusFinished,
		utils.Int2Str(weimobapi.OrderStatusCanceled):   model.OrderStatusCanceled,

		weimobapi.MsgEventCreateRights: model.OrderStatusLocked,
	}
)

func (p *PurchaseHandler) onOrderMsg(msg *weimobapi.CallbackMsg) (response *weimobapi.CallbackResponse) {
	var (
		err          error
		status       *model.OrderStatus
		orderMapData map[string]interface{}
	)
	if msg.IsFake || weimobapi.MsgEventCreateOrder == msg.Event || weimobapi.MsgEventOrderStatusChange == msg.Event {
		orderMapData, err = api.WeimobAPI.QueryOrderDetail(msg.OrderNo, false)
		if err == nil {
			status = p.callbackMsg2Status(msg, orderMapData)
			if msg.Event == weimobapi.MsgEventCreateOrder || status.Status == model.OrderStatusNew {
				order := p.Map2Order(orderMapData)
				order.StatusTime = msg.StatusTime
				err = partner.CurOrderManager.OnOrderNew(order, status)
			} else {
				err = partner.CurOrderManager.OnOrderStatusChanged("", status)
			}
		}
	}
	return weimobapi.Err2CallbackResponse(err, "")
}

func (p *PurchaseHandler) callbackMsg2Status(msg *weimobapi.CallbackMsg, orderMapData map[string]interface{}) (orderStatus *model.OrderStatus) {
	orderID := utils.Int64ToStr(msg.OrderNo)
	var vendorStatus string
	if msg.IsFake {
		vendorStatus = msg.Event
	} else {
		if msg.Event == weimobapi.MsgEventCreateRights {
			vendorStatus = msg.Event
		} else {
			vendorStatus = p.getOrderStatus(orderMapData)
		}
	}
	orderStatus = &model.OrderStatus{
		VendorOrderID:    orderID,
		VendorID:         model.VendorIDWSC,
		OrderType:        model.OrderTypeOrder,
		RefVendorOrderID: orderID,
		RefVendorID:      model.VendorIDWSC,
		VendorStatus:     vendorStatus,
		Status:           p.getStatusFromVendorStatus(vendorStatus),
		StatusTime:       msg.StatusTime,
		Remark:           "",
	}
	return orderStatus
}

func (p *PurchaseHandler) getOrderStatus(orderMapData map[string]interface{}) (status string) {
	return utils.Int64ToStr(utils.MustInterface2Int64(orderMapData["orderStatus"]))
}

func (p *PurchaseHandler) getStatusFromVendorStatus(vendorStatus string) int {
	if status, ok := VendorStatus2StatusMap[vendorStatus]; ok {
		return status
	}
	return model.OrderStatusUnknown
}

func (p *PurchaseHandler) GetOrder(vendorOrgCode, vendorOrderID string) (order *model.GoodsOrder, err error) {
	result, err := api.WeimobAPI.QueryOrderDetail(utils.Str2Int64(vendorOrderID), false)
	if err != nil {
		return nil, err
	}
	order = p.Map2Order(result)
	return order, err
}

func (p *PurchaseHandler) GetOrderStatus(vendorOrgCode, vendorOrderID string) (status int, err error) {
	return 0, fmt.Errorf("未实现")
}

func (p *PurchaseHandler) Map2Order(orderData map[string]interface{}) (order *model.GoodsOrder) {
	result := orderData
	vendorOrderID := utils.Int64ToStr(utils.MustInterface2Int64(result["orderNo"]))
	deliveryDetail := result["deliveryDetail"].(map[string]interface{})
	logisticsDeliveryDetail, _ := deliveryDetail["logisticsDeliveryDetail"].(map[string]interface{})
	// paymentInfo := result["paymentInfo"].(map[string]interface{})
	order = &model.GoodsOrder{
		VendorOrderID: vendorOrderID,
		VendorID:      model.VendorIDWSC,
		VendorStoreID: utils.Int64ToStr(utils.MustInterface2Int64(result["processStoreId"])), // 这个不是通常意义上的vendor store id
		// StoreID
		BuyerComment:          utils.Interface2String(result["buyerRemark"]),
		ExpectedDeliveredTime: utils.DefaultTimeValue,
		PickDeadline:          utils.DefaultTimeValue,
		VendorStatus:          utils.Int64ToStr(utils.MustInterface2Int64(result["orderStatus"])),
		StatusTime:            utils.Timestamp2Time(utils.MustInterface2Int64(result["createTime"]) / 1000),
		OrderCreatedAt:        utils.Timestamp2Time(utils.MustInterface2Int64(result["createTime"]) / 1000),
		OriginalData:          string(utils.MustMarshal(result)),
		ActualPayPrice:        jxutils.StandardPrice2Int(utils.MustInterface2Float64(result["totalAmount"])),
		BusinessType:          model.BusinessTypeImmediate,
	}
	if logisticsDeliveryDetail != nil {
		order.StoreName = utils.Interface2String(logisticsDeliveryDetail["processStoreTitle"])
		order.ConsigneeName = utils.Interface2String(logisticsDeliveryDetail["receiverName"])
		order.ConsigneeMobile = utils.Interface2String(logisticsDeliveryDetail["receiverMobile"])
		order.ConsigneeAddress = utils.Interface2String(logisticsDeliveryDetail["receiverAddress"])
		order.ConsigneeLng = jxutils.StandardCoordinate2Int(utils.Str2Float64(utils.Interface2String(logisticsDeliveryDetail["receiverLongitude"])))
		order.ConsigneeLat = jxutils.StandardCoordinate2Int(utils.Str2Float64(utils.Interface2String(logisticsDeliveryDetail["receiverLatitude"])))
		order.CoordinateType = model.CoordinateTypeMars
		// if expectDeliveryTimeStr, ok := logisticsDeliveryDetail["expectDeliveryTime"].(string); ok {
		// 	if expectDeliveryTimeStr =
		// }
	} else {
		selfPickupDetail, _ := deliveryDetail["selfPickupDetail"].(map[string]interface{})
		if selfPickupDetail != nil {
			order.StoreName = utils.Interface2String(selfPickupDetail["selfPickupSiteName"])
		}
	}
	order.Status = p.getStatusFromVendorStatus(order.VendorStatus)
	itemList := result["itemList"].([]interface{})
	for _, v := range itemList {
		item := v.(map[string]interface{})
		skuName := utils.Interface2String(item["goodsTitle"])
		_, _, _, specUnit, _, specQuality := jxutils.SplitSkuName(skuName)
		sku := &model.OrderSku{
			VendorOrderID: vendorOrderID,
			VendorID:      model.VendorIDWSC,
			Count:         int(utils.MustInterface2Int64(item["skuNum"])),
			SkuID:         int(utils.Str2Int64WithDefault(utils.Interface2String(item["skuCode"]), 0)),
			VendorSkuID:   utils.Int64ToStr(utils.MustInterface2Int64(item["skuId"])),
			SkuName:       skuName,
			Weight:        jxutils.FormatSkuWeight(specQuality, specUnit), // 订单信息里没有重量，只有名字里尝试找
			SalePrice:     jxutils.StandardPrice2Int(utils.MustInterface2Float64(item["price"])),
		}
		order.Skus = append(order.Skus, sku)
	}
	jxutils.RefreshOrderSkuRelated(order)
	if logisticsDeliveryDetail != nil {
		p.arrangeSaleStore(order, utils.Interface2String(logisticsDeliveryDetail["receiverCity"]), utils.Interface2String(logisticsDeliveryDetail["receiverProvince"]))
	}
	p.setStoreOrderSeq(order)
	return order
}

func (p *PurchaseHandler) AcceptOrRefuseOrder(order *model.GoodsOrder, isAcceptIt bool, userName string) (err error) {
	if !isAcceptIt {
		if globals.EnableWscStoreWrite {
			err = api.WeimobAPI.CancelOrder(utils.Str2Int64(order.VendorOrderID), "")
		}
	} else {
		if jxutils.GetSaleStoreIDFromOrder(order) != 0 {
			// 微商城没有确认，只有取消，模拟接受
			p.postFakeMsg(utils.Str2Int64(order.VendorOrderID), FakeOrderStatusAccepted)
		} else {
			if globals.EnableWscStoreWrite {
				err = api.WeimobAPI.CancelOrder(utils.Str2Int64(order.VendorOrderID), "没有找到合适的配送店")
			}
		}
	}
	return err
}

func (p *PurchaseHandler) PickupGoods(order *model.GoodsOrder, isSelfDelivery bool, userName string) (err error) {
	if globals.EnableWscStoreWrite && !isSelfDelivery {
		_, err = dada.CurDeliveryHandler.CreateWaybill(order, 0)
	}
	// 微商城没有拣货完成，模拟
	p.postFakeMsg(utils.Str2Int64(order.VendorOrderID), FakeOrderStatusFinishedPickup)
	return err
}

func (p *PurchaseHandler) AcceptOrRefuseFailedGetOrder(ctx *jxcontext.Context, order *model.GoodsOrder, isAcceptIt bool) (err error) {
	return err
}

func (p *PurchaseHandler) CallCourier(ctx *jxcontext.Context, order *model.GoodsOrder) (err error) { // 拣货失败后再次招唤平台配送
	return err
}

func (p *PurchaseHandler) ConfirmReceiveGoods(ctx *jxcontext.Context, order *model.GoodsOrder) (err error) { // 投递失败后确认收到退货
	return err
}

// 将订单从购物平台配送转为自送
func (p *PurchaseHandler) Swtich2SelfDeliver(order *model.GoodsOrder, userName string) (err error) {
	if globals.EnableWscStoreWrite {
		err = api.WeimobAPI.DeliveryOrder(&weimobapi.DeliveryOrder{
			OrderNo: utils.Str2Int64(order.VendorOrderID),
		})
	}
	return err
}

// 将订单从购物平台配送转为自送后又送达
func (p *PurchaseHandler) Swtich2SelfDelivered(order *model.GoodsOrder, userName string) (err error) {
	return err
}

// 完全自送的门店表示开始配送
func (p *PurchaseHandler) SelfDeliverDelivering(order *model.GoodsOrder, userName string) (err error) {
	if globals.EnableWscStoreWrite {
		err = api.WeimobAPI.DeliveryOrder(&weimobapi.DeliveryOrder{
			OrderNo: utils.Str2Int64(order.VendorOrderID),
		})
	}
	return err
}

// 完全自送的门店表示配送完成
func (p *PurchaseHandler) SelfDeliverDelivered(order *model.GoodsOrder, userName string) (err error) {
	return err
}

func (p *PurchaseHandler) postFakeMsg(orderNo int64, fakeStatus string) {
	msg := &weimobapi.CallbackMsg{
		IsFake:     true,
		Event:      fakeStatus,
		OrderNo:    orderNo,
		StatusTime: time.Now(),
	}
	utils.CallFuncAsync(func() {
		OnCallbackMsg(msg)
	})
}

func (p *PurchaseHandler) arrangeSaleStore(order *model.GoodsOrder, cityName, provinceName string) {

}

func (p *PurchaseHandler) setStoreOrderSeq(order *model.GoodsOrder) {
	if order.StoreID > 0 {
		year, month, day := order.StatusTime.Date()
		dateBegin := time.Date(year, month, day, 0, 0, 0, 0, order.StatusTime.Location())
		// dateEnd := date1.Add(time.Hour * 24)
		sql := `
		SELECT COUNT(*) ct
		FROM goods_order t1
		WHERE t1.store_id = ? AND t1.order_created_at >= ? AND t1.vendor_id = ?
		`
		var count int
		db := dao.GetDB()
		if err := dao.GetRow(db, &count, sql, order.StoreID, dateBegin, model.VendorIDWSC); err == nil {
			order.OrderSeq = count + 1
		} else {
			globals.SugarLogger.Errorf("setStoreOrderSeq orderID:%s failed with error:%v", order.VendorOrderID, err)
		}
	}
}

func (c *PurchaseHandler) GetOrderRealMobile(ctx *jxcontext.Context, order *model.GoodsOrder) (mobile string, err error) {
	err = errors.New("微盟微商城还未实现GetOrderRealMobile")
	return mobile, err
}

func (c *PurchaseHandler) AgreeOrRefuseCancel(ctx *jxcontext.Context, order *model.GoodsOrder, isAgree bool, reason string,refundParam interface{}) (err error) {
	return err
}

func (c *PurchaseHandler) CancelOrder(ctx *jxcontext.Context, order *model.GoodsOrder, reason string) (err error) {
	return err
}

func (c *PurchaseHandler) AdjustOrder(ctx *jxcontext.Context, order *model.GoodsOrder, removedSkuList []*model.OrderSku, reason string) (err error) {
	return err
}

// 转自配送时取消非专送混合送门店取消理由
func (c *PurchaseHandler) GetCancelDeliveryReason(order *model.GoodsOrder) (string, error) {
	return "", nil
}

// 取消美团外卖理由转使用三方配送
func (c *PurchaseHandler) CancelLogisticsByWmOrderId(order *model.GoodsOrder, reasonCode, detailContent, appPoiCode, orderId string) error {
	return nil
}

// 获取订单配送状态
func (c *PurchaseHandler) OrderLogisticsStatus(orderId string) (*utils.RiderInfo, error) {
	return nil, nil
}

// GetOrderSettleAccounts 获取订单结算信息
func (c *PurchaseHandler) GetOrderSettleAccounts(order *model.GoodsOrder) (int64, error) {
	return 0, nil
}

// ApplyCompensationOrder 订单索赔
func (c *PurchaseHandler) ApplyCompensationOrder(order *model.GoodsOrder) (string, error) {
	return "", nil
}

// UploadPickingImg  上传拣货图片
func (c *PurchaseHandler) UploadPickingImg(order *model.GoodsOrder, imgList interface{}) error {
	return nil
}
