/*
 * 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.kafka.streams.kstream.internals.suppress;

import org.apache.kafka.streams.kstream.Suppressed;
import org.apache.kafka.streams.kstream.internals.suppress.TimeDefinitions.TimeDefinition;

import java.time.Duration;
import java.util.Objects;

public class SuppressedInternal<K> implements Suppressed<K> {
    private static final Duration DEFAULT_SUPPRESSION_TIME = Duration.ofMillis(Long.MAX_VALUE);
    private static final StrictBufferConfigImpl DEFAULT_BUFFER_CONFIG = (StrictBufferConfigImpl) BufferConfig.unbounded();

    private final String name;
    private final BufferConfigInternal bufferConfig;
    private final Duration timeToWaitForMoreEvents;
    private final TimeDefinition<K> timeDefinition;
    private final boolean suppressTombstones;

    public SuppressedInternal(final String name,
                              final Duration suppressionTime,
                              final BufferConfig bufferConfig,
                              final TimeDefinition<K> timeDefinition,
                              final boolean suppressTombstones) {
        this.name = name;
        this.timeToWaitForMoreEvents = suppressionTime == null ? DEFAULT_SUPPRESSION_TIME : suppressionTime;
        this.timeDefinition = timeDefinition == null ? TimeDefinitions.RecordTimeDefintion.instance() : timeDefinition;
        this.bufferConfig = bufferConfig == null ? DEFAULT_BUFFER_CONFIG : (BufferConfigInternal) bufferConfig;
        this.suppressTombstones = suppressTombstones;
    }

    @Override
    public Suppressed<K> withName(final String name) {
        return new SuppressedInternal<>(name, timeToWaitForMoreEvents, bufferConfig, timeDefinition, suppressTombstones);
    }

    public String name() {
        return name;
    }

    BufferConfigInternal bufferConfig() {
        return bufferConfig;
    }

    TimeDefinition<K> timeDefinition() {
        return timeDefinition;
    }

    Duration timeToWaitForMoreEvents() {
        return timeToWaitForMoreEvents == null ? Duration.ZERO : timeToWaitForMoreEvents;
    }

    boolean shouldSuppressTombstones() {
        return suppressTombstones;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final SuppressedInternal<?> that = (SuppressedInternal<?>) o;
        return suppressTombstones == that.suppressTombstones &&
            Objects.equals(name, that.name) &&
            Objects.equals(bufferConfig, that.bufferConfig) &&
            Objects.equals(timeToWaitForMoreEvents, that.timeToWaitForMoreEvents) &&
            Objects.equals(timeDefinition, that.timeDefinition);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, bufferConfig, timeToWaitForMoreEvents, timeDefinition, suppressTombstones);
    }

    @Override
    public String toString() {
        return "SuppressedInternal{name='" + name + '\'' +
            ", bufferConfig=" + bufferConfig +
            ", timeToWaitForMoreEvents=" + timeToWaitForMoreEvents +
            ", timeDefinition=" + timeDefinition +
            ", suppressTombstones=" + suppressTombstones +
            '}';
    }
}
