/*
 * 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.heos.internal.handler;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.heos.internal.json.payload.BrowseResult;
import org.openhab.binding.heos.internal.json.payload.Media;
import org.openhab.binding.heos.internal.json.payload.YesNoEnum;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateOption;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * Dynamically create the users list of favorites and playlists.
 *
 * @author Martin van Wingerden - Initial contribution
 */
@Component(service = { DynamicStateDescriptionProvider.class, HeosDynamicStateDescriptionProvider.class })
@NonNullByDefault
public class HeosDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {

    @Activate
    public HeosDynamicStateDescriptionProvider(final @Reference EventPublisher eventPublisher,
            final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
            final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
        this.eventPublisher = eventPublisher;
        this.itemChannelLinkRegistry = itemChannelLinkRegistry;
        this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
    }

    String getValueByLabel(ChannelUID channelUID, String input) {
        List<StateOption> options = channelOptionsMap.get(channelUID);
        if (options == null) {
            return input;
        }

        // if no match was found we assume that it already was a value and not a label
        return Objects.requireNonNull(options.stream().filter(o -> input.equals(o.getLabel()))
                .map(StateOption::getValue).findFirst().orElse(input));
    }

    public void setFavorites(ChannelUID channelUID, List<BrowseResult> favorites) {
        setBrowseResultList(channelUID, favorites, d -> d.mediaId);
    }

    public void setPlaylists(ChannelUID channelUID, List<BrowseResult> playLists) {
        setBrowseResultList(channelUID, playLists, d -> d.containerId);
    }

    private void setBrowseResultList(ChannelUID channelUID, List<BrowseResult> browseResults,
            Function<BrowseResult, @Nullable String> function) {
        setStateOptions(channelUID,
                browseResults.stream().filter(browseResult -> browseResult.playable == YesNoEnum.YES)
                        .flatMap(browseResult -> getStateOption(function, browseResult).stream()).toList());
    }

    private Optional<StateOption> getStateOption(Function<BrowseResult, @Nullable String> function,
            BrowseResult browseResult) {
        return Optional.ofNullable(function.apply(browseResult))
                .map(value -> new StateOption(value, browseResult.name));
    }

    public void setQueue(ChannelUID channelUID, List<Media> queue) {
        setStateOptions(channelUID,
                queue.stream().map(m -> new StateOption(String.valueOf(m.queueId), m.combinedSongArtist())).toList());
    }
}
