/**
 * Copyright (c) 2010-2019 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.valloxmv.internal;

import java.math.BigDecimal;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.Temperature;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@link ValloxMVHandler} is responsible for handling commands, which are
 * sent to one of the channels.
 *
 * @author Björn Brings - Initial contribution
 */
@NonNullByDefault
public class ValloxMVHandler extends BaseThingHandler {

    private final Logger logger = LoggerFactory.getLogger(ValloxMVHandler.class);
    private @Nullable ScheduledFuture<?> updateTasks;
    private @Nullable ValloxMVWebSocket valloxSendSocket;
    private @Nullable WebSocketClient webSocketClient;

    /**
     * Refresh interval in seconds.
     */
    private int updateInterval;
    private long lastUpdate;

    /**
     * IP of vallox ventilation unit web interface.
     */
    public ValloxMVHandler(Thing thing, @Nullable WebSocketClient webSocketClient) {
        super(thing);
        this.webSocketClient = webSocketClient;
    }

    @SuppressWarnings({ "null", "unchecked" })
    @Override
    public void handleCommand(ChannelUID channelUID, Command command) {
        if (valloxSendSocket == null) {
            return;
        }
        if (command instanceof RefreshType) {
            if (lastUpdate > System.currentTimeMillis() + updateInterval * 1000) {
                valloxSendSocket.request(null, null);
            }
        } else {
            if (ValloxMVBindingConstants.CHANNEL_STATE.equals(channelUID.getId())) {
                try {
                    int cmd = Integer.parseInt(command.toString());
                    if ((cmd == ValloxMVBindingConstants.STATE_FIREPLACE)
                            || (cmd == ValloxMVBindingConstants.STATE_ATHOME)
                            || (cmd == ValloxMVBindingConstants.STATE_AWAY)
                            || (cmd == ValloxMVBindingConstants.STATE_BOOST)) {
                        logger.debug("Changing state to: {}", command);
                        // Open WebSocket
                        valloxSendSocket.request(channelUID, command.toString());
                        valloxSendSocket.request(null, null);
                    }
                } catch (NumberFormatException nfe) {
                    // Other commands like refresh
                    return;
                }
            } else if (ValloxMVBindingConstants.CHANNEL_ONOFF.equals(channelUID.getId())) {
                if (OnOffType.ON.equals(command)) {
                    valloxSendSocket.request(channelUID, "0");
                    valloxSendSocket.request(null, null);
                } else if (OnOffType.OFF.equals(command)) {
                    valloxSendSocket.request(channelUID, "5");
                    valloxSendSocket.request(null, null);
                }
            } else if (ValloxMVBindingConstants.WRITABLE_CHANNELS_DIMENSIONLESS.contains(channelUID.getId())) {
                if (command instanceof QuantityType) {
                    QuantityType<Dimensionless> quantity = (QuantityType<Dimensionless>) command;
                    valloxSendSocket.request(channelUID, Integer.toString(quantity.intValue()));
                    valloxSendSocket.request(null, null);
                }
            } else if (ValloxMVBindingConstants.WRITABLE_CHANNELS_TEMPERATURE.contains(channelUID.getId())) {
                if (command instanceof QuantityType) {
                    // Convert temperature to millidegree Kelvin
                    QuantityType<Temperature> quantity = ((QuantityType<Temperature>) command)
                            .toUnit(SmartHomeUnits.KELVIN);
                    if (quantity == null) {
                        return;
                    }
                    int milliKelvin = quantity.multiply(new BigDecimal(100)).intValue();
                    valloxSendSocket.request(channelUID, Integer.toString(milliKelvin));
                    valloxSendSocket.request(null, null);
                }
            }
        }
    }

    @Override
    public void initialize() {
        logger.debug("Start initializing!");
        updateStatus(ThingStatus.UNKNOWN);

        String ip = getConfigAs(ValloxMVConfig.class).getIp();
        valloxSendSocket = new ValloxMVWebSocket(webSocketClient, ValloxMVHandler.this, ip);

        updateInterval = getConfigAs(ValloxMVConfig.class).getUpdateinterval();
        if (updateInterval < 15) {
            updateInterval = 60;
        }

        scheduleUpdates();
    }

    private void scheduleUpdates() {
        logger.debug("Schedule vallox update every {} sec", updateInterval);

        String ip = getConfigAs(ValloxMVConfig.class).getIp();
        logger.debug("Connecting to ip: {}", ip);
        // Open WebSocket
        ValloxMVWebSocket valloxSocket = new ValloxMVWebSocket(webSocketClient, ValloxMVHandler.this, ip);

        updateTasks = scheduler.scheduleWithFixedDelay(() -> {
            // Do a pure read request to websocket interface
            valloxSocket.request(null, null);
        }, 0, updateInterval, TimeUnit.SECONDS);
    }

    public void dataUpdated() {
        lastUpdate = System.currentTimeMillis();
    }

    @Override
    public void dispose() {
        if (updateTasks != null) {
            updateTasks.cancel(true);
        }
        updateTasks = null;
    }

    @Override
    protected void updateState(String strChannelName, State dt) {
        super.updateState(strChannelName, dt);
    }

    @Override
    protected void updateStatus(ThingStatus ts, ThingStatusDetail statusDetail) {
        super.updateStatus(ts, statusDetail);
    }

    @Override
    protected void updateStatus(ThingStatus ts) {
        super.updateStatus(ts);
    }
}
