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

/**
 * Passes through actions of the CAN Bricklet.
 * @author Erik Fleckstein - Initial contribution
 */
@ThingActionsScope(name = "tinkerforge")
@NonNullByDefault
public class BrickletCANActions implements ThingActions, IBrickletCANActions {

    private @Nullable DeviceHandler handler;

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

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

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

    
    @Override
    @RuleAction(label = "Write Frame")
    public @ActionOutput(name = "success", type="boolean")
           Map<String, Object> brickletCANWriteFrame(
            @ActionInput(name = "frameType") short frameType,
            @ActionInput(name = "identifier") long identifier,
            @ActionInput(name = "data") short[] data,
            @ActionInput(name = "length") short length) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.writeFrame(frameType, identifier, data, length);
        
        result.put("success", value);
        return result;
    }

    public static Map<String, Object> brickletCANWriteFrame(@Nullable ThingActions actions, short frameType, long identifier, short[] data, short length) throws TinkerforgeException {
        return invokeMethodOf(actions).brickletCANWriteFrame(frameType, identifier, data, length);
    }


    @Override
    @RuleAction(label = "Read Frame")
    public @ActionOutput(name = "success", type="boolean")
           @ActionOutput(name = "frameType", type="short")
           @ActionOutput(name = "identifier", type="long")
           @ActionOutput(name = "data", type="short[]")
           @ActionOutput(name = "length", type="short")
           Map<String, Object> brickletCANReadFrame(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletCAN.ReadFrame value = dev.readFrame();
        
        result.put("success", value.success);
        result.put("frameType", value.frameType);
        result.put("identifier", value.identifier);
        result.put("data", value.data);
        result.put("length", value.length);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Configuration")
    public @ActionOutput(name = "baudRate", type="short")
           @ActionOutput(name = "transceiverMode", type="short")
           @ActionOutput(name = "writeTimeout", type="int")
           Map<String, Object> brickletCANGetConfiguration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletCAN.Configuration value = dev.getConfiguration();
        
        result.put("baudRate", value.baudRate);
        result.put("transceiverMode", value.transceiverMode);
        result.put("writeTimeout", value.writeTimeout);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Read Filter")
    public void brickletCANSetReadFilter(
            @ActionInput(name = "mode") short mode,
            @ActionInput(name = "mask") long mask,
            @ActionInput(name = "filter1") long filter1,
            @ActionInput(name = "filter2") long filter2) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setReadFilter(mode, mask, filter1, filter2);
        
    }

    public static void brickletCANSetReadFilter(@Nullable ThingActions actions, short mode, long mask, long filter1, long filter2) throws TinkerforgeException {
        invokeMethodOf(actions).brickletCANSetReadFilter(mode, mask, filter1, filter2);
    }


    @Override
    @RuleAction(label = "Get Read Filter")
    public @ActionOutput(name = "mode", type="short")
           @ActionOutput(name = "mask", type="long")
           @ActionOutput(name = "filter1", type="long")
           @ActionOutput(name = "filter2", type="long")
           Map<String, Object> brickletCANGetReadFilter(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletCAN.ReadFilter value = dev.getReadFilter();
        
        result.put("mode", value.mode);
        result.put("mask", value.mask);
        result.put("filter1", value.filter1);
        result.put("filter2", value.filter2);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Error Log")
    public @ActionOutput(name = "writeErrorLevel", type="short")
           @ActionOutput(name = "readErrorLevel", type="short")
           @ActionOutput(name = "transceiverDisabled", type="boolean")
           @ActionOutput(name = "writeTimeoutCount", type="long")
           @ActionOutput(name = "readRegisterOverflowCount", type="long")
           @ActionOutput(name = "readBufferOverflowCount", type="long")
           Map<String, Object> brickletCANGetErrorLog(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletCANWrapper dev = ((@Nullable BrickletCANWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletCAN.ErrorLog value = dev.getErrorLog();
        
        result.put("writeErrorLevel", value.writeErrorLevel);
        result.put("readErrorLevel", value.readErrorLevel);
        result.put("transceiverDisabled", value.transceiverDisabled);
        result.put("writeTimeoutCount", value.writeTimeoutCount);
        result.put("readRegisterOverflowCount", value.readRegisterOverflowCount);
        result.put("readBufferOverflowCount", value.readBufferOverflowCount);
        return result;
    }

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


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

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