package com.axelor.apps.purchase.web;

import com.axelor.apps.base.db.Currency;
import com.axelor.apps.base.db.repo.CurrencyRepository;
import com.axelor.apps.base.service.CurrencyConversionService;
import com.axelor.apps.purchase.db.ImportationFolder;
import com.axelor.apps.purchase.db.PurchaseOrder;
import com.axelor.apps.purchase.db.repo.ImportationFolderRepository;
import com.axelor.apps.purchase.service.ImportationFolderService;
import com.axelor.apps.purchase.service.ImportationFolderServiceImpl;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
import com.axelor.rpc.ActionResponse;
import com.google.inject.Singleton;
import wslite.json.JSONException;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.List;

@Singleton
public class ImportationFolderController {
    public void draftImportationFolder(ActionRequest request, ActionResponse response) {
        ImportationFolder importationFolder = request.getContext().asType(ImportationFolder.class);
        Beans.get(ImportationFolderService.class).draftImportationFolder(importationFolder);
    }

    public void openImportationFolder(ActionRequest request, ActionResponse response) throws AxelorException {
        ImportationFolder importationFolder = request.getContext().asType(ImportationFolder.class);
        Beans.get(ImportationFolderService.class).openImportationFolder(importationFolder);
    }

    public void closeImportationFolder(ActionRequest request, ActionResponse response) {
        ImportationFolder importationFolder = request.getContext().asType(ImportationFolder.class);
        Beans.get(ImportationFolderService.class).closeImportationFolder(importationFolder);
    }

    public void cancelImportationFolder(ActionRequest request, ActionResponse response) {
        ImportationFolder importationFolder = request.getContext().asType(ImportationFolder.class);
        Beans.get(ImportationFolderService.class).cancelImportationFolder(importationFolder);
    }

    public void calculateSum(ActionRequest request, ActionResponse response)
                throws MalformedURLException, JSONException, AxelorException {
    
            ImportationFolder iimportationFolder = (ImportationFolder) request.getContext().asType(ImportationFolder.class);
            ImportationFolder importationFolder = Beans.get(ImportationFolderRepository.class)
                    .find(iimportationFolder.getId());
            List<PurchaseOrder> purchaseOrders = importationFolder.getPurchaseOrderList();


            Beans.get(ImportationFolderServiceImpl.class).calculateSum(purchaseOrders,
                    importationFolder);

        }
}
