package com.sii.rental.ui;

import static com.sii.rental.ui.RentalUIConstants.IMG_AGENCY;
import static com.sii.rental.ui.RentalUIConstants.IMG_CUSTOMER;
import static com.sii.rental.ui.RentalUIConstants.IMG_RENTAL;
import static com.sii.rental.ui.RentalUIConstants.IMG_RENTAL_OBJECT;
import static com.sii.rental.ui.RentalUIConstants.RENTAL_UI_IMG_REGISTRY;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

import com.opcoach.training.rental.Customer;
import com.opcoach.training.rental.RentalAgency;
import com.sii.rental.ecore.RentalCoreActivator;

public class RentalAddOn {

	public static final String RENTAL_AGENCIES = "com.opcoach.training.rental.RentalAgency (Collection)"; 
	
	@PostConstruct
	public void initContext(IEclipseContext context) {
		context.set(RentalAgency.class, RentalCoreActivator.getAgency());
		context.set(RENTAL_AGENCIES, Stream.of(RentalCoreActivator.getAgency(), RentalCoreActivator.getAgency()).collect(Collectors.toList()));
		context.set(RENTAL_UI_IMG_REGISTRY, getLocalImageRegistry());
	}
	
	ImageRegistry getLocalImageRegistry() {
		Bundle b = FrameworkUtil.getBundle(this.getClass());
		
		ImageRegistry registry = new ImageRegistry();
		
		registry.put(IMG_CUSTOMER, ImageDescriptor.createFromURL(b.getEntry(IMG_CUSTOMER)));
		registry.put(IMG_RENTAL, ImageDescriptor.createFromURL(b.getEntry(IMG_RENTAL)));
		registry.put(IMG_RENTAL_OBJECT, ImageDescriptor.createFromURL(b.getEntry(IMG_RENTAL_OBJECT)));
		registry.put(IMG_AGENCY, ImageDescriptor.createFromURL(b.getEntry(IMG_AGENCY)));
		
		return registry;
	}
	
	@Inject @Optional
	void reactOnRentalEvent(@UIEventTopic("rental/copy") Customer customer) {
		System.out.println(customer.getDisplayName());
	}
	
}


@@ -357,24 +357,50 @@
 EGISTRY;%0D%0A%0D%0A
+import java.util.Arrays;%0D%0A
 import java.
@@ -526,24 +526,133 @@
 .Inject;%0D%0A%0D%0A
+import org.eclipse.core.runtime.IConfigurationElement;%0D%0Aimport org.eclipse.core.runtime.IExtensionRegistry;%0D%0A
 import org.e
@@ -2299,13 +2299,598 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09@Inject%0D%0A%09public void getExtensionQuickAccess(IExtensionRegistry registry) %7B%0D%0A%09%09IConfigurationElement%5B%5D elements = registry.getConfigurationElementsFor(%22org.eclipse.e4.workbench.model%22);%0D%0A%09%09%0D%0A%09%09Arrays.stream(elements)%0D%0A%09%09%09.filter((e) -%3E e.getName().equals(%22fragment%22))%0D%0A%09%09%09.map((e) -%3E e.getNamespaceIdentifier() + %22:%22 + e.getAttribute(%22uri%22))%0D%0A%09%09%09.forEach(System.out::println);%0D%0A%09%09%0D%0A%09%09Arrays.stream(elements)%0D%0A%09%09.filter((e) -%3E e.getName().equals(%22processor%22))%0D%0A%09%09.map((e) -%3E e.getNamespaceIdentifier() + %22:%22 + e.getAttribute(%22class%22))%0D%0A%09%09.forEach(System.out::println);%0D%0A%09%09%09%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
 %7D%0D%0A%0D%0A

Print extension of models