package com.yiqi.core.domain.dto.wx;

import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.yiqi.common.config.SystemEnvConfig;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.enums.ClothOrderDeliveryType;
import com.yiqi.common.enums.ClothOrderStatusType;
import com.yiqi.common.enums.SystemEnv;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.core.domain.vo.wx.IntracityExpressPreAddOrderResult;
import com.yiqi.order.domain.OrderCloth;
import com.yiqi.order.domain.OrderDelivery;
import com.yiqi.system.domain.SysStore;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaohei
 * @date 2024/2/4
 */
@Data
public class IntracityExpressAddOrderRequest {

    @JsonProperty("wx_store_id")
    @ApiModelProperty(value = "微信门店编号", required = true)
    private String wx_store_id;

    @JsonProperty("store_order_id")
    @ApiModelProperty(value = "门店订单编号", required = true)
    private String store_order_id;

    @JsonProperty("user_openid")
    @ApiModelProperty(value = "收货用户openid", required = true)
    private String user_openid;

    @JsonProperty("user_name")
    @ApiModelProperty(value = "收件人姓名", required = true)
    private String user_name;

    @JsonProperty("user_phone")
    @ApiModelProperty(value = "收件人手机号", required = true)
    private String user_phone;

    @JsonProperty("user_lng")
    @ApiModelProperty(value = "收件用户位置经度", required = true)
    private double user_lng;

    @JsonProperty("user_lat")
    @ApiModelProperty(value = "收件用户位置维度", required = true)
    private double user_lat;

    @JsonProperty("user_address")
    @ApiModelProperty(value = "收件用户详细地址", required = true)
    private String user_address;

    @JsonProperty("use_sandbox")
    @ApiModelProperty("是否使用沙箱;1:使用沙箱环境;使用测试沙箱环境，不需要充值运费就可以生成测试订单")
    private Integer use_sandbox;

    @JsonProperty("order_detail_path")
    @ApiModelProperty(value = "跳转商家订单页面路径,物流轨迹页面跳转到商家小程序的订单页面路径参数，期望向用户展示商品订单详情", required = true)
    private String order_detail_path;

    @JsonProperty("callback_url")
    @ApiModelProperty(value = "订单状态回调地址")
    private String callback_url;

    @ApiModelProperty(value = "商品信息", required = true)
    private CargoInfo cargo;

    public String toJson() {
        return WxMaGsonBuilder.create().toJson(this);
    }

    @Data
    public static class CargoInfo {
        @JsonProperty("cargo_name")
        @ApiModelProperty(value = "商品名称", required = true)
        private String cargo_name;

        @JsonProperty("cargo_weight")
        @ApiModelProperty(value = "商品总重量", required = true)
        private Integer cargo_weight;

        @JsonProperty("cargo_price")
        @ApiModelProperty(value = "商品总价格", required = true)
        private Integer cargo_price;

        @JsonProperty("cargo_type")
        @ApiModelProperty("商品类型，固定3")
        private Integer cargo_type = 3;

        @JsonProperty("cargo_num")
        @ApiModelProperty(value = "商品数量", required = true)
        private Integer cargo_num;

        @JsonProperty("item_list")
        @ApiModelProperty(value = "物品列表", required = true)
        private List<ItemDetail> item_list = new ArrayList<>();
    }

    @Data
    public static class ItemDetail {
        @JsonProperty("item_name")
        @ApiModelProperty("物品名称")
        private String item_name;

        @JsonProperty("item_pic_url")
        @ApiModelProperty("物品图片")
        private String item_pic_url;

        @ApiModelProperty("物品数量")
        private Integer count;

        public ItemDetail(String item_name, String item_pic_url, Integer count) {
            this.item_name = item_name;
            this.item_pic_url = item_pic_url;
            this.count = count;
        }
    }

    public static IntracityExpressAddOrderRequest build(OrderCloth orderCloth, OrderDelivery orderDelivery, SysStore sysStore, String user_openid) {
        IntracityExpressAddOrderRequest request = new IntracityExpressAddOrderRequest();
        request.setStore_order_id(orderDelivery.getDeliverySn());
        CargoInfo cargoInfo = new CargoInfo();
        cargoInfo.setCargo_name("服装");

        if (orderDelivery.getClothOrderType().equals(ClothOrderDeliveryType.collectCloth.getCode())) {
            cargoInfo.setCargo_num(orderCloth.getAppointmentCount());
            cargoInfo.setCargo_weight(orderCloth.getAppointmentCount() / 5 + 1);
            cargoInfo.getItem_list().add(new ItemDetail("待洗衣物", sysStore.getCompanyPic(), orderCloth.getAppointmentCount()));
        } else {
            cargoInfo.setCargo_num(orderCloth.getOrderClothCount());
            cargoInfo.setCargo_weight(orderCloth.getOrderClothCount() / 5 + 1);
            cargoInfo.getItem_list().add(new ItemDetail("洗好衣物", sysStore.getCompanyPic(), orderCloth.getOrderClothCount()));
        }

        cargoInfo.setCargo_price(1000);
        request.setCargo(cargoInfo);
        request.setWx_store_id(sysStore.getWxStoreId());
        request.setOrder_detail_path("/pageExt/makes/detail?type=order&id=" + orderCloth.getId());
        request.setCallback_url(SystemEnvConfig.baseUrl + "/" + UrlConstants.managerApi + "/store/intracity/express/statusChange");
        request.setUse_sandbox(SystemEnvConfig.env.equals(SystemEnv.PROD.getCode()) ? 0 : 1);
        request.setUser_openid(user_openid);
        if (orderCloth.getOrderStatus().equals(ClothOrderStatusType.APPOINTMENT.getCode())) {
            throw new ServiceException("同城配送暂不支持上门取件");
        } else {
            if (SystemEnvConfig.env.equals(SystemEnv.PROD.getCode())) {
                request.setUser_name(orderDelivery.getUserName());
                request.setUser_phone(orderDelivery.getUserPhone());
                request.setUser_address(orderDelivery.getUserAddress());
            } else {
                request.setUser_name("顺丰同城");
                request.setUser_phone("13881979410");
                request.setUser_address("北京市海淀区学清嘉创大厦A座15层");
            }

            request.setUser_lat(Double.parseDouble(orderDelivery.getUserLat()));
            request.setUser_lng(Double.parseDouble(orderDelivery.getUserLng()));
        }
        return request;
    }
}
