package org.se.lab.web;

import org.apache.log4j.Logger;
import org.se.lab.db.data.Community;
import org.se.lab.service.CommunityService;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Named
@RequestScoped
public class CommunityOverviewBean {

	private final static Logger LOG = Logger.getLogger(CommunityOverviewBean.class);

	@Inject
	private CommunityService service;

	private List<Community> communities;
	private Community selectedCommunity;

	private String newCommunityName;
	private String newCommunityDescription;

	private Flash flash;
	private FacesContext context;
	private int userId = 0;

	@PostConstruct
	public void init() {
		context = FacesContext.getCurrentInstance();
		Map<String, Object> session = context.getExternalContext().getSessionMap();
		userId = (int) session.get("user");

		communities = new ArrayList<>();
		communities = service.findAll();
	}

	public void reset() {
		setNewCommunityDescription(null);
		setNewCommunityName(null);
		setSelectedCommunity(null);
	}

	public List<Community> getCommunities() {
		return communities;
	}

	public void setCommunities(List<Community> communities) {
		this.communities = communities;
	}

	public Community getSelectedCommunity() {
		return selectedCommunity;
	}

	public void setSelectedCommunity(Community selectedCommunity) {
		this.selectedCommunity = selectedCommunity;
	}

	public String getNewCommunityName() {
		return newCommunityName;
	}

	public void setNewCommunityName(String newCommunityName) {
		this.newCommunityName = newCommunityName;
	}

	public String getNewCommunityDescription() {
		return newCommunityDescription;
	}

	public void setNewCommunityDescription(String newCommunityDescription) {
		this.newCommunityDescription = newCommunityDescription;
	}

	public String createNewCommunity() {
		Community newCommunity;
		if (!newCommunityName.isEmpty()) {
			FacesContext context = FacesContext.getCurrentInstance();

			if (newCommunityDescription.isEmpty()) {
				newCommunityDescription = "<Edit me ...>";
			}

			try {
				newCommunity = service.request(newCommunityName, newCommunityDescription, userId);
				newCommunity = service.findByName(newCommunityName);

				context.getExternalContext().getSessionMap().put("communityId", newCommunity.getId());
				LOG.info(newCommunity.getName() + " community created with the id: " + newCommunity.getId() + ".");

			} catch (Exception e) {
				context.addMessage(null,
						new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fail!", "Unable to request community."));
				reset();
				return "";
			}
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Community requested."));
		}
		reset();
		return "";
	}

	public void gotoCom() {
		LOG.info("In Method gotoCom");
		if (selectedCommunity != null) {
			LOG.info("Selected Community: " + selectedCommunity.getId() + " " + selectedCommunity.getDescription());

			FacesContext context = FacesContext.getCurrentInstance();
			context.getExternalContext().getSessionMap().put("communityId", selectedCommunity.getId());

			try {
				context.getExternalContext().redirect("/pse/communityprofile.xhtml");
			} catch (IOException e) {
				LOG.error("Can't redirect to /pse/communityprofile.xhtml");
			}
		}
	}

	public void deleteCom() {
		LOG.info("In Method deleteCom");

		if (selectedCommunity == null)
			return;

		LOG.info("Selected Community: " + selectedCommunity.getId() + " " + selectedCommunity.getDescription());

		service.delete(selectedCommunity);

		try {
			context.getExternalContext().redirect("/pse/communityoverview.xhtml");
		} catch (IOException e) {
			LOG.error("Can't redirect to /pse/communityoverview.xhtml");
		}
	}
}

@@ -290,21 +290,24 @@
 context.
-Faces
+External
 Context;
@@ -335,20 +335,27 @@
 ontext.F
-lash
+acesContext
 ;%0Aimport
@@ -859,19 +859,22 @@
 ate 
-Flash flash
+int userId = 0
 ;%0A%09p
@@ -880,21 +880,24 @@
 private 
-Faces
+External
 Context 
@@ -908,33 +908,8 @@
 ext;
-%0A%09private int userId = 0;
 %0A%0A%09@
@@ -985,24 +985,45 @@
 ntInstance()
+.getExternalContext()
 ;%0A%09%09Map%3CStri
@@ -1052,37 +1052,16 @@
 context.
-getExternalContext().
 getSessi
@@ -3816,37 +3816,16 @@
 context.
-getExternalContext().
 redirect

Refactor CommunityOverviewBean