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

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

    private @Nullable DeviceHandler handler;

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

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

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

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

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

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


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

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

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

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

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

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

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


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

        int value = dev.getConvergenceSpeed();
        
        result.put("speed", value);
        return result;
    }

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


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

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


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

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


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

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


    @Override
    @RuleAction(label = "Get All Data")
    public @ActionOutput(name = "accX", type="short")
           @ActionOutput(name = "accY", type="short")
           @ActionOutput(name = "accZ", type="short")
           @ActionOutput(name = "magX", type="short")
           @ActionOutput(name = "magY", type="short")
           @ActionOutput(name = "magZ", type="short")
           @ActionOutput(name = "angX", type="short")
           @ActionOutput(name = "angY", type="short")
           @ActionOutput(name = "angZ", type="short")
           @ActionOutput(name = "temperature", type="short")
           Map<String, Object> brickIMUGetAllData(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUWrapper dev = ((@Nullable BrickIMUWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickIMU.AllData value = dev.getAllData();
        
        result.put("accX", value.accX);
        result.put("accY", value.accY);
        result.put("accZ", value.accZ);
        result.put("magX", value.magX);
        result.put("magY", value.magY);
        result.put("magZ", value.magZ);
        result.put("angX", value.angX);
        result.put("angY", value.angY);
        result.put("angZ", value.angZ);
        result.put("temperature", value.temperature);
        return result;
    }

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


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

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

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


    @Override
    @RuleAction(label = "Get Calibration")
    public @ActionOutput(name = "data", type="short[]")
           Map<String, Object> brickIMUGetCalibration(
            @ActionInput(name = "typ") short typ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUWrapper dev = ((@Nullable BrickIMUWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short[] value = dev.getCalibration(typ);
        
        result.put("data", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Is Orientation Calculation On")
    public @ActionOutput(name = "orientationCalculationOn", type="boolean")
           Map<String, Object> brickIMUIsOrientationCalculationOn(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickIMUWrapper dev = ((@Nullable BrickIMUWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.isOrientationCalculationOn();
        
        result.put("orientationCalculationOn", value);
        return result;
    }

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


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

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


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

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

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


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

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

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


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

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

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


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

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

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


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

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


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

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

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