package com.dayou.brokerage.interceptor;

import com.dayou.brokerage.annotation.DoBrokerage;
import com.dayou.dto.ItemDTO;
import com.dayou.entity.ItemEvaluate;
import com.dayou.entity.PaymentCollection;
import com.dayou.enums.OperationTypeEnum;
import com.dayou.service.IItemBrokerageGeneralService;
import com.dayou.vo.ItemStageVO;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import static com.dayou.brokerage.constants.BrokerageRule.LAND_MARKETER_RULE;
import static com.dayou.brokerage.constants.BrokerageRule.LAND_OTHER_RULE;
import static com.dayou.enums.OperationTypeEnum.*;

/**
 * 类说明：提成切面
 *
 * @author: wucl
 * @since: 2023/3/7
 * created with IntelliJ IDEA.
 */
@Slf4j
@Component
public class BrokerageInterceptor implements MethodInterceptor {

    @Autowired
    private IItemBrokerageGeneralService iItemBrokerageGeneralService;


    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Method method = methodInvocation.getMethod();
        DoBrokerage doBrokerageAnnotation = method.getAnnotation(DoBrokerage.class);

        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE &&doBrokerageAnnotation.operation() == SETTLE){
            ItemEvaluate evaluate = (ItemEvaluate) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.beforeToSettleParticipatorBrokerage(evaluate.getItemId());
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == SETTLE){
            Object argument = methodInvocation.getArguments()[0];
            if (argument instanceof Long){
                Long paymentId = (Long) argument;
                iItemBrokerageGeneralService.beforeToSettleMarketerBrokerage(paymentId);
            }else if (argument instanceof PaymentCollection){
                PaymentCollection payment = (PaymentCollection) argument;
                iItemBrokerageGeneralService.beforeToSettleMarketerBrokerage(payment);
            }

        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == EDIT){
            ItemDTO itemDTO = (ItemDTO) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.beforeEditMarketerBrokerage(itemDTO);
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == ADD){
            ItemStageVO itemStageVO = (ItemStageVO) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.beforeAddParticipatorBrokerage(itemStageVO );
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == EDIT){
            ItemStageVO itemStageVO = (ItemStageVO) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.beforeAddParticipatorBrokerage(itemStageVO);
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == DELETE){
            Long itemStageId = (Long) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.beforeRemoveParticipatorBrokerage(itemStageId);
        }
        Object result = methodInvocation.proceed();
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == ADD){
            if (result instanceof Long){
                iItemBrokerageGeneralService.addMarketerBrokerage((Long) result);
                log.info("created general and marketer detail,itemId[{}]", result);
            };
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == EDIT){
            ItemDTO itemDTO = (ItemDTO) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.editMarketerBrokerage(itemDTO);
            log.info("updated general and marketer detail,itemId[{}]", itemDTO.getId());
        }

        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == CHANGE){
            ItemDTO itemDTO = (ItemDTO) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.changeManagerBrokerage(itemDTO);
            log.info("updated general and marketer detail,itemId[{}]", itemDTO.getId());
        }

        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == DELETE){
            Long itemId = (Long) methodInvocation.getArguments()[0];
            iItemBrokerageGeneralService.removeMarketerBrokerage(itemId);
            log.info("deleted general and marketer and non-marketer detail,itemId[{}]", itemId);
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == ADD){
            if (result instanceof Long){
                ItemStageVO itemStageVO = (ItemStageVO) methodInvocation.getArguments()[0];
                iItemBrokerageGeneralService.addParticipatorBrokerage(itemStageVO,(Long) result);
                log.info("created participator detail,itemId[{}]", itemStageVO.getItemId());
            }
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == EDIT){
            if (result instanceof Boolean && (Boolean) result){
                ItemStageVO itemStageVO = (ItemStageVO) methodInvocation.getArguments()[0];
                iItemBrokerageGeneralService.editParticipatorBrokerage(itemStageVO);
                log.info("created participator detail,itemId[{}]", itemStageVO.getItemId());
            }
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE && doBrokerageAnnotation.operation() == DELETE){
            if (result instanceof Boolean && (Boolean) result){
                Long itemStageId = (Long) methodInvocation.getArguments()[0];
                iItemBrokerageGeneralService.removeParticipatorBrokerage(itemStageId);
                log.info("deleted non-marketer detail,itemId[{}]", itemStageId);
            }
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_OTHER_RULE &&doBrokerageAnnotation.operation() == SETTLE){
            if (result instanceof Boolean && (Boolean) result){
                ItemEvaluate evaluate = (ItemEvaluate) methodInvocation.getArguments()[0];
                iItemBrokerageGeneralService.toSettleParticipatorBrokerage(evaluate.getItemId());
                log.info("settle non-marketer detail,itemId[{}]", evaluate.getItemId());
            }
        }
        if (doBrokerageAnnotation!=null && doBrokerageAnnotation.rule() == LAND_MARKETER_RULE && doBrokerageAnnotation.operation() == SETTLE){
            if (result instanceof Boolean && (Boolean) result){
                Object argument = methodInvocation.getArguments()[0];
                if (argument instanceof PaymentCollection){
                    PaymentCollection payment = (PaymentCollection) argument;
                    iItemBrokerageGeneralService.toSettleMarketerBrokerage(payment);
                    log.info("try settle marketer detail,itemId[{}]", payment.getItemId());
                }
                if (argument instanceof Long){
                    Long paymentId = (Long) argument;
                    iItemBrokerageGeneralService.toSettleMarketerBrokerage(paymentId);
                    log.info("try settle marketer detail,itemId[{}]", paymentId);
                }
            }
        }
        return result;
    }
}
