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.persistence.Column;
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 {


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

    @NotBlank(message = "铺位名称不能为空")
    @ApiModelProperty("铺位名称")
    private String pointName;

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

    @NotBlank(message = "经度不能为空")
    @ApiModelProperty("经度")
    private String longitude;

    @NotBlank(message = "纬度不能为空")
    @ApiModelProperty("纬度")
    private String latitude;

    @NotBlank(message = "详细地址不能为空")
    @ApiModelProperty("详细地址")
    private String address;

    @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;

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

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

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

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

    @NotBlank(message = "省不能为空")
    @ApiModelProperty("省")
    private String province;

    @NotBlank(message = "市不能为空")
    @ApiModelProperty("市")
    private String city;

    @NotBlank(message = "区/县不能为空")
    @ApiModelProperty("区/县")
    private String district;

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

    @ApiModelProperty("机会点编号")
    private String opportunityPointCode;

    @ApiModelProperty("机会点名称")
    private String opportunityPointName;

    @ApiModelProperty("省编码")
    private String provinceCode;

    @ApiModelProperty("市编码")
    private String cityCode;

    @ApiModelProperty("区/县编码")
    private String districtCode;
    @NotBlank(message = "铺位经纬度geohash不能为空")
    @ApiModelProperty("铺位经纬度geohash")
    private String location;

    @ApiModelProperty("门店月房租")
    private String monthRent;

    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());
        result.setMonthRent(request.getMonthRent());
        return result;
    }

    public static PointInfoDO convertPointDO(MiniAddPointRequest request) {
        PointInfoDO result = new PointInfoDO();
        result.setOpportunityPointName(request.getOpportunityPointName());
        result.setOpportunityPointCode(request.getOpportunityPointCode());
        result.setProvinceCode(request.getProvinceCode());
        result.setCityCode(request.getCityCode());
        result.setDistrictCode(request.getDistrictCode());
        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;
    }

}