/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.dolphinscheduler.server.master.engine.workflow.execution;

import static com.google.common.base.Preconditions.checkArgument;

import org.apache.dolphinscheduler.dao.entity.WorkflowInstance;
import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowPauseLifecycleEvent;
import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowStopLifecycleEvent;
import org.apache.dolphinscheduler.server.master.engine.workflow.listener.IWorkflowLifecycleListener;
import org.apache.dolphinscheduler.server.master.engine.workflow.policy.IWorkflowFailureStrategy;
import org.apache.dolphinscheduler.server.master.engine.workflow.policy.WorkflowFailureStrategyFactory;
import org.apache.dolphinscheduler.server.master.runner.IWorkflowExecuteContext;

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

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WorkflowExecution implements IWorkflowExecution {

    @Getter
    private final IWorkflowExecuteContext workflowExecuteContext;

    @Getter
    private final List<IWorkflowLifecycleListener> workflowInstanceLifecycleListeners;

    @Getter
    private final IWorkflowFailureStrategy workflowFailureStrategy;

    public WorkflowExecution(WorkflowExecutionBuilder workflowExecutionBuilder) {
        this.workflowExecuteContext = workflowExecutionBuilder.getWorkflowExecuteContextBuilder().build();
        this.workflowInstanceLifecycleListeners =
                new ArrayList<>(workflowExecuteContext.getWorkflowInstanceLifecycleListeners());
        this.workflowFailureStrategy = WorkflowFailureStrategyFactory
                .getStrategy(workflowExecuteContext.getWorkflowInstance().getFailureStrategy());
    }

    @Override
    public void pause() {
        getWorkflowEventBus().publish(WorkflowPauseLifecycleEvent.of(this));
    }

    @Override
    public void stop() {
        getWorkflowEventBus().publish(WorkflowStopLifecycleEvent.of(this));
    }

    @Override
    public List<IWorkflowLifecycleListener> getWorkflowLifecycleListeners() {
        return workflowInstanceLifecycleListeners;
    }

    @Override
    public void registerWorkflowInstanceLifecycleListener(IWorkflowLifecycleListener listener) {
        checkArgument(listener != null, "listener cannot be null");
        workflowInstanceLifecycleListeners.add(listener);
    }

    @Override
    public String toString() {
        final WorkflowInstance workflowInstance = workflowExecuteContext.getWorkflowInstance();
        return "WorkflowExecution{" +
                "name=" + workflowInstance.getName() +
                ", state=" + workflowInstance.getState().name() +
                '}';
    }
}
