/*-
 * ~~~~~~licensing~~~~~~
 * profile-management
 * ==========
 * Copyright (C) 2020 - 2023 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~~~~~~
 */
package ru.entaxy.esb.platform.core.management.profile.impl;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.esb.platform.base.management.core.utils.ManagedObjectsListener;
import ru.entaxy.esb.platform.core.management.profile.ProfileMBean;
import ru.entaxy.esb.platform.core.management.profile.impl.helper.Helpers;
import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.base.support.osgi.bundle.BundleCapabilityHelper;
import ru.entaxy.platform.base.support.osgi.bundle.CapabilityDescriptor;

public class ManagedProfilesListener extends ManagedObjectsListener<List<ManagedProfile>
		, ManagedProfile, ProfileMBean, ProfileMBeanImpl> {

	private static final Logger log = LoggerFactory.getLogger(ManagedProfilesListener.class);
	
	protected Helpers helpers;
	
	public ManagedProfilesListener(BundleContext bundleContext, Helpers helpers) {
		super(bundleContext, ProfileMBean.class);
		this.helpers = helpers;
	}

	@Override
	protected Iterator<ManagedProfile> getIterator(List<ManagedProfile> managedObject) {
		return managedObject.iterator();
	}

	@Override
	protected String getKeyValue(ManagedProfile object) {
		return object.getName();
	}

	@Override
	protected ProfileMBeanImpl createService(ManagedProfile managedObject) {
		try {
			return new ProfileMBeanImpl(bundleContext, managedObject, helpers);
		} catch (Exception e) {
			log.error("Error creating service", e);
			return null;
		}	}

	@Override
	protected String getJmxObjectName(ManagedProfile managedObject) {
		return ProfileMBean.Helper.getQualifier(managedObject.getName()).getValue();
	}

	// TODO add method to refresh objects to parent class
	@Override
	public void modified(List<ManagedProfile> managedObject) {
		super.modified(managedObject);
		for (ManagedProfile mp: managedObject) {
			updateManagedProfile(mp);
		}
	}
	
	protected void updateManagedProfile(ManagedProfile managedObject) {
		BundleCapabilityHelper helper = new BundleCapabilityHelper(bundleContext.getBundle(managedObject.getBundleId()));
		List<CapabilityDescriptor> capabilities = helper.getProvidedCapabilities(ProfileMBean.CAPABILITY_NAMESPACE);
		if (capabilities.isEmpty())
			return;

		CapabilityDescriptor capability = capabilities.get(0);
		
		Object val = capability.getAttributes().get("name");
		String name = val==null?"":val.toString();
		if (!CommonUtils.isValid(name))
			return;

		val = capability.getAttributes().get("desc");
		String desc;
		try {
			desc = val==null?"":new String(val.toString().getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			desc = val==null?"":val.toString();
		}
		
		managedObject.name(name).description(desc);
	}
	
}
