/*
 * Copyright (c) 2010-2026 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.binding.energyforecast;

import static org.junit.jupiter.api.Assertions.fail;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
import org.openhab.core.thing.type.ChannelGroupTypeUID;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;

/**
 * Test implementation of {@link ThingHandlerCallback} used by the energyforecast binding
 * to verify interactions in unit and integration tests. It records state updates, triggers
 * and time series sent by handlers so that tests can assert on them.
 *
 * @author Bernd Weymann - Initial contribution
 */
@NonNullByDefault
public class CallbackMock implements ThingHandlerCallback {
    private @Nullable Bridge bridge;
    private ThingStatusInfo status = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE).build();
    public Map<String, State> stateMap = new HashMap<>();
    public Map<String, String> triggerMap = new HashMap<>();
    public Map<String, TimeSeries> timeSeriesMap = new HashMap<>();

    public void clear() {
        stateMap.clear();
    }

    public @Nullable State getState(String channel) {
        return stateMap.get(channel);
    }

    @Override
    public void stateUpdated(ChannelUID channelUID, State state) {
        stateMap.put(channelUID.getAsString(), state);
    }

    @Override
    public void postCommand(ChannelUID channelUID, Command command) {
    }

    @Override
    public void sendTimeSeries(ChannelUID channelUID, TimeSeries timeSeries) {
        synchronized (timeSeriesMap) {
            timeSeriesMap.put(channelUID.getAsString(), timeSeries);
            timeSeriesMap.notifyAll();
        }
    }

    public @Nullable TimeSeries getTimeSeries(String channel) {
        synchronized (timeSeriesMap) {
            Instant start = Instant.now();
            Instant check = Instant.now();
            while (timeSeriesMap.get(channel) == null && Duration.between(start, check).getSeconds() < 10) {
                try {
                    timeSeriesMap.wait(100);
                } catch (InterruptedException e) {
                    fail("Interrupted getting timeseries " + channel);
                }
                check = Instant.now();
            }
            TimeSeries timeSeries = timeSeriesMap.get(channel);
            if (timeSeries == null) {
                fail("No timeseries available for " + channel);
            }
            return timeSeries;
        }
    }

    @Override
    public void statusUpdated(Thing thing, ThingStatusInfo thingStatus) {
        synchronized (this) {
            status = thingStatus;
            this.notifyAll();
        }
    }

    public ThingStatusInfo getStatus() {
        return status;
    }

    public void waitForOnline() {
        synchronized (this) {
            Instant start = Instant.now();
            Instant check = Instant.now();
            while (!ThingStatus.ONLINE.equals(status.getStatus()) && Duration.between(start, check).getSeconds() < 10) {
                try {
                    this.wait(1000);
                } catch (InterruptedException e) {
                    fail("Interrupted waiting for ONLINE");
                }
                check = Instant.now();
            }
        }
        // if method is exited without reaching ONLINE e.g. through timeout fail
        if (!ThingStatus.ONLINE.equals(status.getStatus())) {
            fail("waitForOnline just reached status " + status);
        }
    }

    @Override
    public void thingUpdated(Thing thing) {
    }

    @Override
    public void validateConfigurationParameters(Thing thing, Map<String, Object> configurationParameters) {
    }

    @Override
    public void validateConfigurationParameters(Channel channel, Map<String, Object> configurationParameters) {
    }

    @Override
    public @Nullable ConfigDescription getConfigDescription(ChannelTypeUID channelTypeUID) {
        return null;
    }

    @Override
    public @Nullable ConfigDescription getConfigDescription(ThingTypeUID thingTypeUID) {
        return null;
    }

    @Override
    public void configurationUpdated(Thing thing) {
    }

    @Override
    public void migrateThingType(Thing thing, ThingTypeUID thingTypeUID, Configuration configuration) {
    }

    @Override
    public void channelTriggered(Thing thing, ChannelUID channelUID, String event) {
        triggerMap.put(channelUID.getAsString(), event);
    }

    @Override
    public ChannelBuilder createChannelBuilder(ChannelUID channelUID, ChannelTypeUID channelTypeUID) {
        return ChannelBuilder.create(new ChannelUID("handler:test"));
    }

    @Override
    public ChannelBuilder editChannel(Thing thing, ChannelUID channelUID) {
        return ChannelBuilder.create(new ChannelUID("handler:test"));
    }

    @Override
    public List<ChannelBuilder> createChannelBuilders(ChannelGroupUID channelGroupUID,
            ChannelGroupTypeUID channelGroupTypeUID) {
        return List.of();
    }

    @Override
    public boolean isChannelLinked(ChannelUID channelUID) {
        return false;
    }

    @Override
    public @Nullable Bridge getBridge(ThingUID bridgeUID) {
        return bridge;
    }

    public void setBridge(Bridge bridge) {
        this.bridge = bridge;
    }
}
