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

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

    private @Nullable DeviceHandler handler;

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

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

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

    
    @Override
    @RuleAction(label = "Get Counter")
    public @ActionOutput(name = "counter", type="long")
           Map<String, Object> brickletIndustrialCounterGetCounter(
            @ActionInput(name = "channel") int channel) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        long value = dev.getCounter(channel);
        
        result.put("counter", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get All Counter")
    public @ActionOutput(name = "counter", type="long[]")
           Map<String, Object> brickletIndustrialCounterGetAllCounter(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        long[] value = dev.getAllCounter();
        
        result.put("counter", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Counter")
    public void brickletIndustrialCounterSetCounter(
            @ActionInput(name = "channel") int channel,
            @ActionInput(name = "counter") long counter) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setCounter(channel, counter);
        
    }

    public static void brickletIndustrialCounterSetCounter(@Nullable ThingActions actions, int channel, long counter) throws TinkerforgeException {
        invokeMethodOf(actions).brickletIndustrialCounterSetCounter(channel, counter);
    }

    @Override
    @RuleAction(label = "Set All Counter")
    public void brickletIndustrialCounterSetAllCounter(
            @ActionInput(name = "counter") long[] counter) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setAllCounter(counter);
        
    }

    public static void brickletIndustrialCounterSetAllCounter(@Nullable ThingActions actions, long[] counter) throws TinkerforgeException {
        invokeMethodOf(actions).brickletIndustrialCounterSetAllCounter(counter);
    }


    @Override
    @RuleAction(label = "Get Signal Data")
    public @ActionOutput(name = "dutyCycle", type="int")
           @ActionOutput(name = "period", type="long")
           @ActionOutput(name = "frequency", type="long")
           @ActionOutput(name = "value", type="boolean")
           Map<String, Object> brickletIndustrialCounterGetSignalData(
            @ActionInput(name = "channel") int channel) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletIndustrialCounter.SignalData value = dev.getSignalData(channel);
        
        result.put("dutyCycle", value.dutyCycle);
        result.put("period", value.period);
        result.put("frequency", value.frequency);
        result.put("value", value.value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get All Signal Data")
    public @ActionOutput(name = "dutyCycle", type="int[]")
           @ActionOutput(name = "period", type="long[]")
           @ActionOutput(name = "frequency", type="long[]")
           @ActionOutput(name = "value", type="boolean[]")
           Map<String, Object> brickletIndustrialCounterGetAllSignalData(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletIndustrialCounter.AllSignalData value = dev.getAllSignalData();
        
        result.put("dutyCycle", value.dutyCycle);
        result.put("period", value.period);
        result.put("frequency", value.frequency);
        result.put("value", value.value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Counter Active")
    public @ActionOutput(name = "active", type="boolean")
           Map<String, Object> brickletIndustrialCounterGetCounterActive(
            @ActionInput(name = "channel") int channel) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.getCounterActive(channel);
        
        result.put("active", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get All Counter Active")
    public @ActionOutput(name = "active", type="boolean[]")
           Map<String, Object> brickletIndustrialCounterGetAllCounterActive(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean[] value = dev.getAllCounterActive();
        
        result.put("active", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Counter Configuration")
    public @ActionOutput(name = "countEdge", type="int")
           @ActionOutput(name = "countDirection", type="int")
           @ActionOutput(name = "dutyCyclePrescaler", type="int")
           @ActionOutput(name = "frequencyIntegrationTime", type="int")
           Map<String, Object> brickletIndustrialCounterGetCounterConfiguration(
            @ActionInput(name = "channel") int channel) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletIndustrialCounterWrapper dev = ((@Nullable BrickletIndustrialCounterWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletIndustrialCounter.CounterConfiguration value = dev.getCounterConfiguration(channel);
        
        result.put("countEdge", value.countEdge);
        result.put("countDirection", value.countDirection);
        result.put("dutyCyclePrescaler", value.dutyCyclePrescaler);
        result.put("frequencyIntegrationTime", value.frequencyIntegrationTime);
        return result;
    }

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


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

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

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


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

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

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


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

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

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


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

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

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


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

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

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

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


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

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