/*
 * 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.homematic.internal.communicator.parser;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmInterface;
import org.openhab.binding.homematic.internal.model.HmParamsetType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Parses a parameter description message and extracts datapoint metadata.
 *
 * @author Gerhard Riegler - Initial contribution
 */
@NonNullByDefault
public class GetParamsetDescriptionParser extends CommonRpcParser<Object[], @Nullable Void> {
    private final Logger logger = LoggerFactory.getLogger(GetParamsetDescriptionParser.class);
    private HmParamsetType paramsetType;
    private HmChannel channel;
    private boolean isHmIpDevice;

    public GetParamsetDescriptionParser(HmChannel channel, HmParamsetType paramsetType) {
        this.channel = channel;
        this.paramsetType = paramsetType;
        this.isHmIpDevice = channel.getDevice().getHmInterface() == HmInterface.HMIP;
    }

    @Override
    @SuppressWarnings("unchecked")
    public @Nullable Void parse(Object[] message) throws IOException {
        if (message.length == 0 || !(message[0] instanceof Map)) {
            String datatype = message.length == 0 ? "<empty>" : message[0].getClass().getName();
            logger.debug("Unexpected datatype '{}', ignoring message", datatype);
            return null;
        }
        Map<String, Map<String, Object>> dpNames = (Map<String, Map<String, Object>>) message[0];

        for (String datapointName : dpNames.keySet()) {
            Map<String, Object> dpMeta = dpNames.get(datapointName);
            if (dpMeta == null) {
                logger.debug("No metadata found for datapoint '{}', skipping", datapointName);
                continue;
            }

            HmDatapoint dp = assembleDatapoint(datapointName, toString(dpMeta.get("UNIT")),
                    toString(dpMeta.get("TYPE")), toOptionList(dpMeta.get("VALUE_LIST")), dpMeta.get("MIN"),
                    dpMeta.get("MAX"), toInteger(dpMeta.get("OPERATIONS")), dpMeta.get("DEFAULT"),
                    toSpecialValues(dpMeta.get("SPECIAL")), paramsetType, isHmIpDevice);
            channel.addDatapoint(dp);
        }

        return null;
    }

    private @Nullable Map<String, Number> toSpecialValues(@Nullable Object specialValues) {
        if (specialValues != null && specialValues instanceof Object[] array) {
            Map<String, Number> result = new HashMap<>();
            for (int i = 0; i < array.length; i++) {
                if (array[i] instanceof Map itemMap) {
                    String id = (String) itemMap.get("ID");
                    Object value = itemMap.get("VALUE");
                    if (id != null && value instanceof Number number) {
                        result.put(id, number);
                    }
                }
            }
            if (!result.isEmpty()) {
                return result;
            }
        }
        return null;
    }
}
