package org.hso.ecommerce.supplier;

import org.hso.ecommerce.supplier.data.Article;
import org.hso.ecommerce.supplier.data.Order;
import org.hso.ecommerce.supplier.data.OrderConfirmation;
import org.hso.ecommerce.supplier.data.Supplier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class RequestController {

    private final HashMap<String, Integer> dailySalesVolumeCent = new HashMap<>();
    private final HashMap<String, Supplier> knownSuppliers  = new HashMap<>();
    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @PostConstruct
    public void init() throws IOException {
        for (Supplier s : ConfigurationReader.read()) {
            knownSuppliers.put(s.id, s);
        }
    }


    @GetMapping("/")
    public List<Supplier> index() {
        return new ArrayList<>(knownSuppliers.values());
    }

    @GetMapping("/{supplier}/")
    public Supplier supplier(HttpServletResponse res, @PathVariable("supplier") String supplierName) {
        Supplier s = knownSuppliers.get(supplierName);
        if(s == null) {
            res.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
        return s;
    }

    @PostMapping("/{supplier}/order")
    public OrderConfirmation order(HttpServletResponse res, @PathVariable("supplier") String supplierName, @RequestBody Order order) {
        Supplier s = knownSuppliers.get(supplierName);
        if(s == null) {
            res.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }

        String dateKey = simpleDateFormat.format(new Date());
        int dailyVolume = dailySalesVolumeCent.getOrDefault(dateKey,0);

        Article a = s.findArticle(order.manufacturer, order.articleNumber);
        if(a == null) {
            res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return null;
        }

        int priceNet = a.pricePerUnitNet * order.quantity;
        int discount = 0;
        if(dailyVolume >= s.discount.minimumDailySalesVolumeNetCent) {
            discount = (priceNet * s.discount.percentDiscount) / 100;
        }

        OrderConfirmation confirmation = new OrderConfirmation();
        confirmation.articleNumber = order.articleNumber;
        confirmation.discountNetCent = discount;
        confirmation.pricePerUnitNetCent = a.pricePerUnitNet;
        confirmation.manufacturer = a.manufacturer;
        confirmation.quantity = order.quantity;
        confirmation.totalPriceNetCharged = priceNet - discount;

        if (confirmation.totalPriceNetCharged > order.maxTotalPriceCentNet) {
            res.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
            return null;
        }

        dailyVolume += confirmation.totalPriceNetCharged;
        dailySalesVolumeCent.put(dateKey, dailyVolume);

        return confirmation;
    }
}
