/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.watcher.actions.jira;

import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.notification.jira.JiraService;
import org.elasticsearch.xpack.watcher.actions.ActionFactory;

import java.io.IOException;

public class JiraActionFactory extends ActionFactory {

    private final TextTemplateEngine templateEngine;
    private final JiraService jiraService;

    public JiraActionFactory(Settings settings, TextTemplateEngine templateEngine, JiraService jiraService) {
        super(Loggers.getLogger(ExecutableJiraAction.class, settings));
        this.templateEngine = templateEngine;
        this.jiraService = jiraService;
    }

    @Override
    public ExecutableJiraAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException {
        JiraAction action = JiraAction.parse(watchId, actionId, parser);
        jiraService.getAccount(action.getAccount()); // for validation -- throws exception if account not present
        return new ExecutableJiraAction(action, actionLogger, jiraService, templateEngine);
    }
}
