package com.cool.store.request;

import com.cool.store.entity.PointDetailInfoDO;
import com.cool.store.entity.PointInfoDO;
import com.cool.store.enums.StorageStatusEnum;
import com.cool.store.utils.GeoMapUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

@Data
public class MiniAddPointRequest {

    @NotNull
    @ApiModelProperty("店铺id")
    private Long shopId;

    @NotBlank
    @ApiModelProperty("铺位名称")
    private String pointName;

    @ApiModelProperty("所属大区")
    private Long regionId;

    @NotBlank
    @ApiModelProperty("经度")
    private String longitude;

    @NotBlank
    @ApiModelProperty("纬度")
    private String latitude;

    @NotBlank
    @ApiModelProperty("详细地址")
    private String address;

    @NotNull
    @Min(1)
    @Max(2)
    @ApiModelProperty("经营状况 1营业中 2空铺")
    private Integer businessStatus;

    @NotBlank
    @ApiModelProperty("使用面积(一楼)")
    private String pointArea;

    @Min(1)
    @Max(4)
    @ApiModelProperty("支付方式 1.月付 2.季付 3.半年付 4.年付")
    private Integer paymentMethod;

    @NotBlank
    @ApiModelProperty("转让费")
    private String transferFee;

    @NotBlank
    @ApiModelProperty("图片对象")
    private String pictureObj;

    @ApiModelProperty(value = "当前线索id", hidden = true)
    private Long curLineId;

    @NotBlank
    @ApiModelProperty("加盟商签名")
    private String lineSign;

    @NotBlank
    @ApiModelProperty("省")
    private String province;

    @NotBlank
    @ApiModelProperty("市")
    private String city;

    @NotBlank
    @ApiModelProperty("区/县")
    private String district;

    @ApiModelProperty("街道")
    private String township;


    public static PointDetailInfoDO convertDO(MiniAddPointRequest request) {
        PointDetailInfoDO result = new PointDetailInfoDO();
        result.setBusinessStatus(request.getBusinessStatus());
        result.setPaymentMethod(request.getPaymentMethod());
        result.setTransferFee(request.getTransferFee());
        result.setPictureObj(request.getPictureObj());
        result.setLineSign(request.getLineSign());
        result.setLineSignTime(new Date());
        return result;
    }

    public static PointInfoDO convertPointDO(MiniAddPointRequest request) {
        PointInfoDO result = new PointInfoDO();
        result.setPointName(request.getPointName());
        result.setRegionId(request.getRegionId());
        result.setPointArea(request.getPointArea());
        result.setLatitude(request.getLatitude());
        result.setLongitude(request.getLongitude());
        result.setAddress(request.getAddress());
        result.setProvince(request.getProvince());
        result.setCity(request.getCity());
        result.setDistrict(request.getDistrict());
        result.setTownship(request.getTownship());
        if(StringUtils.isAnyBlank(result.getProvince(),result.getCity(), result.getDistrict(), result.getTownship())){
            GeoMapUtil.AddressInfo addressInfo = GeoMapUtil.reverseGeoCoding(result.getLatitude(), result.getLongitude());
            if(Objects.nonNull(addressInfo)){
                if(StringUtils.isBlank(result.getProvince())){
                    result.setProvince(addressInfo.getProvince());
                }
                if(StringUtils.isBlank(result.getCity())){
                    result.setCity(addressInfo.getCity());
                }
                if(StringUtils.isBlank(result.getDistrict())){
                    result.setDistrict(addressInfo.getDistrict());
                }
                if(StringUtils.isBlank(result.getTownship())){
                    result.setTownship(addressInfo.getTownship());
                }
            }
        }
        result.setStorageStatus(StorageStatusEnum.NOT_IN_STORAGE.getCode());
        return result;
    }

}