package com.yonge.cooleshow.admin.io.request.coupon;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponCategoryEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 优惠券订单信息
 * Created by Eric.Shang on 2022/9/6.
 */
public class CouponOrderVO {

    /**
     * 订单优惠券信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("订单优惠券查询条件")
    public static class PageRequest implements Serializable {

        @ApiModelProperty("用户ID")
        private Long userId;

        @ApiModelProperty("客户端类型")
        private ClientEnum clientType;

        @ApiModelProperty("优惠券类型")
        private List<CouponCategoryEnum> couponTypes;

        @ApiModelProperty("优惠券发放ID")
        private List<Long> issueIds;

        @ApiModelProperty("可用状态")
        private CouponUseStateEnum useState;

        @ApiModelProperty("当前时间")
        private Long timestamp;

        @ApiModelProperty("订单支付金额")
        private Double amount;

        public boolean invalidRequestParam() {

            return Objects.isNull(getUserId())
                    || CollectionUtils.isEmpty(getIssueIds())
                    || Objects.isNull(getAmount());
        }

        public String jsonString() {

            return JSON.toJSONString(this);
        }
    }

    /**
     * 订单优惠券信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponPageInfo implements Serializable {

        @ApiModelProperty("优惠券抵扣金额")
        private Double discountedPrices;

        @ApiModelProperty("优惠券信息")
        private List<CouponInfo> couponInfos;

        @ApiModelProperty("优惠券状态重置标识")
        private Boolean reset;

        @ApiModelProperty("订单编号")
        private String orderNo;
    }

    /**
     * 优惠券详情信息
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponInfo implements Serializable {

        @ApiModelProperty("优惠券ID")
        private Long id;

        @ApiModelProperty("优惠券名称")
        private String name;

        @ApiModelProperty("优惠券使用门槛")
        private Double useLimit;

        @ApiModelProperty("优惠券抵扣金额")
        private Double discountPrice;

        @ApiModelProperty("使用状态")
        private Boolean selected;

        @ApiModelProperty("优惠券发放ID")
        private Long issueId;
    }

    /**
     * 订单优惠券更新请求
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponUpdateRequest implements Serializable {

        @ApiModelProperty("用户ID")
        private Long userId;

        @ApiModelProperty(value = "优惠券发放ID")
        private List<Long> issueIds;

        @ApiModelProperty(value = "优惠券信息", hidden = true)
        private List<CouponInfo> couponInfos;

        @ApiModelProperty("优惠券状态重置标识")
        private Boolean reset;

        @ApiModelProperty("订单编号")
        private String orderNo;

        @ApiModelProperty("使用类型")
        private CouponCategoryEnum useType;

        public boolean invalidRequestParam() {

            if (Optional.ofNullable(getReset()).orElse(false)) {

                // 订单取消，优惠券重置
                if (StringUtils.isEmpty(getOrderNo())) {
                    return  true;
                }

            } else {

                // 更新优惠券使用状态
                if (CollectionUtils.isEmpty(getIssueIds())) {
                    return true;
                }
            }

            return Objects.isNull(getUserId());
        }

        public String jsonString() {
            return JSON.toJSONString(this);
        }


        public List<CouponInfo> getCouponInfos() {

            List<CouponInfo> retlist = Lists.newArrayList();

            if (CollectionUtils.isNotEmpty(getIssueIds())) {

                for (Long item : getIssueIds()) {

                    retlist.add(CouponInfo.builder().issueId(item).selected(true).build());
                }
            }

            return retlist;
        }
    }
}
