package com.bohuikeji.frame.module.admin.entity.vo;

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.frame.module.admin.base.VoBase;
import com.bohuikeji.frame.module.admin.utils.MapUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 地址信息数据块
 * @author 10552
 */
@Slf4j
@Data
@ApiModel(value="AddressVo对象", description="位置信息")
public class AddressVo extends VoBase {
    public AddressVo(){}
    public AddressVo(String str){
        log.error("初始化测试："+str);
    }

    /**
     * 使用地址json初始化地址信息
     * @param addressJson
     */
    public AddressVo(JSONObject addressJson){
        if (addressJson==null){
            log.error("无法转换地址null");
            return;
        }
        this.province=addressJson.getString("province");
        this.city=addressJson.getString("city");
        this.area=addressJson.getString("area");
        this.address=addressJson.getString("address");
        this.detail=addressJson.getString("detail");
        JSONObject location=addressJson.getJSONObject("location");
        if (location==null){
            log.error("【地址转换失败】地址JSON不合法：{}",addressJson.toJSONString());
            throw new RuntimeException("地址转换失败");
        }
        this.lat=addressJson.getJSONObject("location").getString("lat");
        this.lng=addressJson.getJSONObject("location").getString("lng");
        this.setAdcode(addressJson.getString("adcode"));
    }
    @ApiModelProperty(value = "行政区划代码")
    private String adcode;
    @ApiModelProperty(value = "省")
    private String province="";
    @ApiModelProperty(value = "市")
    private String city="";
    @ApiModelProperty(value = "区(县)")
    private String area="";
    @ApiModelProperty(value = "地图地址")
    private String address="";
    @ApiModelProperty(value = "详细地址")
    private String detail="";
    @ApiModelProperty(value = "纬度")
    private String lat;
    @ApiModelProperty(value = "经度")
    private String lng;
    @ApiModelProperty(value = "城市Id")
    private String cityId;


    /**
     * 地址对象转JSON
     * @return
     */
    public JSONObject forJsonAddress(){
        JSONObject json=new JSONObject(6);
        json.put("province",this.province);
        json.put("city",this.city);
        json.put("area",this.area);
        json.put("address",this.address);
        json.put("detail",this.detail);
        json.put("adcode",adcode);
        JSONObject location=new JSONObject(2);
        location.put("lat",lat);
        location.put("lng",lng);
        json.put("location",location);
        return json;
    }

    /**
     * 校验并填充地址信息
     * 传入的地址，经纬度和结构化文本地址至少有一个，全部都有优先使用经纬度
     * 无法填充返回null
     * @return
     */
    public AddressVo fillAddress(){
        //地址是否完整
        boolean isEntire=true;
        if (StringUtils.isBlank(adcode)){
            isEntire=false;
        }
        if(StringUtils.isBlank(this.address)){
            isEntire=false;
        }
        if (StringUtils.isBlank(this.lat)){
            isEntire=false;
        }
        if(StringUtils.isBlank(this.lng)){
            isEntire=false;
        }
        if (isEntire){
            return this;
        }
        AddressVo tmpAds;
        if(StringUtils.isBlank(this.lat)||StringUtils.isBlank(this.lng)){
            if(StringUtils.isBlank(this.address)){
                log.error("地址填充失败，至少要有一个经纬度或者结构化地址信息。当前地址对象：[{}]",this);
                return null;
            }
            tmpAds=MapUtils.getAddressGeo(this.address);
            tmpAds.setDetail(this.detail);
            return tmpAds;
        }
        tmpAds=MapUtils.getAddressReGeo(this.lng+","+this.lat);
        if(StringUtils.isNotBlank(this.address)){
            tmpAds.setAddress(this.address);
        }
        tmpAds.setDetail(this.detail);
        return tmpAds;
    }

    public String getDetail() {
        if(this.detail==null){
            return "";
        }
        return detail;
    }

    public String getLocation(){
        return this.lng+","+this.lat;
    }

    @Override
    public String toString() {
        return this.address+this.detail;
    }

    public String getCity() {
        if(StringUtils.isBlank(city)||"[]".equals(city)){
            return this.province;
        }
        return city;
    }
}
