package com.ycwl.basic.biz;

import com.ycwl.basic.model.mobile.order.IsBuyBatchRespVO;
import com.ycwl.basic.model.pc.coupon.entity.CouponEntity;
import com.ycwl.basic.model.pc.couponRecord.resp.CouponRecordQueryResp;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.model.pc.order.entity.OrderEntity;
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
import com.ycwl.basic.model.pc.price.resp.GoodsListRespVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import com.ycwl.basic.repository.FaceRepository;
import com.ycwl.basic.repository.PriceRepository;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.repository.TemplateRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class PriceBiz {
    @Autowired
    private TemplateRepository templateRepository;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private PriceRepository priceRepository;
    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private FaceRepository faceRepository;
    @Autowired
    private CouponBiz couponBiz;

    public List<GoodsListRespVO> listGoodsByScenic(Long scenicId) {
        List<GoodsListRespVO> goodsList = new ArrayList<>();
        // 景区视频
        List<TemplateRespVO> templateList = templateRepository.getTemplateListByScenicId(scenicId);
        templateList.stream().map(template -> {
            GoodsListRespVO goods = new GoodsListRespVO();
            goods.setGoodsId(template.getId());
            goods.setGoodsName(template.getName());
            return goods;
        }).forEach(goodsList::add);
        ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId);
        if (scenicConfig != null) {
            if (!Integer.valueOf(1).equals(scenicConfig.getDisableSourceVideo())) {
                goodsList.add(new GoodsListRespVO(1L, "录像集"));
            }
            if (!Integer.valueOf(1).equals(scenicConfig.getDisableSourceImage())) {
                goodsList.add(new GoodsListRespVO(2L, "照片集"));
            }
        }
        return goodsList;
    }

    public List<GoodsListRespVO> queryGoodsList(Integer configId) {
        PriceConfigEntity priceConfig = priceRepository.getPriceConfig(configId);
        if (priceConfig == null) {
            return Collections.emptyList();
        }
        List<GoodsListRespVO> goodsList = listGoodsByScenic(priceConfig.getScenicId());
        if (Integer.valueOf(-1).equals(priceConfig.getType())) {
            return goodsList;
        }
        String[] goodsIds = priceConfig.getGoodsIds().split(",");
        return goodsList.stream().filter(goods -> {
            for (String goodsId : goodsIds) {
                if (StringUtils.equals(goods.getGoodsId().toString(), goodsId)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
    }

    public IsBuyBatchRespVO isBuy(Long userId, Long faceId, Long scenicId, Integer type, String goodsIds) {
        IsBuyBatchRespVO respVO = new IsBuyBatchRespVO();
        PriceConfigEntity priceConfig = priceRepository.getPriceConfigByScenicTypeGoods(scenicId, type, goodsIds);
        if (priceConfig == null) {
            return null;
        }
        FaceEntity face = faceRepository.getFace(faceId);
        if (face != null && !face.getMemberId().equals(userId)) {
            return null;
        }
        ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenicId);
        if (scenicConfig != null) {
            if (Integer.valueOf(1).equals(scenicConfig.getAllFree())) {
                // 景区全免
                respVO.setFree(true);
                respVO.setSlashPrice(BigDecimal.ZERO);
                return respVO;
            }
        }
        switch (type) {
            case 0: // 单个定价
                CouponRecordQueryResp recordQueryResp = couponBiz.queryUserCouponRecord(scenicId, userId, faceId, goodsIds);
                if (recordQueryResp.isUsable()) {
                    respVO.setCouponId(recordQueryResp.getCouponId());
                    respVO.setCouponRecordId(recordQueryResp.getId());
                    CouponEntity coupon = recordQueryResp.getCoupon();
                    if (coupon != null) {
                        respVO.setCouponPrice(coupon.calculateDiscountPrice(priceConfig.getPrice()));
                    }
                }
                break;
            case -1:
                CouponRecordQueryResp oneCouponRecordQueryResp = couponBiz.queryUserCouponRecord(scenicId, userId, faceId, "-1");
                if (oneCouponRecordQueryResp.isUsable()) {
                    respVO.setCouponId(oneCouponRecordQueryResp.getCouponId());
                    respVO.setCouponRecordId(oneCouponRecordQueryResp.getId());
                    CouponEntity coupon = oneCouponRecordQueryResp.getCoupon();
                    if (coupon != null) {
                        respVO.setCouponPrice(coupon.calculateDiscountPrice(priceConfig.getPrice()));
                    }
                }
                break;
        }
        respVO.setConfigId(priceConfig.getId());
        respVO.setGoodsIds(goodsIds);
        respVO.setType(type);
        respVO.setOrigPrice(priceConfig.getPrice());
        respVO.setSlashPrice(priceConfig.getSlashPrice());

        // 查询用户是否有此类订单
        respVO.setBuy(false);
        if (userId != null) {
            OrderEntity orderEntity = orderBiz.hasTypeOrder(userId, scenicId, type, priceConfig.getId());
            if (orderEntity != null) {
                respVO.setOrderId(orderEntity.getId());
                respVO.setBuy(Integer.valueOf(1).equals(orderEntity.getStatus()));
            }
        }
        return respVO;
    }
}
