/*
 * 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.guessnumber;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import org.icefaces.application.PortableRenderer;
import org.icefaces.application.PushRenderer;

@ManagedBean(name = "statistics")
@ApplicationScoped
public class Statistics implements Serializable {
    private final Set<Record> recordSet = new HashSet<Record>();

    private PortableRenderer portableRenderer;

    public void record(int numberOfGuesses) {
        recordSet.add(new Record(numberOfGuesses));
        portableRenderer.render("guessnumber");
    }

    public int getNumberOfCorrectGuesses() {
        return recordSet.size();
    }

    @PostConstruct
    public void setUp() {
        portableRenderer = PushRenderer.getPortableRenderer();
    }

    @PreDestroy
    public void tearDown() {
    }

    private static class Record {
        private final int numberOfGuesses;

        private Record(int numberOfGuesses) {
            this.numberOfGuesses = numberOfGuesses;
        }

        private int getNumberOfGuesses() {
            return numberOfGuesses;
        }
    }
}
