/**
 * 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.BrickIMUV2;

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

    private @Nullable DeviceHandler handler;

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

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

    private static IBrickIMUV2Actions invokeMethodOf(@Nullable ThingActions actions) {
        if (actions == null) {
            throw new IllegalArgumentException("actions cannot be null");
        }
        if (actions.getClass().getName().equals(BrickIMUV2Actions.class.getName())) {
            if (actions instanceof IBrickIMUV2Actions) {
                return (IBrickIMUV2Actions) actions;
            } else {
                return (IBrickIMUV2Actions) Proxy.newProxyInstance(IBrickIMUV2Actions.class.getClassLoader(),
                        new Class[] { IBrickIMUV2Actions.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 BrickIMUV2Actions");
    }

    
    @Override
    @RuleAction(label = "Get Orientation")
    public @ActionOutput(name = "heading", type="short")
           @ActionOutput(name = "roll", type="short")
           @ActionOutput(name = "pitch", type="short")
           Map<String, Object> brickIMUV2GetOrientation(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.Orientation value = dev.getOrientation();
        
        result.put("heading", value.heading);
        result.put("roll", value.roll);
        result.put("pitch", value.pitch);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Linear Acceleration")
    public @ActionOutput(name = "x", type="short")
           @ActionOutput(name = "y", type="short")
           @ActionOutput(name = "z", type="short")
           Map<String, Object> brickIMUV2GetLinearAcceleration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.LinearAcceleration value = dev.getLinearAcceleration();
        
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Gravity Vector")
    public @ActionOutput(name = "x", type="short")
           @ActionOutput(name = "y", type="short")
           @ActionOutput(name = "z", type="short")
           Map<String, Object> brickIMUV2GetGravityVector(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.GravityVector value = dev.getGravityVector();
        
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Quaternion")
    public @ActionOutput(name = "w", type="short")
           @ActionOutput(name = "x", type="short")
           @ActionOutput(name = "y", type="short")
           @ActionOutput(name = "z", type="short")
           Map<String, Object> brickIMUV2GetQuaternion(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.Quaternion value = dev.getQuaternion();
        
        result.put("w", value.w);
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get All Data")
    public @ActionOutput(name = "acceleration", type="short[]")
           @ActionOutput(name = "magneticField", type="short[]")
           @ActionOutput(name = "angularVelocity", type="short[]")
           @ActionOutput(name = "eulerAngle", type="short[]")
           @ActionOutput(name = "quaternion", type="short[]")
           @ActionOutput(name = "linearAcceleration", type="short[]")
           @ActionOutput(name = "gravityVector", type="short[]")
           @ActionOutput(name = "temperature", type="byte")
           @ActionOutput(name = "calibrationStatus", type="short")
           Map<String, Object> brickIMUV2GetAllData(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.AllData value = dev.getAllData();
        
        result.put("acceleration", value.acceleration);
        result.put("magneticField", value.magneticField);
        result.put("angularVelocity", value.angularVelocity);
        result.put("eulerAngle", value.eulerAngle);
        result.put("quaternion", value.quaternion);
        result.put("linearAcceleration", value.linearAcceleration);
        result.put("gravityVector", value.gravityVector);
        result.put("temperature", value.temperature);
        result.put("calibrationStatus", value.calibrationStatus);
        return result;
    }

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

    @Override
    @RuleAction(label = "Leds On")
    public void brickIMUV2LedsOn(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.ledsOn();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickIMUV2EnableLEDs")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickIMUV2EnableLEDs"), RefreshType.REFRESH);
}
    }

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

    @Override
    @RuleAction(label = "Leds Off")
    public void brickIMUV2LedsOff(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.ledsOff();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickIMUV2EnableLEDs")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickIMUV2EnableLEDs"), RefreshType.REFRESH);
}
    }

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


    @Override
    @RuleAction(label = "Get Acceleration")
    public @ActionOutput(name = "x", type="short")
           @ActionOutput(name = "y", type="short")
           @ActionOutput(name = "z", type="short")
           Map<String, Object> brickIMUV2GetAcceleration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.Acceleration value = dev.getAcceleration();
        
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Magnetic Field")
    public @ActionOutput(name = "x", type="short")
           @ActionOutput(name = "y", type="short")
           @ActionOutput(name = "z", type="short")
           Map<String, Object> brickIMUV2GetMagneticField(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.MagneticField value = dev.getMagneticField();
        
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


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

        BrickIMUV2.AngularVelocity value = dev.getAngularVelocity();
        
        result.put("x", value.x);
        result.put("y", value.y);
        result.put("z", value.z);
        return result;
    }

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


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

        byte value = dev.getTemperature();
        
        result.put("temperature", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Sensor Configuration")
    public @ActionOutput(name = "magnetometerRate", type="short")
           @ActionOutput(name = "gyroscopeRange", type="short")
           @ActionOutput(name = "gyroscopeBandwidth", type="short")
           @ActionOutput(name = "accelerometerRange", type="short")
           @ActionOutput(name = "accelerometerBandwidth", type="short")
           Map<String, Object> brickIMUV2GetSensorConfiguration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.SensorConfiguration value = dev.getSensorConfiguration();
        
        result.put("magnetometerRate", value.magnetometerRate);
        result.put("gyroscopeRange", value.gyroscopeRange);
        result.put("gyroscopeBandwidth", value.gyroscopeBandwidth);
        result.put("accelerometerRange", value.accelerometerRange);
        result.put("accelerometerBandwidth", value.accelerometerBandwidth);
        return result;
    }

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


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

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

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


    @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> brickIMUV2GetIdentity(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.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> brickIMUV2GetIdentity(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickIMUV2GetIdentity();
    }


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

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

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


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

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

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


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

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

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


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

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

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


    @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> brickIMUV2GetSPITFPErrorCount(
            @ActionInput(name = "brickletPort") char brickletPort) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUV2Wrapper dev = ((@Nullable BrickIMUV2Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMUV2.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> brickIMUV2GetSPITFPErrorCount(@Nullable ThingActions actions, char brickletPort) throws TinkerforgeException {
        return invokeMethodOf(actions).brickIMUV2GetSPITFPErrorCount(brickletPort);
    }


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

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

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