package order

import (
	"fusenapi/constants"
	"fusenapi/utils/configs"
	"fusenapi/utils/format"
)

// 获取订单生产状态
func GetOrderStatus(orderStatus constants.Order, deliveryMethod constants.DeliveryMethod) constants.Order {
	switch orderStatus {
	//已支付
	case constants.STATUS_NEW_PART_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_SURE:
		return constants.STATUS_FONT_PAID
	//生产中
	case constants.STATUS_NEW_PRODUTING, constants.STATUS_NEW_PRODUT_COMPLETED:
		return constants.STATUS_FONT_PRODUCTION
	//运输中-直邮单
	case constants.STATUS_NEW_DELIVER, constants.STATUS_NEW_UPS:
		return constants.STATUS_FONT_SHIPPED
	//到达-云仓
	case constants.STATUS_NEW_ARRIVAL:
		return constants.STATUS_FONT_INVENTORY
	//订单完成
	case constants.STATUS_NEW_COMPLETED:
		if deliveryMethod == constants.DELIVERY_METHOD_CLOUD {
			return constants.STATUS_FONT_COMPLETED_CLOUD
		}
		return constants.STATUS_FONT_COMPLETED
	//订单关闭
	default:
		return constants.STATUS_FONT_CLOSED
	}
}

type GetOrderStatusAndLogisticsReq struct {
	OrderStatus    constants.Order
	DeliveryMethod constants.DeliveryMethod
	IsPayCompleted int64
	OrderCtime     int64

	SureTime       int64
	ProductTime    int64
	ProductEndtime int64
	DeliverTime    int64
	UpsDeliverTime int64
	UpsTime        int64
	ArrivalTime    int64
	RecevieTime    int64

	WebSetTimeInfo configs.WebSetTimeInfo
}

type GetOrderStatusAndLogisticsRes struct {
	OrderStatus     constants.Order
	LogisticsStatus constants.Order

	Times []GetOrderStatusAndLogisticsResTimes
}

type GetOrderStatusAndLogisticsResTimes struct {
	Key  int    `json:"key"`
	Time string `json:"time"`
}

// 获取订单物流状态
func GetOrderStatusAndLogistics(req GetOrderStatusAndLogisticsReq) (res GetOrderStatusAndLogisticsRes) {
	var status constants.Order
	logisticsStatus := constants.LOGISTICS_STATUS_DRAW

	times := make([]GetOrderStatusAndLogisticsResTimes, 5)
	m := 1
	for i := 0; i < 5; i++ {
		times[i] = GetOrderStatusAndLogisticsResTimes{
			Key:  m,
			Time: "",
		}
		m++
	}

	switch req.OrderStatus {
	//已支付
	case constants.STATUS_NEW_PART_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_SURE:
		status = constants.STATUS_FONT_PAID
		logisticsStatus = constants.LOGISTICS_STATUS_DRAW
	//生产中
	case constants.STATUS_NEW_PRODUTING, constants.STATUS_NEW_PRODUT_COMPLETED:
		//直邮单有完成的物流状态
		if req.DeliveryMethod == constants.DELIVERY_METHOD_ADDRESS {
			if req.OrderStatus == constants.STATUS_NEW_PRODUTING {
				logisticsStatus = constants.LOGISTICS_STATUS_DRAW
			} else {
				logisticsStatus = constants.LOGISTICS_STATUS_SHIPPING
			}
		}
		status = constants.STATUS_FONT_PRODUCTION
	//运输中-直邮单
	case constants.STATUS_NEW_DELIVER, constants.STATUS_NEW_UPS:
		if req.DeliveryMethod == constants.DELIVERY_METHOD_ADDRESS {
			if req.OrderStatus == constants.STATUS_NEW_DELIVER {
				logisticsStatus = constants.LOGISTICS_STATUS_UPS
			} else {
				logisticsStatus = constants.LOGISTICS_STATUS_UPS_ARRIVAL
			}
		}
		status = constants.STATUS_FONT_SHIPPED
	//到达-云仓
	case constants.STATUS_NEW_ARRIVAL:
	case constants.STATUS_NEW_COMPLETED:
		if req.DeliveryMethod == constants.DELIVERY_METHOD_CLOUD {
			status = constants.STATUS_FONT_COMPLETED_CLOUD
			logisticsStatus = constants.LOGISTICS_STATUS_UPS
		} else {
			status = constants.STATUS_FONT_COMPLETED
			logisticsStatus = constants.LOGISTICS_STATUS_ARRIVAL
		}

	//订单关闭
	default:
		status = constants.STATUS_FONT_CLOSED
	}
	res.OrderStatus = status
	res.LogisticsStatus = logisticsStatus

	var daySecond int64 = 3600 * 24

	if req.DeliveryMethod == constants.DELIVERY_METHOD_ADDRESS {
		switch logisticsStatus {
		case constants.LOGISTICS_STATUS_DRAW:
			times[1].Time = format.TimeIntToFormat(req.OrderCtime + req.WebSetTimeInfo.ProductDay*daySecond)
		case constants.LOGISTICS_STATUS_SHIPPING:
			if req.ProductEndtime > 0 {
				times[2].Time = format.TimeIntToFormat(req.ProductEndtime + req.WebSetTimeInfo.FactoryDeliverDay*daySecond)
			} else {
				times[2].Time = format.TimeIntToFormat(req.OrderCtime + req.WebSetTimeInfo.FactoryDeliverDay*daySecond)
			}
		case constants.LOGISTICS_STATUS_UPS:
			if req.DeliverTime > 0 {
				times[2].Time = format.TimeIntToFormat(req.DeliverTime)
				times[3].Time = format.TimeIntToFormat(req.DeliverTime + req.WebSetTimeInfo.DeliverUpsDay*daySecond)
			}
		case constants.LOGISTICS_STATUS_UPS_ARRIVAL:
			if req.DeliverTime > 0 {
				times[2].Time = format.TimeIntToFormat(req.DeliverTime)
			}
			if req.UpsDeliverTime > 0 {
				times[3].Time = format.TimeIntToFormat(req.UpsDeliverTime)
				times[4].Time = format.TimeIntToFormat(req.UpsDeliverTime + req.WebSetTimeInfo.UpsTransDay*daySecond)
			}
		case constants.LOGISTICS_STATUS_ARRIVAL:
			if req.DeliverTime > 0 {
				times[2].Time = format.TimeIntToFormat(req.DeliverTime)
			}
			if req.UpsDeliverTime > 0 {
				times[3].Time = format.TimeIntToFormat(req.UpsDeliverTime)
			}
			if req.UpsTime > 0 {
				times[4].Time = format.TimeIntToFormat(req.UpsTime)
			}
		}
		res.Times = times
	} else {
		timesData := times[0:3]
		if logisticsStatus == constants.LOGISTICS_STATUS_DRAW {
			timesData[1].Time = format.TimeIntToFormat(req.OrderCtime + req.WebSetTimeInfo.ProductDay*daySecond)
		}
		res.Times = timesData
	}
	return res
}