package com.base.plan.task.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.api.model.BizTaskStepEnum;

import com.base.api.client.FzTaskRpcService;
import com.base.api.model.request.TaskStateNotice;
import com.base.plan.model.dto.request.StripDTOListAddDTO;
import com.base.task.handler.BizTaskStepStateNotice;
import com.base.task.model.BaseTaskStateNotice;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

@Component
public class PlanTaskStepStateNotice implements BizTaskStepStateNotice {

    @Resource
    FzTaskRpcService fzTaskRpcService;

    @Override
    public BaseTaskStateNotice getBaseTaskStateNotice(Object o, int state) {
        StripDTOListAddDTO stripDTOListAddDTO = (StripDTOListAddDTO)o;
        BaseTaskStateNotice baseTaskStateNotice = new BaseTaskStateNotice();
        baseTaskStateNotice.setBizTaskId(stripDTOListAddDTO.getReqIds().get(0));
        baseTaskStateNotice.setStep(BizTaskStepEnum.TASK_ACCESS_COMPUTE.toString());
        baseTaskStateNotice.setState(state);
        return baseTaskStateNotice;
    }

    @Override
    public void notice(BaseTaskStateNotice notice) {
        TaskStateNotice taskStateNotice = new TaskStateNotice();
        taskStateNotice.setBizTaskId(notice.getBizTaskId());
        Optional<BizTaskStepEnum> optional =  Arrays.stream(BizTaskStepEnum.values()).filter(e->e.getDeclaringClass().toString().equals(notice.getStep())).findFirst();
        if (optional.isPresent()){
            taskStateNotice.setStep(optional.get());
            taskStateNotice.setState(notice.getState());
            fzTaskRpcService.taskStepStateUpdate(taskStateNotice);
        }
    }
}
