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

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

    private @Nullable DeviceHandler handler;

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

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

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

    
    @Override
    @RuleAction(label = "Get Switching State")
    public @ActionOutput(name = "state", type="short")
           Map<String, Object> brickletRemoteSwitchGetSwitchingState(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short value = dev.getSwitchingState();
        
        result.put("state", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Switch Socket A")
    public void brickletRemoteSwitchSwitchSocketA(
            @ActionInput(name = "houseCode") short houseCode,
            @ActionInput(name = "receiverCode") short receiverCode,
            @ActionInput(name = "switchTo") short switchTo) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.switchSocketA(houseCode, receiverCode, switchTo);
        
    }

    public static void brickletRemoteSwitchSwitchSocketA(@Nullable ThingActions actions, short houseCode, short receiverCode, short switchTo) throws TinkerforgeException {
        invokeMethodOf(actions).brickletRemoteSwitchSwitchSocketA(houseCode, receiverCode, switchTo);
    }

    @Override
    @RuleAction(label = "Switch Socket B")
    public void brickletRemoteSwitchSwitchSocketB(
            @ActionInput(name = "address") long address,
            @ActionInput(name = "unit") short unit,
            @ActionInput(name = "switchTo") short switchTo) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.switchSocketB(address, unit, switchTo);
        
    }

    public static void brickletRemoteSwitchSwitchSocketB(@Nullable ThingActions actions, long address, short unit, short switchTo) throws TinkerforgeException {
        invokeMethodOf(actions).brickletRemoteSwitchSwitchSocketB(address, unit, switchTo);
    }

    @Override
    @RuleAction(label = "Dim Socket B")
    public void brickletRemoteSwitchDimSocketB(
            @ActionInput(name = "address") long address,
            @ActionInput(name = "unit") short unit,
            @ActionInput(name = "dimValue") short dimValue) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.dimSocketB(address, unit, dimValue);
        
    }

    public static void brickletRemoteSwitchDimSocketB(@Nullable ThingActions actions, long address, short unit, short dimValue) throws TinkerforgeException {
        invokeMethodOf(actions).brickletRemoteSwitchDimSocketB(address, unit, dimValue);
    }

    @Override
    @RuleAction(label = "Switch Socket C")
    public void brickletRemoteSwitchSwitchSocketC(
            @ActionInput(name = "systemCode") char systemCode,
            @ActionInput(name = "deviceCode") short deviceCode,
            @ActionInput(name = "switchTo") short switchTo) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.switchSocketC(systemCode, deviceCode, switchTo);
        
    }

    public static void brickletRemoteSwitchSwitchSocketC(@Nullable ThingActions actions, char systemCode, short deviceCode, short switchTo) throws TinkerforgeException {
        invokeMethodOf(actions).brickletRemoteSwitchSwitchSocketC(systemCode, deviceCode, switchTo);
    }

    @Override
    @RuleAction(label = "Set Repeats")
    public void brickletRemoteSwitchSetRepeats(
            @ActionInput(name = "repeats") short repeats) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletRemoteSwitchWrapper dev = ((@Nullable BrickletRemoteSwitchWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setRepeats(repeats);
        
    }

    public static void brickletRemoteSwitchSetRepeats(@Nullable ThingActions actions, short repeats) throws TinkerforgeException {
        invokeMethodOf(actions).brickletRemoteSwitchSetRepeats(repeats);
    }


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

        short value = dev.getRepeats();
        
        result.put("repeats", value);
        return result;
    }

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


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

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