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

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

    private @Nullable DeviceHandler handler;

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

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

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

    
    @Override
    @RuleAction(label = "Write")
    public @ActionOutput(name = "written", type="short")
           Map<String, Object> brickletRS232Write(
            @ActionInput(name = "message") char[] message,
            @ActionInput(name = "length") short length) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletRS232Wrapper dev = ((@Nullable BrickletRS232Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.write(message, length);
        
        result.put("written", value);
        return result;
    }

    public static Map<String, Object> brickletRS232Write(@Nullable ThingActions actions, char[] message, short length) throws TinkerforgeException {
        return invokeMethodOf(actions).brickletRS232Write(message, length);
    }


    @Override
    @RuleAction(label = "Read")
    public @ActionOutput(name = "message", type="char[]")
           @ActionOutput(name = "length", type="short")
           Map<String, Object> brickletRS232Read(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletRS232Wrapper dev = ((@Nullable BrickletRS232Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletRS232.Read value = dev.read();
        
        result.put("message", value.message);
        result.put("length", value.length);
        return result;
    }

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


    @Override
    @RuleAction(label = "Read Frame")
    public @ActionOutput(name = "message", type="char[]")
           @ActionOutput(name = "length", type="short")
           Map<String, Object> brickletRS232ReadFrame(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletRS232Wrapper dev = ((@Nullable BrickletRS232Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletRS232.ReadFrame value = dev.readFrame();
        
        result.put("message", value.message);
        result.put("length", value.length);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Configuration")
    public @ActionOutput(name = "baudrate", type="short")
           @ActionOutput(name = "parity", type="short")
           @ActionOutput(name = "stopbits", type="short")
           @ActionOutput(name = "wordlength", type="short")
           @ActionOutput(name = "hardwareFlowcontrol", type="short")
           @ActionOutput(name = "softwareFlowcontrol", type="short")
           Map<String, Object> brickletRS232GetConfiguration(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletRS232Wrapper dev = ((@Nullable BrickletRS232Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletRS232.Configuration value = dev.getConfiguration();
        
        result.put("baudrate", value.baudrate);
        result.put("parity", value.parity);
        result.put("stopbits", value.stopbits);
        result.put("wordlength", value.wordlength);
        result.put("hardwareFlowcontrol", value.hardwareFlowcontrol);
        result.put("softwareFlowcontrol", value.softwareFlowcontrol);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Break Condition")
    public void brickletRS232SetBreakCondition(
            @ActionInput(name = "breakTime") int breakTime) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRS232Wrapper dev = ((@Nullable BrickletRS232Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setBreakCondition(breakTime);
        
    }

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


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

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