package com.heyu.api.data.service.impl;

import com.heyu.api.data.entity.vv.VvReverseOrderEntity;
import com.heyu.api.data.enums.ActionEnums;
import com.heyu.api.data.kuaidi100.ActionDTO;
import com.heyu.api.data.service.impl.reverse.*;
import com.heyu.api.data.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class AbstractReverseAction {

    public abstract ActionEnums getAction(VvReverseOrderEntity vvReverseOrderEntity, boolean isApp, boolean isAdmin);

    public static final List<AbstractReverseAction> actions = Arrays.asList(
            new SellerAgreeCancelAction(),
            new SellerAgreeRefundAction(),
            new SellerAgreeReturnRefundAction(),
            new SellerAgreeSignReceiptAction(),
            new SellerQaCheckFinishedAction(),
            new SellerRejectCancelAction(),
            new SellerRejectRefundAction(),
            new SellerRejectReturnRefundAction(),
            new SellerRejectSignReceiptAction()
    );

    public static List<ActionDTO> getOrderActionDTO(VvReverseOrderEntity vvReverseOrderEntity, boolean isApp) {
        List<ActionDTO> actionDTOS = new ArrayList<>();
        for (AbstractReverseAction action : actions) {
            ActionEnums orderActionEnums = action.getAction(vvReverseOrderEntity, isApp, !isApp);
            if (orderActionEnums != null) {
                ActionDTO orderActionDTO = new ActionDTO();
                orderActionDTO.setDesc(orderActionEnums.getDesc());
                orderActionDTO.setButtonName(orderActionEnums.getButtonName());
                orderActionDTO.setInterfaceUri(orderActionEnums.getInterfaceUri());
                orderActionDTO.setTargetStatus(orderActionEnums.getTargetStatus());
                actionDTOS.add(orderActionDTO);
            }
        }
        return actionDTOS;
    }


    public static ActionDTO getOrderAction(VvReverseOrderEntity vvReverseOrderEntity, boolean isApp, String buttonName) {
        List<ActionDTO> orderActionDTOS = getOrderActionDTO(vvReverseOrderEntity, isApp);
        if(CollectionUtils.isNotEmpty(orderActionDTOS)){
            for (ActionDTO orderActionDTO : orderActionDTOS) {
                if(orderActionDTO.getButtonName().equals(buttonName)){
                    return orderActionDTO;
                }
            }
        }
        return null;
    }


    public static boolean checkFailed(VvReverseOrderEntity vvReverseOrderEntity, boolean isApp,String buttonName) {
        ActionDTO orderActionDTO =  getOrderAction(vvReverseOrderEntity, isApp, buttonName);
        if(orderActionDTO !=null){
            return false;
        }
        return true;
    }

}
