/**
 * Copyright (c) 2010-2020 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
 */

 /* ***********************************************************
 * This file was automatically generated on 2022-01-03.      *
 *                                                           *
 * openHAB Bindings Version 2.0.0                            *
 *                                                           *
 * If you have a bugfix for this file and want to commit it, *
 * please fix the bug in the generator. You can find a link  *
 * to the generators git repository on tinkerforge.com       *
 *************************************************************/

package org.openhab.binding.tinkerforge.internal.device;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.tinkerforge.internal.handler.DeviceHandler;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.binding.ThingActions;
import org.eclipse.smarthome.core.thing.binding.ThingActionsScope;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.ActionOutput;
import org.openhab.core.automation.annotation.RuleAction;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import java.util.Map;
import java.util.HashMap;

import com.tinkerforge.TinkerforgeException;
import com.tinkerforge.BrickDC;

/**
 * Passes through actions of the DC Brick.
 * @author Erik Fleckstein - Initial contribution
 */
@ThingActionsScope(name = "tinkerforge")
@NonNullByDefault
public class BrickDCActions implements ThingActions, IBrickDCActions {

    private @Nullable DeviceHandler handler;

    @Override
    public void setThingHandler(@Nullable ThingHandler handler) { this.handler = (DeviceHandler) handler; }

    @Override
    public @Nullable ThingHandler getThingHandler() { return handler; }

    private static IBrickDCActions invokeMethodOf(@Nullable ThingActions actions) {
        if (actions == null) {
            throw new IllegalArgumentException("actions cannot be null");
        }
        if (actions.getClass().getName().equals(BrickDCActions.class.getName())) {
            if (actions instanceof IBrickDCActions) {
                return (IBrickDCActions) actions;
            } else {
                return (IBrickDCActions) Proxy.newProxyInstance(IBrickDCActions.class.getClassLoader(),
                        new Class[] { IBrickDCActions.class }, (Object proxy, Method method, Object[] args) -> {
                            Method m = actions.getClass().getDeclaredMethod(method.getName(),
                                    method.getParameterTypes());
                            return m.invoke(actions, args);
                        });
            }
        }
        throw new IllegalArgumentException("Actions is not an instance of BrickDCActions");
    }

        @Override
    @RuleAction(label = "Set Velocity")
    public void brickDCSetVelocity(
            @ActionInput(name = "velocity") short velocity) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setVelocity(velocity);
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCVelocity")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCVelocity"), RefreshType.REFRESH);
}
    }

    public static void brickDCSetVelocity(@Nullable ThingActions actions, short velocity) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCSetVelocity(velocity);
    }


    @Override
    @RuleAction(label = "Get Velocity")
    public @ActionOutput(name = "velocity", type="short")
           Map<String, Object> brickDCGetVelocity(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.getVelocity();
        
        result.put("velocity", value);
        return result;
    }

    public static Map<String, Object> brickDCGetVelocity(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetVelocity();
    }


    @Override
    @RuleAction(label = "Get Current Velocity")
    public @ActionOutput(name = "velocity", type="short")
           Map<String, Object> brickDCGetCurrentVelocity(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.getCurrentVelocity();
        
        result.put("velocity", value);
        return result;
    }

    public static Map<String, Object> brickDCGetCurrentVelocity(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetCurrentVelocity();
    }

    @Override
    @RuleAction(label = "Set Acceleration")
    public void brickDCSetAcceleration(
            @ActionInput(name = "acceleration") int acceleration) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setAcceleration(acceleration);
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCAcceleration")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCAcceleration"), RefreshType.REFRESH);
}
    }

    public static void brickDCSetAcceleration(@Nullable ThingActions actions, int acceleration) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCSetAcceleration(acceleration);
    }


    @Override
    @RuleAction(label = "Get Acceleration")
    public @ActionOutput(name = "acceleration", type="int")
           Map<String, Object> brickDCGetAcceleration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getAcceleration();
        
        result.put("acceleration", value);
        return result;
    }

    public static Map<String, Object> brickDCGetAcceleration(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetAcceleration();
    }

    @Override
    @RuleAction(label = "Full Brake")
    public void brickDCFullBrake(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.fullBrake();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCVelocity")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCVelocity"), RefreshType.REFRESH);
}
    }

    public static void brickDCFullBrake(@Nullable ThingActions actions) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCFullBrake();
    }

    @Override
    @RuleAction(label = "Enable")
    public void brickDCEnable(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.enable();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCEnabled")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCEnabled"), RefreshType.REFRESH);
}
    }

    public static void brickDCEnable(@Nullable ThingActions actions) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCEnable();
    }

    @Override
    @RuleAction(label = "Disable")
    public void brickDCDisable(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.disable();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCEnabled")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCEnabled"), RefreshType.REFRESH);
}
    }

    public static void brickDCDisable(@Nullable ThingActions actions) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCDisable();
    }


    @Override
    @RuleAction(label = "Is Enabled")
    public @ActionOutput(name = "enabled", type="boolean")
           Map<String, Object> brickDCIsEnabled(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.isEnabled();
        
        result.put("enabled", value);
        return result;
    }

    public static Map<String, Object> brickDCIsEnabled(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCIsEnabled();
    }

    @Override
    @RuleAction(label = "Set PWM Frequency")
    public void brickDCSetPWMFrequency(
            @ActionInput(name = "frequency") int frequency) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setPWMFrequency(frequency);
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCPWMFrequency")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCPWMFrequency"), RefreshType.REFRESH);
}
    }

    public static void brickDCSetPWMFrequency(@Nullable ThingActions actions, int frequency) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCSetPWMFrequency(frequency);
    }


    @Override
    @RuleAction(label = "Get PWM Frequency")
    public @ActionOutput(name = "frequency", type="int")
           Map<String, Object> brickDCGetPWMFrequency(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getPWMFrequency();
        
        result.put("frequency", value);
        return result;
    }

    public static Map<String, Object> brickDCGetPWMFrequency(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetPWMFrequency();
    }


    @Override
    @RuleAction(label = "Get Stack Input Voltage")
    public @ActionOutput(name = "voltage", type="int")
           Map<String, Object> brickDCGetStackInputVoltage(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getStackInputVoltage();
        
        result.put("voltage", value);
        return result;
    }

    public static Map<String, Object> brickDCGetStackInputVoltage(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetStackInputVoltage();
    }


    @Override
    @RuleAction(label = "Get External Input Voltage")
    public @ActionOutput(name = "voltage", type="int")
           Map<String, Object> brickDCGetExternalInputVoltage(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getExternalInputVoltage();
        
        result.put("voltage", value);
        return result;
    }

    public static Map<String, Object> brickDCGetExternalInputVoltage(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetExternalInputVoltage();
    }


    @Override
    @RuleAction(label = "Get Current Consumption")
    public @ActionOutput(name = "voltage", type="int")
           Map<String, Object> brickDCGetCurrentConsumption(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getCurrentConsumption();
        
        result.put("voltage", value);
        return result;
    }

    public static Map<String, Object> brickDCGetCurrentConsumption(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetCurrentConsumption();
    }

    @Override
    @RuleAction(label = "Set Drive Mode")
    public void brickDCSetDriveMode(
            @ActionInput(name = "mode") short mode) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setDriveMode(mode);
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickDCFreewheeling")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickDCFreewheeling"), RefreshType.REFRESH);
}
    }

    public static void brickDCSetDriveMode(@Nullable ThingActions actions, short mode) throws TinkerforgeException {
        invokeMethodOf(actions).brickDCSetDriveMode(mode);
    }


    @Override
    @RuleAction(label = "Get Drive Mode")
    public @ActionOutput(name = "mode", type="short")
           Map<String, Object> brickDCGetDriveMode(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.getDriveMode();
        
        result.put("mode", value);
        return result;
    }

    public static Map<String, Object> brickDCGetDriveMode(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetDriveMode();
    }


    @Override
    @RuleAction(label = "Get Minimum Voltage")
    public @ActionOutput(name = "voltage", type="int")
           Map<String, Object> brickDCGetMinimumVoltage(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getMinimumVoltage();
        
        result.put("voltage", value);
        return result;
    }

    public static Map<String, Object> brickDCGetMinimumVoltage(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetMinimumVoltage();
    }


    @Override
    @RuleAction(label = "Get Identity")
    public @ActionOutput(name = "uid", type="String")
           @ActionOutput(name = "connectedUid", type="String")
           @ActionOutput(name = "position", type="char")
           @ActionOutput(name = "hardwareVersion", type="short[]")
           @ActionOutput(name = "firmwareVersion", type="short[]")
           @ActionOutput(name = "deviceIdentifier", type="int")
           Map<String, Object> brickDCGetIdentity(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickDC.Identity value = dev.getIdentity();
        
        result.put("uid", value.uid);
        result.put("connectedUid", value.connectedUid);
        result.put("position", value.position);
        result.put("hardwareVersion", value.hardwareVersion);
        result.put("firmwareVersion", value.firmwareVersion);
        result.put("deviceIdentifier", value.deviceIdentifier);
        return result;
    }

    public static Map<String, Object> brickDCGetIdentity(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetIdentity();
    }


    @Override
    @RuleAction(label = "Is Status LED Enabled")
    public @ActionOutput(name = "enabled", type="boolean")
           Map<String, Object> brickDCIsStatusLEDEnabled(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.isStatusLEDEnabled();
        
        result.put("enabled", value);
        return result;
    }

    public static Map<String, Object> brickDCIsStatusLEDEnabled(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCIsStatusLEDEnabled();
    }


    @Override
    @RuleAction(label = "Get Chip Temperature")
    public @ActionOutput(name = "temperature", type="short")
           Map<String, Object> brickDCGetChipTemperature(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.getChipTemperature();
        
        result.put("temperature", value);
        return result;
    }

    public static Map<String, Object> brickDCGetChipTemperature(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetChipTemperature();
    }


    @Override
    @RuleAction(label = "Get Send Timeout Count")
    public @ActionOutput(name = "timeoutCount", type="long")
           Map<String, Object> brickDCGetSendTimeoutCount(
            @ActionInput(name = "communicationMethod") short communicationMethod) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        long value = dev.getSendTimeoutCount(communicationMethod);
        
        result.put("timeoutCount", value);
        return result;
    }

    public static Map<String, Object> brickDCGetSendTimeoutCount(@Nullable ThingActions actions, short communicationMethod) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetSendTimeoutCount(communicationMethod);
    }


    @Override
    @RuleAction(label = "Get SPITFP Baudrate Config")
    public @ActionOutput(name = "enableDynamicBaudrate", type="boolean")
           @ActionOutput(name = "minimumDynamicBaudrate", type="long")
           Map<String, Object> brickDCGetSPITFPBaudrateConfig(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickDC.SPITFPBaudrateConfig value = dev.getSPITFPBaudrateConfig();
        
        result.put("enableDynamicBaudrate", value.enableDynamicBaudrate);
        result.put("minimumDynamicBaudrate", value.minimumDynamicBaudrate);
        return result;
    }

    public static Map<String, Object> brickDCGetSPITFPBaudrateConfig(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetSPITFPBaudrateConfig();
    }


    @Override
    @RuleAction(label = "Get SPITFP Error Count")
    public @ActionOutput(name = "errorCountACKChecksum", type="long")
           @ActionOutput(name = "errorCountMessageChecksum", type="long")
           @ActionOutput(name = "errorCountFrame", type="long")
           @ActionOutput(name = "errorCountOverflow", type="long")
           Map<String, Object> brickDCGetSPITFPErrorCount(
            @ActionInput(name = "brickletPort") char brickletPort) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickDC.SPITFPErrorCount value = dev.getSPITFPErrorCount(brickletPort);
        
        result.put("errorCountACKChecksum", value.errorCountACKChecksum);
        result.put("errorCountMessageChecksum", value.errorCountMessageChecksum);
        result.put("errorCountFrame", value.errorCountFrame);
        result.put("errorCountOverflow", value.errorCountOverflow);
        return result;
    }

    public static Map<String, Object> brickDCGetSPITFPErrorCount(@Nullable ThingActions actions, char brickletPort) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetSPITFPErrorCount(brickletPort);
    }


    @Override
    @RuleAction(label = "Get SPITFP Baudrate")
    public @ActionOutput(name = "baudrate", type="long")
           Map<String, Object> brickDCGetSPITFPBaudrate(
            @ActionInput(name = "brickletPort") char brickletPort) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickDCWrapper dev = ((@Nullable BrickDCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        long value = dev.getSPITFPBaudrate(brickletPort);
        
        result.put("baudrate", value);
        return result;
    }

    public static Map<String, Object> brickDCGetSPITFPBaudrate(@Nullable ThingActions actions, char brickletPort) throws TinkerforgeException {
        return invokeMethodOf(actions).brickDCGetSPITFPBaudrate(brickletPort);
    }
}
