package com.samsung.sample.approval.knox.interceptor;

import com.samsung.approval.annotation.ApprovalDocumentType;
import com.samsung.approval.entity.Approval;
import com.samsung.approval.entity.ApprovalStep;
import com.samsung.approval.enums.ApprovalDocStatus;
import com.samsung.approval.interceptor.ApprovalInterceptor;
import com.samsung.sample.approval.knox.KnoxApprovalSampleService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Log4j2
@ApprovalDocumentType(names = {"knoxSample"})
public class KnoxApprovalSampleInterceptor implements ApprovalInterceptor {

    @Autowired
    KnoxApprovalSampleService knoxApprovalSampleService;

    @Override
    public void afterSubmit(Approval approval, List<ApprovalStep> approvalStepList, Map<String, Object> attribute) {
        log.debug("execute ApprovalSampleInterceptor afterSubmit");
        //Knox 상신 후 문서 상태 업데이트
        knoxApprovalSampleService.updateSampleApprovalDocument(approval.getDbDocId(), ApprovalDocStatus.INPROCESS);
    }

    @Override
    public void afterCancel(Approval approval) {
        log.debug("상신자의 결재 상태를 상신 취소로 변경한다.");
        //Knox 상신 후 문서 상태 업데이트
        knoxApprovalSampleService.updateSampleApprovalDocument(approval.getDbDocId(), ApprovalDocStatus.CANCELLED);
    }

    /**
     * 완결 후
     *
     * @param approval
     */
    @Override
    public void afterComplete(Approval approval) {
        knoxApprovalSampleService.updateSampleApprovalDocument(approval.getDbDocId(), ApprovalDocStatus.COMPLETED);
    }

    /**
     * 반려 후
     *
     * @param approval
     */
    @Override
    public void afterReject(Approval approval) {
        knoxApprovalSampleService.updateSampleApprovalDocument(approval.getDbDocId(), ApprovalDocStatus.REJECTED);
    }
}