/*
 * 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.input;

import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.watcher.watch.Payload;

import java.io.IOException;

public abstract class ExecutableInput<I extends Input, R extends Input.Result> implements ToXContentObject {

    protected final I input;
    protected final Logger logger;

    protected ExecutableInput(I input, Logger logger) {
        this.input = input;
        this.logger = logger;
    }

    /**
     * @return the type of this input
     */
    public final String type() {
        return input.type();
    }

    public I input() {
        return input;
    }

    /**
     * Executes this input
     */
    public abstract R execute(WatchExecutionContext ctx, @Nullable Payload payload);

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        return input.toXContent(builder, params);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ExecutableInput<?, ?> that = (ExecutableInput<?, ?>) o;

        return input.equals(that.input);
    }

    @Override
    public int hashCode() {
        return input.hashCode();
    }
}
