package com.tandbergtv.workflow.auth;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.tandbergtv.workflow.auth.jaxb.ActionType;
import com.tandbergtv.workflow.auth.jaxb.Authorization;
import com.tandbergtv.workflow.auth.jaxb.MenuType;
import com.tandbergtv.workflow.auth.jaxb.SectionType;

public class AuthorizationConfReader {
	private static AuthorizationConfReader _instance;
	
	private AuthorizationConfReader() {}
	
	public static synchronized AuthorizationConfReader getInstance() {
		if(_instance == null)
			_instance = new AuthorizationConfReader();
		return _instance;
	}
	
	public Set<Module> read(File authFile) {
		Set<Module> modules = new HashSet<Module>();
		try {
			JAXBContext jc = JAXBContext.newInstance(AuthorizationConfReader.class.getPackage().getName() + ".jaxb");
			Unmarshaller unmarshaller = jc.createUnmarshaller();
			Authorization auth = (Authorization) unmarshaller.unmarshal(authFile);
			
			List<com.tandbergtv.workflow.auth.jaxb.Authorization.Module> jaxbModulesList = auth.getModule();
			if(jaxbModulesList != null) {
				for(com.tandbergtv.workflow.auth.jaxb.Authorization.Module jaxbModule : auth.getModule()) {
					modules.add(convert(jaxbModule));
				}
			}
		} catch(JAXBException e) {
			throw new RuntimeException("Unable to get authorization configuration", e);
		}
		return modules;
	}
	
	private Module convert(com.tandbergtv.workflow.auth.jaxb.Authorization.Module jaxbModule) {
		Module m = new Module();

		m.setName(jaxbModule.getName());
		m.setExternal(Boolean.valueOf(jaxbModule.getIsExternal()));

		String displayName = !isBlank(jaxbModule.getDisplayName()) ?
				jaxbModule.getDisplayName()
				: jaxbModule.getName();
		m.setDisplayName(displayName);

		List<com.tandbergtv.workflow.auth.jaxb.Authorization.Module.Permission> jaxbPermissions = jaxbModule.getPermission();
		if(jaxbPermissions != null) {
			for(com.tandbergtv.workflow.auth.jaxb.Authorization.Module.Permission jaxbPermission : jaxbPermissions) {
				m.addPermission(convert(jaxbPermission));
			}
		}
		
		return m;
	}
	
	private Permission convert(com.tandbergtv.workflow.auth.jaxb.Authorization.Module.Permission jaxbPermission) {
		Permission p = new Permission();
		
		p.setName(jaxbPermission.getName());
		String displayName = !isBlank(jaxbPermission.getDisplayName()) ?
					jaxbPermission.getDisplayName()
					: jaxbPermission.getName();
		p.setDisplayName(displayName);
		
		List<MenuType> jaxbMenuList = jaxbPermission.getMenu();
		if(jaxbMenuList != null) {
			for(MenuType jaxbMenu : jaxbMenuList) {
				p.addMenu(jaxbMenu.getName());
			}
		}
		
		List<ActionType> jaxbActionsList = jaxbPermission.getAction();
		if(jaxbActionsList != null) {
			for(ActionType jaxbAction : jaxbActionsList) {
				p.addAction(jaxbAction.getUrl());
			}
		}
		
		List<SectionType> jaxbSectionsList = jaxbPermission.getSection();
		if(jaxbSectionsList != null) {
			for(SectionType jaxbSection : jaxbSectionsList) {
				p.addSection(jaxbSection.getName());
			}
		}
		
		return p;
	}
	
	private boolean isBlank(String s) {
		return (s == null || s.trim().isEmpty());
	}
}
