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

/**
 * Passes through actions of the HAT Brick.
 * @author Erik Fleckstein - Initial contribution
 */
@ThingActionsScope(name = "tinkerforge")
@NonNullByDefault
public class BrickHATActions implements ThingActions, IBrickHATActions {

    private @Nullable DeviceHandler handler;

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

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

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

        @Override
    @RuleAction(label = "Set Sleep Mode")
    public void brickHATSetSleepMode(
            @ActionInput(name = "powerOffDelay") long powerOffDelay,
            @ActionInput(name = "powerOffDuration") long powerOffDuration,
            @ActionInput(name = "raspberryPiOff") boolean raspberryPiOff,
            @ActionInput(name = "brickletsOff") boolean brickletsOff,
            @ActionInput(name = "enableSleepIndicator") boolean enableSleepIndicator) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setSleepMode(powerOffDelay, powerOffDuration, raspberryPiOff, brickletsOff, enableSleepIndicator);
        
    }

    public static void brickHATSetSleepMode(@Nullable ThingActions actions, long powerOffDelay, long powerOffDuration, boolean raspberryPiOff, boolean brickletsOff, boolean enableSleepIndicator) throws TinkerforgeException {
        invokeMethodOf(actions).brickHATSetSleepMode(powerOffDelay, powerOffDuration, raspberryPiOff, brickletsOff, enableSleepIndicator);
    }


    @Override
    @RuleAction(label = "Get Sleep Mode")
    public @ActionOutput(name = "powerOffDelay", type="long")
           @ActionOutput(name = "powerOffDuration", type="long")
           @ActionOutput(name = "raspberryPiOff", type="boolean")
           @ActionOutput(name = "brickletsOff", type="boolean")
           @ActionOutput(name = "enableSleepIndicator", type="boolean")
           Map<String, Object> brickHATGetSleepMode(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickHAT.SleepMode value = dev.getSleepMode();
        
        result.put("powerOffDelay", value.powerOffDelay);
        result.put("powerOffDuration", value.powerOffDuration);
        result.put("raspberryPiOff", value.raspberryPiOff);
        result.put("brickletsOff", value.brickletsOff);
        result.put("enableSleepIndicator", value.enableSleepIndicator);
        return result;
    }

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

    @Override
    @RuleAction(label = "Set Bricklet Power")
    public void brickHATSetBrickletPower(
            @ActionInput(name = "brickletPower") boolean brickletPower) throws TinkerforgeException {
        DeviceHandler h = handler;
        if(h == null) {
            return;
        }
        Thing t = h.getThing();
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return;
        }
        dev.setBrickletPower(brickletPower);
        if(t.getChannel(new ChannelUID(t.getUID(), "BrickHATPowerBricklets")) != null){
	h.handleCommand(new ChannelUID(t.getUID(), "BrickHATPowerBricklets"), RefreshType.REFRESH);
}
    }

    public static void brickHATSetBrickletPower(@Nullable ThingActions actions, boolean brickletPower) throws TinkerforgeException {
        invokeMethodOf(actions).brickHATSetBrickletPower(brickletPower);
    }


    @Override
    @RuleAction(label = "Get Bricklet Power")
    public @ActionOutput(name = "brickletPower", type="boolean")
           Map<String, Object> brickHATGetBrickletPower(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        boolean value = dev.getBrickletPower();
        
        result.put("brickletPower", value);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get Voltages")
    public @ActionOutput(name = "voltageUSB", type="int")
           @ActionOutput(name = "voltageDC", type="int")
           Map<String, Object> brickHATGetVoltages(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        BrickHAT.Voltages value = dev.getVoltages();
        
        result.put("voltageUSB", value.voltageUSB);
        result.put("voltageDC", value.voltageDC);
        return result;
    }

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


    @Override
    @RuleAction(label = "Get RTC Driver")
    public @ActionOutput(name = "rtcDriver", type="int")
           Map<String, Object> brickHATGetRTCDriver(
            ) throws TinkerforgeException {
        Map<String, Object> result = new HashMap<>();
        DeviceHandler h = handler;
        if(h == null) {
            return result;
        }
        
        @Nullable BrickHATWrapper dev = ((@Nullable BrickHATWrapper)h.getDevice());
        if(dev == null) {
            return result;
        }

        int value = dev.getRTCDriver();
        
        result.put("rtcDriver", value);
        return result;
    }

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


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

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

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


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

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

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


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

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

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


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

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

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

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