/*-
 * ~~~~~~licensing~~~~~~
 * management-core
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package ru.entaxy.esb.platform.base.management.core.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;

import ru.entaxy.esb.platform.base.management.core.api.Attribute;
import ru.entaxy.esb.platform.base.management.core.api.MBeanExportPolicy;
import ru.entaxy.esb.platform.base.management.core.api.NotExported;
import ru.entaxy.esb.platform.base.management.core.api.Operation;
import ru.entaxy.esb.platform.base.management.core.api.Parameter;

public class MBeanInfoHelper {
    // Constants -----------------------------------------------------

    // Attributes ----------------------------------------------------

    // Static --------------------------------------------------------

    // Constructors --------------------------------------------------

    // Public --------------------------------------------------------

    public static MBeanOperationInfo[] getMBeanOperationsInfo(final Class mbeanInterface, MBeanExportPolicy policy) {
        List<MBeanOperationInfo> operations = new ArrayList<>();

        for (Method method : mbeanInterface.getMethods()) {
            if (!MBeanInfoHelper.isGetterMethod(method) && !MBeanInfoHelper.isSetterMethod(method) &&
                    !MBeanInfoHelper.isIsBooleanMethod(method)) {
                MBeanOperationInfo info = MBeanInfoHelper.getOperationInfo(method, policy);
                if (info != null)
                    operations.add(info);
            } else {
                // check for explicitly defined operations with @Operation annotation
                if (isExplicitOperation(method, policy)) {
                    MBeanOperationInfo info = MBeanInfoHelper.getOperationInfo(method, policy);
                    if (info != null)
                        operations.add(info);
                }
            }
        }

        return operations.toArray(new MBeanOperationInfo[operations.size()]);
    }

    protected static boolean isExplicitOperation(Method method, MBeanExportPolicy policy) {
        if (!method.isAnnotationPresent(Operation.class))
            return false;
        if (method.isAnnotationPresent(NotExported.class))
            return false;
        return MBeanExportPolicy.ANNOTATED_ONLY.equals(policy) || MBeanExportPolicy.ANNOTATION_ENRICH.equals(policy);

    }

    public static MBeanAttributeInfo[] getMBeanAttributesInfo(final Class mbeanInterface, MBeanExportPolicy policy) {
        List<MBeanAttributeInfo> tempAttributes = new ArrayList<>();
        List<MBeanAttributeInfo> finalAttributes = new ArrayList<>();
        List<String> alreadyAdded = new ArrayList<>();

        for (Method method : mbeanInterface.getMethods()) {
            if (MBeanInfoHelper.isGetterMethod(method) || MBeanInfoHelper.isSetterMethod(method) ||
                    MBeanInfoHelper.isIsBooleanMethod(method)) {
                MBeanAttributeInfo info = MBeanInfoHelper.getAttributeInfo(method, policy);
                if (info != null)
                    tempAttributes.add(info);
            }
        }

        // since getters and setters will each have an MBeanAttributeInfo we need to de-duplicate
        for (MBeanAttributeInfo info1 : tempAttributes) {
            MBeanAttributeInfo infoToCopy = info1;
            for (MBeanAttributeInfo info2 : tempAttributes) {
                if (info1.getName().equals(info2.getName()) && !info1.equals(info2)) {
                    infoToCopy = new MBeanAttributeInfo(info1.getName(),
                            info1.getType().equals("void") ? info2.getType() : info1.getType(), info1.getDescription(),
                            (info1.isReadable() || info2.isReadable()), (info1.isWritable() || info2.isWritable()),
                            (info1.isIs() || info2.isIs()));
                }
            }
            if (!alreadyAdded.contains(infoToCopy.getName())) {
                finalAttributes.add(infoToCopy);
                alreadyAdded.add(infoToCopy.getName());
            }
        }

        return finalAttributes.toArray(new MBeanAttributeInfo[finalAttributes.size()]);
    }

    // Package protected ---------------------------------------------

    // Protected -----------------------------------------------------

    // Private -------------------------------------------------------

    private static boolean isGetterMethod(final Method method) {
        if (!method.getName().equals("get") && method.getName().startsWith("get") &&
                method.getParameterTypes().length == 0 &&
                !method.getReturnType().equals(void.class)) {
            return true;
        }

        return false;
    }

    private static boolean isSetterMethod(final Method method) {
        if (!method.getName().equals("set") && method.getName().startsWith("set") &&
                method.getParameterTypes().length == 1 &&
                method.getReturnType().equals(void.class)) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean isIsBooleanMethod(final Method method) {
        if (!method.getName().equals("is") && method.getName().startsWith("is") &&
                method.getParameterTypes().length == 0 &&
                method.getReturnType().equals(boolean.class)) {
            return true;
        } else {
            return false;
        }
    }

    private static MBeanOperationInfo getOperationInfo(final Method operation, MBeanExportPolicy policy) {
        MBeanOperationInfo info = null;
        Class<?> returnType = operation.getReturnType();

        MBeanParameterInfo[] paramsInfo =
                MBeanInfoHelper.getParametersInfo(operation.getParameterAnnotations(), operation.getParameterTypes());

        String description = operation.getName();
        int impact = MBeanOperationInfo.UNKNOWN;

        if (!MBeanExportPolicy.IGNORE_ANNOTATIONS.equals(policy)) {

            // ANNOTATED_ONLY, EXCEPT_HIDDEN, ANNOTATION_ENRICH
            NotExported annoNotExported = operation.getAnnotation(NotExported.class);
            if (annoNotExported != null) {
                // method is hidden from exporting
                return null;
            }

            Operation annoOperation = operation.getAnnotation(Operation.class);
            if (MBeanExportPolicy.ANNOTATED_ONLY.equals(policy) && (annoOperation == null)) {
                // method is not annotated
                return null;
            }

            if (annoOperation != null) {
                description = annoOperation.desc();
                impact = annoOperation.impact();
            }
        }
        info = new MBeanOperationInfo(operation.getName(), description, paramsInfo, returnType.getName(), impact);

        return info;
    }

    private static MBeanAttributeInfo getAttributeInfo(final Method operation, MBeanExportPolicy policy) {
        String description = "N/A";

        if (!MBeanExportPolicy.IGNORE_ANNOTATIONS.equals(policy)) {

            // ANNOTATED_ONLY, EXCEPT_HIDDEN, ANNOTATION_ENRICH
            NotExported annoNotExported = operation.getAnnotation(NotExported.class);
            if (annoNotExported != null) {
                // method is hidden from exporting
                return null;
            }

            Attribute annoAttribute = operation.getAnnotation(Attribute.class);
            if (MBeanExportPolicy.ANNOTATED_ONLY.equals(policy) && (annoAttribute == null)) {
                // method is not annotated
                return null;
            }

            if (annoAttribute != null) {
                description = operation.getAnnotation(Attribute.class).desc();
            }

        }
        MBeanAttributeInfo info =
                new MBeanAttributeInfo(getAttributeName(operation), operation.getReturnType().getName(), description,
                        (isGetterMethod(operation) || isIsBooleanMethod(operation)), isSetterMethod(operation),
                        isIsBooleanMethod(operation));

        return info;
    }

    private static String getAttributeName(Method operation) {
        String name = operation.getName();

        if (isGetterMethod(operation) || isSetterMethod(operation))
            name = operation.getName().substring(3);
        else if (isIsBooleanMethod(operation))
            name = operation.getName().substring(2);

        return name;
    }

    private static MBeanParameterInfo[] getParametersInfo(final Annotation[][] params, final Class<?>[] paramTypes) {
        MBeanParameterInfo[] paramsInfo = new MBeanParameterInfo[params.length];

        for (int i = 0; i < params.length; i++) {
            MBeanParameterInfo paramInfo = null;
            String type = paramTypes[i].getName();
            for (Annotation anno : params[i]) {
                if (Parameter.class.isInstance(anno)) {
                    String name = Parameter.class.cast(anno).name();
                    String description = Parameter.class.cast(anno).desc();
                    paramInfo = new MBeanParameterInfo(name, type, description);
                }
            }

            if (paramInfo == null) {
                paramInfo = new MBeanParameterInfo("p " + (i + 1), type, "parameter " + (i + 1));
            }

            paramsInfo[i] = paramInfo;
        }

        return paramsInfo;
    }

    // Inner classes -------------------------------------------------
}
