package com.example.orders;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ReportGenerator {

    private ProductRepository productRepository;

    public ReportGenerator(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    // 月次レポートを作る(長い・深い・重複が多い: リファクタリング演習用)
    public String generateMonthlyReport(List<Order> orders, int year, int month) {
        StringBuilder sb = new StringBuilder();
        sb.append("==============================\n");
        sb.append(" MONTHLY ORDER REPORT " + year + "/" + (month < 10 ? "0" + month : "" + month) + "\n");
        sb.append("==============================\n");
        int totalSales = 0;
        int totalOrders = 0;
        int canceled = 0;
        Map<String, Integer> categorySales = new HashMap<String, Integer>();
        Map<String, Integer> customerCount = new HashMap<String, Integer>();
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()) {
            Order o = it.next();
            Calendar cal = Calendar.getInstance();
            cal.setTime(o.getCreatedAt());
            if (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) + 1 == month) {
                if (o.getStatus().equals("CANCELED")) {
                    canceled = canceled + 1;
                } else {
                    totalOrders = totalOrders + 1;
                    totalSales = totalSales + o.getTotalPrice();
                    Iterator it2 = o.getLines().iterator();
                    while (it2.hasNext()) {
                        OrderLine line = (OrderLine) it2.next();
                        Product p = productRepository.findById(line.getProductId());
                        if (p != null) {
                            String cat = p.getCategory();
                            if (categorySales.get(cat) == null) {
                                categorySales.put(cat, Integer.valueOf(p.getPrice() * line.getQuantity()));
                            } else {
                                categorySales.put(cat, Integer.valueOf(categorySales.get(cat).intValue() + p.getPrice() * line.getQuantity()));
                            }
                        }
                    }
                    if (customerCount.get(o.getCustomerId()) == null) {
                        customerCount.put(o.getCustomerId(), Integer.valueOf(1));
                    } else {
                        customerCount.put(o.getCustomerId(), Integer.valueOf(customerCount.get(o.getCustomerId()).intValue() + 1));
                    }
                }
            }
        }
        sb.append("orders   : " + totalOrders + "\n");
        sb.append("canceled : " + canceled + "\n");
        sb.append("sales    : " + totalSales + " yen\n");
        sb.append("------------------------------\n");
        sb.append("[sales by category]\n");
        Iterator<String> catIt = categorySales.keySet().iterator();
        while (catIt.hasNext()) {
            String cat = catIt.next();
            sb.append("  " + cat + " : " + categorySales.get(cat) + " yen\n");
        }
        sb.append("------------------------------\n");
        sb.append("[orders by customer]\n");
        Iterator<String> cusIt = customerCount.keySet().iterator();
        while (cusIt.hasNext()) {
            String cus = cusIt.next();
            sb.append("  " + cus + " : " + customerCount.get(cus) + "\n");
        }
        sb.append("==============================\n");
        return sb.toString();
    }
}
