/*
 * 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.daikin.internal.api.airbase;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.daikin.internal.api.InfoParser;
import org.openhab.binding.daikin.internal.api.airbase.AirbaseEnums.AirbaseFanMovement;
import org.openhab.binding.daikin.internal.api.airbase.AirbaseEnums.AirbaseFanSpeed;
import org.openhab.binding.daikin.internal.api.airbase.AirbaseEnums.AirbaseMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class for holding the set of parameters used by set and get control info.
 *
 * @author Tim Waterhouse - Initial Contribution
 * @author Paul Smedley - Mods for Daikin Airbase Units
 *
 */
@NonNullByDefault
public class AirbaseControlInfo {
    public String ret = "";
    public boolean power = false;
    public AirbaseMode mode = AirbaseMode.AUTO;
    /** Degrees in Celsius. */
    public @Nullable Double temp;
    public AirbaseFanSpeed fanSpeed = AirbaseFanSpeed.LEVEL_1;
    public AirbaseFanMovement fanMovement = AirbaseFanMovement.STOPPED;
    /* Not supported by all units. Sets the target humidity for dehumidifying. */
    public @Nullable Integer targetHumidity;

    private AirbaseControlInfo() {
    }

    public static AirbaseControlInfo parse(String response) {
        Logger logger = LoggerFactory.getLogger(AirbaseControlInfo.class);
        logger.trace("Parsing string: \"{}\"", response);

        Map<String, String> responseMap = InfoParser.parse(response);

        AirbaseControlInfo info = new AirbaseControlInfo();
        info.ret = responseMap.getOrDefault("ret", "");
        info.power = "1".equals(responseMap.get("pow"));
        info.mode = Objects.requireNonNull(
                Optional.ofNullable(responseMap.get("mode")).flatMap(value -> InfoParser.parseInt(value))
                        .map(value -> AirbaseMode.fromValue(value)).orElse(AirbaseMode.AUTO));
        info.temp = Optional.ofNullable(responseMap.get("stemp")).flatMap(InfoParser::parseDouble).orElse(null);
        int fRate = Optional.ofNullable(responseMap.get("f_rate")).flatMap(value -> InfoParser.parseInt(value))
                .orElse(1);
        boolean fAuto = "1".equals(responseMap.getOrDefault("f_auto", "0"));
        boolean fAirside = "1".equals(responseMap.getOrDefault("f_airside", "0"));
        info.fanSpeed = AirbaseFanSpeed.fromValue(fRate, fAuto, fAirside);
        info.fanMovement = Objects.requireNonNull(
                Optional.ofNullable(responseMap.get("f_dir")).flatMap(value -> InfoParser.parseInt(value))
                        .map(value -> AirbaseFanMovement.fromValue(value)).orElse(AirbaseFanMovement.STOPPED));
        info.targetHumidity = Optional.ofNullable(responseMap.get("shum")).flatMap(InfoParser::parseInt).orElse(null);
        return info;
    }

    public Map<String, String> getParamString() {
        Map<String, String> params = new HashMap<>();
        params.put("pow", power ? "1" : "0");
        params.put("mode", Integer.toString(mode.getValue()));
        params.put("f_rate", Integer.toString(fanSpeed.getLevel()));
        params.put("f_auto", fanSpeed.getAuto() ? "1" : "0");
        params.put("f_airside", fanSpeed.getAirside() ? "1" : "0");
        params.put("f_dir", Integer.toString(fanMovement.getValue()));
        params.put("stemp", temp != null ? temp.toString() : "20.0");
        params.put("shum", targetHumidity != null ? targetHumidity.toString() : "");
        return params;
    }
}
