package com.banesco.common.application.helper;

import com.banesco.common.domain.dto.bian.device.Device;
import com.banesco.common.domain.dto.bian.device.DeviceTypeValues;
import com.banesco.common.domain.dto.bian.device.StringHelper;

import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DeviceHelper {


    private static final Logger logger = Logger.getLogger(DeviceHelper.class.getName());


    public static DeviceTypeValues getDeviceTypeValue(
            Device device
    ) {
        var deviceTypeValue = DeviceTypeValues.Unknown;

        if (!Objects.isNull(device) && !StringHelper.isEmpty(device.getDeviceType())) {
            try {
                deviceTypeValue = DeviceTypeValues.valueOf(device.getDeviceType());
            } catch (Exception e) {
                logger.log(Level.WARNING, "Error Mapping Device Type: %s -> %s".formatted(device.getDeviceType(), e.getMessage()));
            }
        }

        return deviceTypeValue;
    }

    public static boolean isValidDeviceType(String type) {

        for (DeviceTypeValues deviceType : DeviceTypeValues.values()) {
            if (deviceType.name().equals(type)) {
                return true;
            }
        }
        return false;
    }

    public static String getDeviceTypes() {
        return Stream.of(DeviceTypeValues.values()).map(Enum::name).collect(
                Collectors.joining(", ")
        );
    }

}
