/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.faces.demo.poll;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.servlet.http.HttpSession;

import org.icefaces.application.PushRenderer;

@ManagedBean(name = "participant")
@SessionScoped
public class Participant
implements Serializable {
    private static final Logger LOGGER = Logger.getLogger(Participant.class.getName());

    private final Set<String> againstSet = new HashSet<String>();
    private final Set<String> forSet = new HashSet<String>();

    private final String id = UUID.randomUUID().toString();

    public String pushMode;

    public Participant() {
        // Do nothing.
    }

    public int getAgainstVotes() {
        return againstSet.size();
    }

    public int getForVotes() {
        return forSet.size();
    }

    public String getId() {
        return id;
    }

    public static Participant getInstance() {
        return (Participant)FacesUtils.getManagedBean("participant");
    }

    public String getResult() {
        switch (((Integer)forSet.size()).compareTo(againstSet.size())) {
            case -1:
                return "assertive";
            case 0:
                return "indifferent";
            case 1:
                return "agreeable";
            default:
                // This should never happen...
                return "[UNKNOWN]";
        }
    }

    public boolean hasVotedAgainst(final Poll poll) {
        return againstSet.contains(poll.getId());
    }

    public boolean hasVotedFor(final Poll poll) {
        return forSet.contains(poll.getId());
    }

    @PostConstruct
    public void setUp() {
        pushMode = ApplicationConfiguration.getInstance().getPushMode();
        if (pushMode.equalsIgnoreCase(Constants.PushMode.PUSH) ||
            pushMode.equalsIgnoreCase(Constants.PushMode.CLOUD_PUSH)) {

            PushRenderer.addCurrentSession(Poll.POLL_GROUP_NAME);
        }
    }

    public void voteAgainst(final String id) {
        boolean _modified = false;
        if (forSet.contains(id)) {
            forSet.remove(id);
            _modified = true;
        }
        if (!againstSet.contains(id)) {
            againstSet.add(id);
            _modified = true;
        }
        if (_modified) {
            persist();
        }
    }

    public void voteFor(final String id) {
        boolean _modified = false;
        if (againstSet.contains(id)) {
            againstSet.remove(id);
            _modified = true;
        }
        if (!forSet.contains(id)) {
            forSet.add(id);
            _modified = false;
        }
        if (_modified) {
            persist();
        }
    }

    void persist() {
        ((HttpSession)FacesUtils.getExternalContext().getSession(false)).setAttribute("participant", this);
    }
}
