/**
 * 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.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.Arrays;
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
    // History 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;
    }


    // 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 Integer getMaxValue(Integer[] list){
        Arrays.sort(list);

        return list[list.length - 1];
    }

    private Integer getMinValue(Integer[] list){
        Arrays.sort(list);

        return list[0];
    }


}

@@ -188,16 +188,47 @@
 eceipt;%0A
+import com.evapps.Entity.User;%0A
 import c
@@ -930,23 +930,23 @@
 %0A    // 
-History
+Product
  related
@@ -2339,16 +2339,701 @@
 %0A    %7D%0A%0A
+    // Transaction Related Functions%0A    public Map%3CString, Float%3E userAveragePurchaseByDollar()%7B%0A%0A        try %7B%0A            Map%3CString, Float%3E statistics = fetchUserLegend();%0A%0A            for (String email: statistics.keySet()) %7B%0A                int count = 0;%0A                for (Receipt receipt : receiptRepository.findByUser(email)) %7B%0A                    statistics.replace(email, statistics.get(email) + receipt.getTotal());%0A                    count++;%0A                %7D%0A%0A                statistics.replace(email, statistics.get(email)/count);%0A            %7D%0A%0A            return statistics;%0A        %7D catch (Exception exp) %7B%0A            //%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A
 %0A    // 
@@ -3454,32 +3454,16 @@
 oduct p:
-%0A               
  product
@@ -3475,34 +3475,32 @@
 itory.findAll())
- %7B
 %0A               
@@ -3488,36 +3488,32 @@
 ())%0A            
-    
 legend.putIfAbse
@@ -3531,34 +3531,24 @@
 plier(), 0);
-%0A        %7D
 %0A%0A        re
@@ -3583,191 +3583,203 @@
 ate 
-Integer getMaxValue(Integer%5B%5D list)%7B%0A        Arrays.sort(list);%0A%0A        return list%5Blist.length - 1%5D;%0A
+Map%3CString, Float%3E fetchUserLegend()%7B%0A        Map%3CString, Float%3E legend = new HashMap%3C%3E();%0A%0A        for (User user: userRepository.findAll())%0A   
     
-%7D%0A%0A
     
-private Integer getMinValue(Integer%5B%5D list)%7B%0A        Arrays.sort(list
+ legend.putIfAbsent(user.getEmail(), 0.00f
 );%0A%0A
@@ -3798,14 +3798,13 @@
 rn l
-ist%5B0%5D
+egend
 ;%0A  
@@ -3808,11 +3808,10 @@
 %0A    %7D%0A%0A
-%0A
 %7D%0A

Add userAveragePurchaseByDollar