/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sun.tutorial.javaee.dukesbank.request;

import com.sun.tutorial.javaee.dukesbank.entity.Account;
import com.sun.tutorial.javaee.dukesbank.entity.Customer;
import com.sun.tutorial.javaee.dukesbank.entity.Tx;
import com.sun.tutorial.javaee.dukesbank.util.Debug;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Dani
 */
@Stateless
public class StatisticsCounterBean implements StatisticsCounter {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("dukesbank-ejb");

    //@PersistenceContext(unitName="dukesbank-ejb")
    private EntityManager em = emf.createEntityManager();

    private Collection customers = null;
    private Collection accounts = null;
    private Collection txs = null;

    public StatisticsCounterBean() {
        init();
    }

    @Override
    public int customersNumber() {
        return customers.size();
    }

    @Override
    public String mostCustomerCity() {
        Iterator i = customers.iterator();
        Map<String, Integer> cityNo = new HashMap<String, Integer>();
        int max=0;
        String maxcity="";

        while(i.hasNext()){
            Customer customer = (Customer)i.next();
            String city = customer.getCity();
            if(cityNo.containsKey(city)){
                int no = cityNo.get(city);
                cityNo.put(city, no+1);
            }
            else{
                cityNo.put(city, 1);
            }
        }

        for(String s:cityNo.keySet()){
            if( cityNo.get(s) > max){
                max=cityNo.get(s);
                maxcity=s;
            }
        }

        return maxcity;
    }

    @Override
    public int accountsNumber() {
        return accounts.size();
    }

    @Override
    public int transactionsNumber() {
        return txs.size();
    }

    @Override
    public double totalBalance() {
        double balance= 0.0;
        Iterator i = accounts.iterator();
        
        while(i.hasNext()){
            Account account = (Account) i.next();
            balance+=account.getBalance().doubleValue();            
        }

        return balance;

    }

    @Override
    public Map<String, Integer> noOfAccountsByType() {
        Map<String, Integer> abt = new TreeMap<String, Integer>();
        Iterator i = accounts.iterator();

        while(i.hasNext()){
            Account account = (Account)i.next();
            if(abt.containsKey(account.getType())){
                String s = account.getType();
                int no=abt.get(s);
                abt.put(s, no+1);
            }
            else{
                abt.put(account.getType(), 1);
            }
        }

        return abt;
    }

    @Override
    public Map<Long, Integer> noOfTransactionsOfAccounts() {
        Map<Long, Integer> toa = new TreeMap<Long, Integer>();
        Iterator i = txs.iterator();

        while(i.hasNext()){
            Tx tx = (Tx)i.next();
            if(toa.containsKey(tx.getAccount().getId())){
                long l = tx.getAccount().getId();
                int no=toa.get(l);
                toa.put(l, no+1);
            }
            else{
                toa.put(tx.getAccount().getId(), 1);
            }
        }

        return toa;
    }

    private void getCustomers(){
        customers = em.createNamedQuery("Customer.GetAll").getResultList();
    }

    private void getAccounts(){
        accounts = em.createNamedQuery("Account.GetAll").getResultList();
    }

    private void getTxs(){
        txs = em.createNamedQuery("Tx.GetAll").getResultList();
    }

    @Override
    public void init(){
        this.getCustomers();
        this.getAccounts();
        this.getTxs();
    }

}
