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

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

    private @Nullable DeviceHandler handler;

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

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

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

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

        int value = dev.getMode();
        
        result.put("mode", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Reader Request Tag ID")
    public void brickletNFCReaderRequestTagID(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerRequestTagID();
        
    }

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


    @Override
    @RuleAction(label = "Reader Get Tag ID")
    public @ActionOutput(name = "tagType", type="int")
           @ActionOutput(name = "tagID", type="int[]")
           Map<String, Object> brickletNFCReaderGetTagID(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletNFC.ReaderGetTagID value = dev.readerGetTagID();
        
        result.put("tagType", value.tagType);
        result.put("tagID", value.tagID);
        return result;
    }

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


    @Override
    @RuleAction(label = "Reader Get State")
    public @ActionOutput(name = "state", type="int")
           @ActionOutput(name = "idle", type="boolean")
           Map<String, Object> brickletNFCReaderGetState(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletNFC.ReaderGetState value = dev.readerGetState();
        
        result.put("state", value.state);
        result.put("idle", value.idle);
        return result;
    }

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

    @Override
    @RuleAction(label = "Reader Write NDEF")
    public void brickletNFCReaderWriteNDEF(
            @ActionInput(name = "ndef") int[] ndef) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerWriteNDEF(ndef);
        
    }

    public static void brickletNFCReaderWriteNDEF(@Nullable ThingActions actions, int[] ndef) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCReaderWriteNDEF(ndef);
    }

    @Override
    @RuleAction(label = "Reader Request NDEF")
    public void brickletNFCReaderRequestNDEF(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerRequestNDEF();
        
    }

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


    @Override
    @RuleAction(label = "Reader Read NDEF")
    public @ActionOutput(name = "ndef", type="int[]")
           Map<String, Object> brickletNFCReaderReadNDEF(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int[] value = dev.readerReadNDEF();
        
        result.put("ndef", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Reader Authenticate Mifare Classic Page")
    public void brickletNFCReaderAuthenticateMifareClassicPage(
            @ActionInput(name = "page") int page,
            @ActionInput(name = "keyNumber") int keyNumber,
            @ActionInput(name = "key") int[] key) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerAuthenticateMifareClassicPage(page, keyNumber, key);
        
    }

    public static void brickletNFCReaderAuthenticateMifareClassicPage(@Nullable ThingActions actions, int page, int keyNumber, int[] key) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCReaderAuthenticateMifareClassicPage(page, keyNumber, key);
    }

    @Override
    @RuleAction(label = "Reader Write Page")
    public void brickletNFCReaderWritePage(
            @ActionInput(name = "page") int page,
            @ActionInput(name = "data") int[] data) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerWritePage(page, data);
        
    }

    public static void brickletNFCReaderWritePage(@Nullable ThingActions actions, int page, int[] data) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCReaderWritePage(page, data);
    }

    @Override
    @RuleAction(label = "Reader Request Page")
    public void brickletNFCReaderRequestPage(
            @ActionInput(name = "page") int page,
            @ActionInput(name = "length") int length) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.readerRequestPage(page, length);
        
    }

    public static void brickletNFCReaderRequestPage(@Nullable ThingActions actions, int page, int length) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCReaderRequestPage(page, length);
    }


    @Override
    @RuleAction(label = "Reader Read Page")
    public @ActionOutput(name = "data", type="int[]")
           Map<String, Object> brickletNFCReaderReadPage(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int[] value = dev.readerReadPage();
        
        result.put("data", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Cardemu Get State")
    public @ActionOutput(name = "state", type="int")
           @ActionOutput(name = "idle", type="boolean")
           Map<String, Object> brickletNFCCardemuGetState(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletNFC.CardemuGetState value = dev.cardemuGetState();
        
        result.put("state", value.state);
        result.put("idle", value.idle);
        return result;
    }

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

    @Override
    @RuleAction(label = "Cardemu Start Discovery")
    public void brickletNFCCardemuStartDiscovery(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.cardemuStartDiscovery();
        
    }

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

    @Override
    @RuleAction(label = "Cardemu Write NDEF")
    public void brickletNFCCardemuWriteNDEF(
            @ActionInput(name = "ndef") int[] ndef) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.cardemuWriteNDEF(ndef);
        
    }

    public static void brickletNFCCardemuWriteNDEF(@Nullable ThingActions actions, int[] ndef) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCCardemuWriteNDEF(ndef);
    }

    @Override
    @RuleAction(label = "Cardemu Start Transfer")
    public void brickletNFCCardemuStartTransfer(
            @ActionInput(name = "transfer") int transfer) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.cardemuStartTransfer(transfer);
        
    }

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


    @Override
    @RuleAction(label = "P2P Get State")
    public @ActionOutput(name = "state", type="int")
           @ActionOutput(name = "idle", type="boolean")
           Map<String, Object> brickletNFCP2PGetState(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletNFC.P2PGetState value = dev.p2pGetState();
        
        result.put("state", value.state);
        result.put("idle", value.idle);
        return result;
    }

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

    @Override
    @RuleAction(label = "P2P Start Discovery")
    public void brickletNFCP2PStartDiscovery(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.p2pStartDiscovery();
        
    }

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

    @Override
    @RuleAction(label = "P2P Write NDEF")
    public void brickletNFCP2PWriteNDEF(
            @ActionInput(name = "ndef") int[] ndef) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.p2pWriteNDEF(ndef);
        
    }

    public static void brickletNFCP2PWriteNDEF(@Nullable ThingActions actions, int[] ndef) throws TinkerforgeException {
        invokeMethodOf(actions).brickletNFCP2PWriteNDEF(ndef);
    }

    @Override
    @RuleAction(label = "P2P Start Transfer")
    public void brickletNFCP2PStartTransfer(
            @ActionInput(name = "transfer") int transfer) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.p2pStartTransfer(transfer);
        
    }

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


    @Override
    @RuleAction(label = "P2P Read NDEF")
    public @ActionOutput(name = "ndef", type="int[]")
           Map<String, Object> brickletNFCP2PReadNDEF(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int[] value = dev.p2pReadNDEF();
        
        result.put("ndef", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Read UID")
    public @ActionOutput(name = "uid", type="long")
           Map<String, Object> brickletNFCReadUID(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        long value = dev.readUID();
        
        result.put("uid", value);
        return result;
    }

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


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

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

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


    @Override
    @RuleAction(label = "Get Status LED Config")
    public @ActionOutput(name = "config", type="int")
           Map<String, Object> brickletNFCGetStatusLEDConfig(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getStatusLEDConfig();
        
        result.put("config", value);
        return result;
    }

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


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

        BrickletNFC.SPITFPErrorCount value = dev.getSPITFPErrorCount();
        
        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> brickletNFCGetSPITFPErrorCount(@Nullable ThingActions actions) throws TinkerforgeException {
        return invokeMethodOf(actions).brickletNFCGetSPITFPErrorCount();
    }

    @Override
    @RuleAction(label = "Reset")
    public void brickletNFCReset(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletNFCWrapper dev = ((@Nullable BrickletNFCWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.reset();
        
    }

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


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

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