/**
 * Created by Djidjelly Siclait on 12/3/2016.
 */
package com.evapps.Service.Auxiliary;

import com.evapps.Entity.History;
import com.evapps.Entity.Product;
import com.evapps.Entity.Receipt;
import com.evapps.Entity.User;
import com.evapps.Repository.HistoryRepository;
import com.evapps.Repository.ProductRepository;
import com.evapps.Repository.ReceiptRepository;
import com.evapps.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticService {

    // Repositories
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ReceiptRepository receiptRepository;
    @Autowired
    private UserRepository userRepository;

    // Functions
    // Product related Statistics
    public Map<Integer, Integer> productViewStatistics(boolean option){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (History history: historyRepository.findAll())
                for (Product product: history.getBrowsingHistory())
                    statistic.replace(product.getProductId(), statistic.get(product.getProductId()) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<Integer, Integer> productPurchaseStatistics(){

        Map<Integer, Integer> statistic = fetchProductLegend();

        try {
            for (Receipt receipt: receiptRepository.findAll())
                for (Integer p: receipt.getProductList())
                    statistic.replace(p, statistic.get(p) + 1);

            return statistic;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<String, Integer> productSupplierStatistics(){

        try {
            Map<String, Integer> statistic = fetchSupplierLegend();

            for (Product product: productRepository.findAll())
                statistic.replace(product.getSupplier(), statistic.get(product.getSupplier()) + 1);

            return statistic;

        } catch (Exception exp) {
            //
        }

        return null;
    }

    // Transaction Related Functions
    public Map<String, Float> userAveragePurchaseByDollar(){

        try {
            Map<String, Float> statistics = fetchUserLegend();

            for (String email: statistics.keySet()) {
                int count = 0;
                for (Receipt receipt : receiptRepository.findByUser(email)) {
                    statistics.replace(email, statistics.get(email) + receipt.getTotal());
                    count++;
                }

                statistics.replace(email, statistics.get(email)/count);
            }

            return statistics;
        } catch (Exception exp) {
            //
        }

        return null;
    }

    public Map<String, Float> userAverageNumberOfItemPurchase(){

        try {
            Map<String, Float> statistics = fetchUserLegend();

            for (String email: statistics.keySet()) {
                int count = 0;
                for (Receipt receipt : receiptRepository.findByUser(email)) {
                    statistics.replace(email, statistics.get(email) + receipt.getProductList().size());
                    count++;
                }

                statistics.replace(email, statistics.get(email)/count);
            }

            return statistics;
        } catch (Exception exp) {
            //
        }

        return null;
    }


    // Auxiliary Functions
    private Map<Integer, Integer> fetchProductLegend(){
        Map<Integer, Integer> legend = new HashMap<>();

        for (Product p:
             productRepository.findAll()) {
            legend.put(p.getProductId(), 0);
        }

        return legend;
    }

    private Map<String, Integer> fetchSupplierLegend(){
        Map<String, Integer> legend = new HashMap<>();

        for (Product p: productRepository.findAll())
            legend.putIfAbsent(p.getSupplier(), 0);

        return legend;
    }

    private Map<String, Float> fetchUserLegend(){
        Map<String, Float> legend = new HashMap<>();

        for (User user: userRepository.findAll())
            legend.putIfAbsent(user.getEmail(), 0.00f);

        return legend;
    }

}

@@ -519,16 +519,44 @@
 rvice;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -573,16 +573,16 @@
 ashMap;%0A
-
 import j
@@ -971,36 +971,32 @@
  public 
-Map%3CInteger, Integer
+ArrayList%3CString
 %3E produc
@@ -1015,22 +1015,8 @@
 ics(
-boolean option
 )%7B%0A%0A
@@ -1070,32 +1070,86 @@
 ProductLegend();
+%0A        ArrayList%3CString%3E buffer = new ArrayList%3C%3E();
 %0A%0A        try %7B%0A
@@ -1398,32 +1398,259 @@
         
-return statistic
+for (Integer i:%0A                 statistic.values()) %7B%0A                buffer.add(%22'%22 + i.toString() + %22--%22 + productRepository.findByProductId(i).getProductName() + %22',%22 + statistic.get(i).toString());%0A            %7D%0A%0A            return buffer
 ;%0A      

Refactor productViewStatistics in StatisticsService