package com.axelor.apps.stock.web;

import com.axelor.apps.stock.db.StockProductionRequest;
import com.axelor.apps.stock.db.StockProductionRequestLine;
import com.axelor.apps.stock.service.StockProductionRequestLineService;
import com.axelor.db.mapper.Mapper;
import com.axelor.exception.AxelorException;
import com.axelor.exception.ResponseMessageType;
import com.axelor.exception.service.TraceBackService;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.axelor.rpc.Context;

public class StockProductionRequestLineController {

  public void setProductInfo(ActionRequest request, ActionResponse response) {

    StockProductionRequestLine productionRequestLine;

    try {
      productionRequestLine = request.getContext().asType(StockProductionRequestLine.class);
      StockProductionRequest productionRequest = productionRequestLine.getStockProductionRequest();

      if (productionRequest == null) {
        productionRequest = request.getContext().getParent().asType(StockProductionRequest.class);
      }

      if (productionRequestLine.getProduct() == null) {
        productionRequestLine = new StockProductionRequestLine();
        response.setValues(Mapper.toMap(productionRequestLine));
        return;
      }

      Beans.get(StockProductionRequestLineService.class)
          .setProductInfo(productionRequest, productionRequestLine, productionRequest.getCompany());
      response.setValues(productionRequestLine);
    } catch (Exception e) {
      productionRequestLine = new StockProductionRequestLine();
      response.setValues(Mapper.toMap(productionRequestLine));
      TraceBackService.trace(response, e, ResponseMessageType.INFORMATION);
    }
  }

  public void compute(ActionRequest request, ActionResponse response) throws AxelorException {
    StockProductionRequestLine productionRequestLine =
        request.getContext().asType(StockProductionRequestLine.class);
    StockProductionRequest productionRequest = productionRequestLine.getStockProductionRequest();
    if (productionRequest == null) {
      Context parentContext = request.getContext().getParent();
      Context superParentContext = parentContext.getParent();
      if (parentContext.getContextClass().equals(StockProductionRequest.class)) {
        productionRequest = parentContext.asType(StockProductionRequest.class);
      } else if (superParentContext.getContextClass().equals(StockProductionRequest.class)) {
        productionRequest = superParentContext.asType(StockProductionRequest.class);
      } else {
        return;
      }
    }
    productionRequestLine =
        Beans.get(StockProductionRequestLineService.class)
            .compute(productionRequestLine, productionRequest);
    response.setValue("unitPriceUntaxed", productionRequestLine.getUnitPriceUntaxed());
    response.setValue("unitPriceTaxed", productionRequestLine.getUnitPriceTaxed());
    response.setValue(
        "companyUnitPriceUntaxed", productionRequestLine.getCompanyUnitPriceUntaxed());
  }
}
