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

/**
 * Passes through actions of the LCD 20x4 Bricklet.
 * @author Erik Fleckstein - Initial contribution
 */
@ThingActionsScope(name = "tinkerforge")
@NonNullByDefault
public class BrickletLCD20x4Actions implements ThingActions, IBrickletLCD20x4Actions {

    private @Nullable DeviceHandler handler;

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

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

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

        @Override
    @RuleAction(label = "Write Line")
    public void brickletLCD20x4WriteLine(
            @ActionInput(name = "line") short line,
            @ActionInput(name = "position") short position,
            @ActionInput(name = "text") String text) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.writeLine(line, position, text);
        
    }

    public static void brickletLCD20x4WriteLine(@Nullable ThingActions actions, short line, short position, String text) throws TinkerforgeException {
        invokeMethodOf(actions).brickletLCD20x4WriteLine(line, position, text);
    }

    @Override
    @RuleAction(label = "Clear Display")
    public void brickletLCD20x4ClearDisplay(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.clearDisplay();
        
    }

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

    @Override
    @RuleAction(label = "Backlight On")
    public void brickletLCD20x4BacklightOn(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.backlightOn();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickletLCD20x4Backlight")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickletLCD20x4Backlight"), RefreshType.REFRESH);
}
    }

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

    @Override
    @RuleAction(label = "Backlight Off")
    public void brickletLCD20x4BacklightOff(
            ) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.backlightOff();
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickletLCD20x4Backlight")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickletLCD20x4Backlight"), RefreshType.REFRESH);
}
    }

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


    @Override
    @RuleAction(label = "Is Backlight On")
    public @ActionOutput(name = "backlight", type="boolean")
           Map<String, Object> brickletLCD20x4IsBacklightOn(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.isBacklightOn();
        
        result.put("backlight", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Config")
    public @ActionOutput(name = "cursor", type="boolean")
           @ActionOutput(name = "blinking", type="boolean")
           Map<String, Object> brickletLCD20x4GetConfig(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickletLCD20x4.Config value = dev.getConfig();
        
        result.put("cursor", value.cursor);
        result.put("blinking", value.blinking);
        return result;
    }

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


    @Override
    @RuleAction(label = "Is Button Pressed")
    public @ActionOutput(name = "pressed", type="boolean")
           Map<String, Object> brickletLCD20x4IsButtonPressed(
            @ActionInput(name = "button") short button) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.isButtonPressed(button);
        
        result.put("pressed", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Custom Character")
    public void brickletLCD20x4SetCustomCharacter(
            @ActionInput(name = "index") short index,
            @ActionInput(name = "character") short[] character) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setCustomCharacter(index, character);
        
    }

    public static void brickletLCD20x4SetCustomCharacter(@Nullable ThingActions actions, short index, short[] character) throws TinkerforgeException {
        invokeMethodOf(actions).brickletLCD20x4SetCustomCharacter(index, character);
    }


    @Override
    @RuleAction(label = "Get Custom Character")
    public @ActionOutput(name = "character", type="short[]")
           Map<String, Object> brickletLCD20x4GetCustomCharacter(
            @ActionInput(name = "index") short index) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        short[] value = dev.getCustomCharacter(index);
        
        result.put("character", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Default Text")
    public void brickletLCD20x4SetDefaultText(
            @ActionInput(name = "line") short line,
            @ActionInput(name = "text") String text) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setDefaultText(line, text);
        
    }

    public static void brickletLCD20x4SetDefaultText(@Nullable ThingActions actions, short line, String text) throws TinkerforgeException {
        invokeMethodOf(actions).brickletLCD20x4SetDefaultText(line, text);
    }


    @Override
    @RuleAction(label = "Get Default Text")
    public @ActionOutput(name = "text", type="String")
           Map<String, Object> brickletLCD20x4GetDefaultText(
            @ActionInput(name = "line") short line) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        String value = dev.getDefaultText(line);
        
        result.put("text", value);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Default Text Counter")
    public void brickletLCD20x4SetDefaultTextCounter(
            @ActionInput(name = "counter") int counter) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickletLCD20x4Wrapper dev = ((@Nullable BrickletLCD20x4Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setDefaultTextCounter(counter);
        
    }

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


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

        int value = dev.getDefaultTextCounter();
        
        result.put("counter", value);
        return result;
    }

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


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

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