package com.yonge.cooleshow.biz.dal.wrapper.coupon;

import com.alibaba.fastjson.JSON;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponCategoryEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * 优惠券订单信息
 * Created by Eric.Shang on 2022/9/6.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CouponOrderWrapper implements Serializable {

    // 优惠券叠加优惠金额
    private Double discountedPrices;

    // 订单优惠券信息
    private List<CouponInfo> couponInfos;

    // 优惠券状态重置标识
    private Boolean reset;
    // 订单编号
    private String orderNo;

    // 使用方式
    private CouponCategoryEnum useType;

    public String jsonString() {

        return JSON.toJSONString(this);
    }

    public static CouponOrderWrapper from(String recv) {

        return JSON.parseObject(recv, CouponOrderWrapper.class);
    }

    public Boolean getReset() {
        return Optional.ofNullable(reset).orElse(false);
    }

    public CouponOrderWrapper discountedPrices(Double discountedPrices) {
        this.discountedPrices = discountedPrices;
        return this;
    }

    public CouponOrderWrapper couponInfos(List<CouponInfo> couponInfos) {
        this.couponInfos = couponInfos;
        return this;
    }

    public CouponOrderWrapper reset(Boolean reset) {
        this.reset = reset;
        return this;
    }

    public CouponOrderWrapper orderNo(String orderNo) {
        this.orderNo = orderNo;
        return this;
    }

    public CouponOrderWrapper useType(CouponCategoryEnum useType) {
        this.useType = useType;
        return this;
    }


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

        // 优惠券ID
        private Long id;
        // 优惠券名称
        private String name;
        // 使用门槛
        private Double useLimit;
        // 抵扣金额
        private Double discountPrice;
        // 使用状态
        private Boolean selected;
        // 发放ID
        private Long issueId;

        public Double getUseLimit() {
            return Optional.ofNullable(useLimit).orElse(0D);
        }

        public Double getDiscountPrice() {
            return Optional.ofNullable(discountPrice).orElse(0D);
        }

        public Boolean getSelected() {
            return Optional.ofNullable(selected).orElse(false);
        }
    }

    /**
     * 订单
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponIssue implements Serializable {

        // 发放ID
        private Long id;
        // 使用状态
        private CouponUseStateEnum useState;
        // 使用时间
        private Long useTime;
        // 订单编号
        private String orderNo;
        // 使用方式
        private CouponCategoryEnum useType;

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