package com.axelor.apps.supplychain.web;

import com.axelor.apps.stock.db.StockMove;
import com.axelor.apps.stock.db.repo.StockMoveRepository;
import com.axelor.apps.supplychain.db.SupplierRating;
import com.axelor.apps.supplychain.service.SupplierRatingServiceImpl;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import javax.inject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SupplierRatingController {

  private static final Logger LOG = LoggerFactory.getLogger(SupplierRatingController.class);

  private SupplierRatingServiceImpl service;

  @Inject
  public SupplierRatingController(SupplierRatingServiceImpl service) {
    this.service = service;
  }

  public void rating(ActionRequest request, ActionResponse response) {

    SupplierRating supplierRating = request.getContext().asType(SupplierRating.class);

    Integer paymentDelayRatingSelect = supplierRating.getPaymentDelayRatingSelect();
    Integer deliveryTimeRatingSelect = supplierRating.getDeliveryTimeRatingSelect();
    Integer complianceWithTechnicalSpecificationsRatingSelect =
        supplierRating.getComplianceWithTechnicalSpecificationsRatingSelect();
    Integer priceRatingSelect = supplierRating.getPriceRatingSelect();
    Integer quantityComplianceRatingSelect = supplierRating.getQuantityComplianceRatingSelect();
    Integer complaintManagementRatingSelect = supplierRating.getComplaintManagementRatingSelect();
    Integer priceStabilityRatingSelect = supplierRating.getPriceStabilityRatingSelect();

    if (paymentDelayRatingSelect != null
        && deliveryTimeRatingSelect != null
        && complianceWithTechnicalSpecificationsRatingSelect != null
        && priceRatingSelect != null
        && quantityComplianceRatingSelect != null
        && complaintManagementRatingSelect != null
        && priceStabilityRatingSelect != null) {
      service.calculatRating(supplierRating);
      response.setValue("overallScore", supplierRating.getOverallScore());
    } else {
      String message = "You should fill all Fields First.";
      response.setFlash(message);
    }
  }

  public void rateSupplier(ActionRequest request, ActionResponse response) {
    System.out.println("Start");
    StockMove stockMovetemp = request.getContext().asType(StockMove.class);
    StockMove stockMove = Beans.get(StockMoveRepository.class).find(stockMovetemp.getId());
    service.createRating(stockMove);
  }
}
