/**
 * Copyright 2015 by Eclettica s.r.l. All Rights Reserved.
 *
 * This file is part of Vescafe project.
 *
 * The contents of this file are subject to the Eclettica Private License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.eclettica.net/
 * 
 * Original file name: AppStart.java
 * Created by luca at 10:16:26 AM
 */
package schedulers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import core.services.ApiService;
import models.User.Authorizations;
import models.User.Roles;
import net.eclettica.esecure.manager.EsAccountManager;
import net.eclettica.esecure.models.EsAuth;
import net.eclettica.esecure.models.EsRole;
import play.Logger;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
import utils.cfg.CfgUtil;
import utils.cfg.ConfigManager;
import utils.cfg.PlayConfigAdapter;

/**
 * 
 * @author luca
 * 
 */

@OnApplicationStart
public class AppStart extends Job {

	public void doJob() {
		ConfigManager.instance().setAdapter(PlayConfigAdapter.instance());
		EsAccountManager esdao = configureESecurePlay1();
		if (esdao == null) {
			throw new RuntimeException();
		}
		initializeAuthsAndRoles(esdao);
		checkMediaDirectory();
	}

	private void checkMediaDirectory() {
		ApiService.instance().createMediaDirectory();
	}

	private EsAccountManager configureESecurePlay1() {
		String clazz = CfgUtil.getString("esecure.dao.class", "net.eclettica.esecure.dao.EsDao");

		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("authconfig.minpassword", 6);
		map.put("authconfig.session", 60);

		try {
			Class c = Class.forName(clazz);
			EsAccountManager esdao = (EsAccountManager) c.newInstance();

			net.eclettica.esecure.services.AuthService.instance().associateManager(esdao);
			net.eclettica.esecure.conf.AuthConfig.getInstance().configure(map);

			return esdao;
		} catch (Exception ex) {
			Logger.error(ex, "Startup error");
			return null;
		}
	}

	private void initializeAuthsAndRoles(EsAccountManager esdao) {
		List<EsAuth> adminAuths = new LinkedList<EsAuth>();
		List<EsAuth> contentProviderAuths = new LinkedList<EsAuth>();

		// for (Authorizations auth : Authorizations.values()) {
		// EsAuth esAuth = esdao.findAuthByName(auth.name());
		// if (esAuth != null) {
		// continue;
		// }
		// esdao.createAuth(auth.name(), auth.name());
		// esAuth = esdao.findAuthByName(auth.name());
		// if (esAuth == null) {
		// continue;
		// }
		// adminAuths.add(esAuth);
		// if (auth.equals(Authorizations.TEST_MANAGEMENT) ||
		// auth.equals(Authorizations.MEDIA_MANAGEMENT)
		// || auth.equals(Authorizations.REPORT_MANAGEMENT)) {
		// contentProviderAuths.add(esAuth);
		// }
		// }

		for (String auth : Authorizations.values()) {
			EsAuth esAuth = esdao.findAuthByName(auth);
			if (esAuth != null) {
				continue;
			}
			esdao.createAuth(auth, auth);
			esAuth = esdao.findAuthByName(auth);
			if (esAuth == null) {
				continue;
			}
			adminAuths.add(esAuth);
			if (auth.equals(Authorizations.TEST_MANAGEMENT) || auth.equals(Authorizations.MEDIA_MANAGEMENT)
					|| auth.equals(Authorizations.REPORT_MANAGEMENT)) {
				contentProviderAuths.add(esAuth);
			}
		}

		for (String role : Roles.values()) {
			EsRole esRole = esdao.findRole(role);
			if (esRole != null) {
				continue;
			}
			esdao.createRole(role, role);
		}

		EsRole admin = null;
		for (EsAuth adminAuth : adminAuths) {
			if (admin == null) {
				admin = esdao.findRole(Roles.ADMIN);
				if (admin == null) {
					throw new RuntimeException("ADMIN Role is null!!!");
				}
			}
			esdao.addAuthRoles(adminAuth, admin);
			esdao.addRoleAuths(adminAuth, admin);
		}

		EsRole contentProvider = null;
		for (EsAuth contentProviderAuth : contentProviderAuths) {
			if (contentProvider == null) {
				contentProvider = esdao.findRole(Roles.CONTENT_PROVIDER);
				if (contentProvider == null) {
					throw new RuntimeException("CONTENT_PROVIDER Role is null!!!");
				}
			}
			esdao.addAuthRoles(contentProviderAuth, contentProvider);
			esdao.addRoleAuths(contentProviderAuth, contentProvider);
		}
	}
}