package cn.superdesk.uniorder.dto.vo.setting;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionCommon;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;


/**
 * @Description: TODO 可进一步优化，先这样用着吧
 * @Author: linjinyu
 * @CreateDate: 2022/7/22 12:28 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/7/22 12:28 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Data
@ToString
public class UnifiedServiceCacheStruct implements Serializable {

    private List<Level1OrderTypeStruct> orderTypeStructList;

    /**
     * 订单类型
     *
     * @param serviceNo
     * @return
     */
    public String lookUpServiceNo(String serviceNo) {

        if (StrUtil.isBlank(serviceNo)) {
            return "输入异常";
        }

        List<Level1OrderTypeStruct> orderTypeLookUpByServiceNoResult = lookUpOrderType(serviceNo);

        if (CollUtil.isEmpty(orderTypeLookUpByServiceNoResult)) {
            return "配置异常";
        }

        if (orderTypeLookUpByServiceNoResult.size() > 1) {
            return "配置重复";
        }

        log.info("订单类型_cache_{} => {}", serviceNo, orderTypeLookUpByServiceNoResult.get(0).getServiceName());

        return orderTypeLookUpByServiceNoResult.get(0).getServiceName();
    }

    /**
     * 订单状态
     *
     * @param serviceNo
     * @param thirdpartOrderState
     * @return
     */
    public String lookUpOrderState(String serviceNo, Integer thirdpartOrderState) {

        if (StrUtil.isBlank(serviceNo)) {
            return "输入异常";
        }

        List<Level1OrderTypeStruct> orderTypeLookUpByServiceNoResult = lookUpOrderType(serviceNo);

        if (CollUtil.isEmpty(orderTypeLookUpByServiceNoResult)) {
            return "配置异常";
        }

        if (orderTypeLookUpByServiceNoResult.size() > 1) {
            return "配置重复";
        }

        List<SelectOptionCommon> selectOptionLookUpByValueResult = lookUpOption(
                orderTypeLookUpByServiceNoResult.get(0).getOrderStateList(),
                thirdpartOrderState
        );

        if (CollUtil.isEmpty(selectOptionLookUpByValueResult)) {
            return "配置异常";
        }

        if (selectOptionLookUpByValueResult.size() > 1) {
            return "配置重复";
        }

        log.info("订单状态_cache_{}_{} => {}", serviceNo, thirdpartOrderState, selectOptionLookUpByValueResult.get(0).getLabel());

        return selectOptionLookUpByValueResult.get(0).getLabel();
    }

    /**
     * 支付方式
     *
     * @param serviceNo
     * @param thirdpartPayType
     * @return
     */
    public String lookUpPayType(String serviceNo, Integer thirdpartPayType) {

        if (StrUtil.isBlank(serviceNo)) {
            return "输入异常";
        }

        List<Level1OrderTypeStruct> orderTypeLookUpByServiceNoResult = lookUpOrderType(serviceNo);

        if (CollUtil.isEmpty(orderTypeLookUpByServiceNoResult)) {
            return "配置异常";
        }

        if (orderTypeLookUpByServiceNoResult.size() > 1) {
            return "配置重复";
        }

        List<SelectOptionCommon> selectOptionLookUpByValueResult = lookUpOption(orderTypeLookUpByServiceNoResult.get(0).getPayTypeList(), thirdpartPayType);

        if (CollUtil.isEmpty(selectOptionLookUpByValueResult)) {
            return "配置异常";
        }

        if (selectOptionLookUpByValueResult.size() > 1) {
            return "配置重复";
        }

        log.info("支付方式_cache_{}_{} => {}", serviceNo, thirdpartPayType, selectOptionLookUpByValueResult.get(0).getLabel());

        return selectOptionLookUpByValueResult.get(0).getLabel();


    }

    public List<Level1OrderTypeStruct> lookUpOrderType(String serviceNo) {

        List<Level1OrderTypeStruct> orderTypeLookUpByServiceNoResult = new ArrayList<>();

        List<Predicate<Level1OrderTypeStruct>> predicatesByLookUpServiceNo = new ArrayList<>();
        predicatesByLookUpServiceNo.add(r -> null != r.getServiceNo()
                && r.getServiceNo().equals(serviceNo));

        orderTypeLookUpByServiceNoResult = AnLamdaUtils.filters(orderTypeStructList, predicatesByLookUpServiceNo);

        return orderTypeLookUpByServiceNoResult;

    }

    public List<SelectOptionCommon> lookUpOption(List<SelectOptionCommon> selectOptionLookUp, Integer value) {

        List<SelectOptionCommon> selectOptionLookUpByValueResult = new ArrayList<>();

        List<Predicate<SelectOptionCommon>> predicatesByLookUpValue = new ArrayList<>();
        predicatesByLookUpValue.add(r -> null != r.getValue()
                && r.getValue().equals(value));

        selectOptionLookUpByValueResult = AnLamdaUtils.filters(selectOptionLookUp, predicatesByLookUpValue);

        return selectOptionLookUpByValueResult;
    }
}
