package shop.alien.entity.second.vo;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.Data;
import shop.alien.entity.second.SecondGoods;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Data
@JsonInclude
@ApiModel(value = "二手商品发布", description = "发布")
public class SecondGoodsVo extends SecondGoods {

    @ApiModelProperty(value = "保存方式 0:草稿 1:发布")
    private Integer saveType;

    @ApiModelProperty(value = "商品距离")
    private String distance;

//    @NotBlank(message = "商品图片不能为空")
    @ApiModelProperty(value = "商品图片")
    private List<String> imgUrl;

    @ApiModelProperty(value = "图片集合")
    private List<Map<String, Object>> imgList;

    @TableField(exist = false)
    @ApiModelProperty(value = "用户名称")
    private String userName;

    @TableField(exist = false)
    @ApiModelProperty(value = "用户真名")
    private String realName;

    @TableField(exist = false)
    @ApiModelProperty(value = "用户电话")
    private String userPhone;

    @TableField(exist = false)
    @ApiModelProperty(value = "用户头像")
    private String userImage;

    @TableField(exist = false)
    @ApiModelProperty(value = "收藏状态 默认为 0")
    private Integer collectStatus = 0;

    @TableField(exist = false)
    @ApiModelProperty(value = "点赞状态 默认为 0")
    private Integer likeStatus = 0;
    
    @TableField(exist = false)
    @ApiModelProperty(value = "商品状态名称")
    private String goodsStatusName;

    @ApiModelProperty("一级分类名称")
    @TableField(exist = false)
    private String categoryOneName;

    @ApiModelProperty("二级分类名称")
    @TableField(exist = false)
    private String categoryTwoName;

    @ApiModelProperty(value = "商品图片列表")
    private List<String> imageUrls;
    /** -------------------- 搜索入参 -------------------- */

    @TableField(exist = false)
    @ApiModelProperty(value = "当前页码")
    private Integer pageNum = 1;

    @ApiModelProperty(value = "每页数量")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "当前经度")
    private Double currentLatitude;

    @ApiModelProperty(value = "当前纬度")
    private Double currentLongitude;

    @ApiModelProperty(value = "排序字段 排序字段 价格-price，发布时间-releaseTime")
    private String orderData;

    @ApiModelProperty(value = "排序方式 排序方式 1-正序 2-倒叙")
    private Integer orderType;

    @ApiModelProperty(value = "搜索入参")
    private String searchData;

    @ApiModelProperty(value = "屏蔽id")
    private String shieldId;

    @ApiModelProperty(value = "商品id")
    private Integer goodsId;

    @ApiModelProperty(value = "屏蔽商品IDList")
    private List<Integer> shieldedGoodsIds;

    @ApiModelProperty(value = "拉黑用户IDList")
    private List<Integer> userIdList;

    @JsonProperty("timeAgo")
    public String getFormattedTimeAgo() {
        return getTimeAgo(getReleaseTime());
    }

    @JsonProperty("topicList")
    public List<String> parseTopic() {
        return parseTopic(getTopic());
    }

    /**
     * 解析话题列表
     *
     * @param topic 话题列表字符串
     * @return 解析后的话题列表
     */
    public List<String> parseTopic(String topic) {
        if (topic != null && !topic.isEmpty()) {
            return Arrays.asList(topic.split(","));
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * 返回时间描述，根据发布时间计算距离现在多久。
     * 24小时内 返回几小时前，24小时外 1-31返回几天前，31天外返回几月前，365天外返回几年前
     *
     * @param releaseTime 发布时间
     * @return 时间描述字符串
     */

    public String getTimeAgo(Date releaseTime) {
        if (releaseTime == null) {
            return "未知时间";
        }

        long now = System.currentTimeMillis();
        long diff = now - releaseTime.getTime();

        // 新增分钟级判断
        long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
        if (minutes < 1) {
            return "刚刚";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        }

        long hours = TimeUnit.MILLISECONDS.toHours(diff);
        long days = TimeUnit.MILLISECONDS.toDays(diff);
        long months = days / 31;
        long years = days / 365;

        if (hours < 24) {
            return hours + "小时前";
        } else if (days <= 31) {
            return days + "天前";
        } else if (years >= 1) {
            return years + "年前";
        } else {
            return months + "个月前";
        }
    }
}
