/*
 * 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.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

@ManagedBean(name = "participantView")
@CustomScoped(value = "#{window}")
public class ParticipantView
implements Serializable {
    private static final Logger LOGGER = Logger.getLogger(ParticipantView.class.getName());

    private String realm;
    private int tabSelectedIndex;
    private String selectedPollId;
    private String pollStatement;
    private String pollDetail;

    public ParticipantView() {
        setRealm(FacesUtils.getExternalContext().getRequestParameterMap().get("realm"));
    }

    public String cancelPoll() {
        reset();
        return "";
    }

    public String createPoll() {
        if (pollStatement != null && pollStatement.trim().length() != 0 &&
            pollDetail != null && pollDetail.trim().length() != 0) {

            PollCollection _pollCollection = PollCollection.getInstance();
            _pollCollection.addPoll(_pollCollection.createPoll(pollStatement, pollDetail));
            reset();
        }
        return "";
    }

    public static ParticipantView getInstance() {
        return (ParticipantView)FacesUtils.getManagedBean("participantView");
    }

    public String getPollDetail() {
        return pollDetail;
    }

    public String getPollStatement() {
        return pollStatement;
    }

    public String getRealm() {
        return realm;
    }

    public Poll getSelectedPoll() {
        return PollCollection.getInstance().getPoll(selectedPollId);
    }

    public int getTabSelectedIndex() {
        return tabSelectedIndex;
    }

    public void setPollDetail(final String pollDetail) {
        if ((this.pollDetail == null && pollDetail != null) ||
            (this.pollDetail != null && !this.pollDetail.equals(pollDetail))) {

            this.pollDetail = pollDetail;
        }
    }

    public void setPollStatement(final String pollStatement) {
        if ((this.pollStatement == null && pollStatement != null) ||
            (this.pollStatement != null && !this.pollStatement.equals(pollStatement))) {

            this.pollStatement = pollStatement;
        }
    }

    public void setRealm(final String realm) {
        if ((this.realm == null && realm != null) ||
            (this.realm != null && !this.realm.equals(realm))) {

            this.realm = realm;
        }
    }

    public void setTabSelectedIndex(final int tabSelectedIndex) {
        if (this.tabSelectedIndex != tabSelectedIndex) {
            this.tabSelectedIndex = tabSelectedIndex;
        }
    }

    void setSelectedPoll(final Poll poll) {
        if ((this.selectedPollId == null && poll != null) ||
            (this.selectedPollId != null && !this.selectedPollId.equals(poll.getId()))) {

            this.selectedPollId = poll.getId();
        }
    }

    private void reset() {
        setPollStatement(null);
        setPollDetail(null);
    }
}
