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

/**
 * Passes through actions of the LCD 16x2 Bricklet.
 * @author Erik Fleckstein - Initial contribution
 */
@ThingActionsScope(name = "tinkerforge")
@NonNullByDefault
public class BrickletLCD16x2Actions implements ThingActions, IBrickletLCD16x2Actions {

    private @Nullable DeviceHandler handler;

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

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

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

        @Override
    @RuleAction(label = "Write Line")
    public void brickletLCD16x2WriteLine(
            @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 BrickletLCD16x2Wrapper dev = ((@Nullable BrickletLCD16x2Wrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.writeLine(line, position, text);
        
    }

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

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

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

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

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

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

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


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

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

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


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

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

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


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

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

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

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

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


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

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

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


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

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