package com.axelor.apps.purchase.service;

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

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.base.service.CurrencyService;
import com.axelor.apps.base.service.administration.SequenceService;
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.exception.AxelorException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

import wslite.json.JSONException;

import com.axelor.inject.Beans;


public class ImportationFolderServiceImpl implements ImportationFolderService {

    @Inject
    protected ImportationFolderRepository importationFolderRepository;

    @Override
    @Transactional
    public void draftImportationFolder(ImportationFolder importationFolder) {
        // TODO Auto-generated method stub
        // importationFolder.setStatusSelect(ImportationFolderRepository.STATUS_DRAFT);
    }

    @Override
    @Transactional
    public void openImportationFolder(ImportationFolder importationFolder) throws AxelorException {
        // importationFolder.setStatusSelect(ImportationFolderRepository.STATUS_OPEND);
    }

    @Override
    @Transactional
    public void closeImportationFolder(ImportationFolder importationFolder) {
        // importationFolder.setStatusSelect(ImportationFolderRepository.STATUS_CLOSED);
    }

    @Override
    @Transactional
    public void cancelImportationFolder(ImportationFolder importationFolder) {
        // importationFolder.setStatusSelect(ImportationFolderRepository.STATUS_CANCELED);
    }
    
    @Override
    @Transactional
    public void calculateSum(List<PurchaseOrder> purchaseOrders,ImportationFolder importationFolder) throws MalformedURLException, JSONException, AxelorException {
        BigDecimal amount = BigDecimal.ZERO;
        BigDecimal taxAmount = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;


        for (PurchaseOrder purchaseOrder : purchaseOrders) {
            if(purchaseOrder.getCurrency().getId() == 148 || purchaseOrder.getCurrency().getId() == 46){
                Currency currency = Beans.get(CurrencyRepository.class).find(purchaseOrder.getCurrency().getId());
                Currency currencyDzd = Beans.get(CurrencyRepository.class).findByCode("DZD");
                BigDecimal currencyAmount = Beans.get(CurrencyService.class).getCurrencyConversionRate(currency, currencyDzd);

                currencyAmount = currencyAmount.setScale(2, BigDecimal.ROUND_HALF_EVEN);

                BigDecimal finalAmount = purchaseOrder.getExTaxTotal().multiply(currencyAmount);
                BigDecimal finalTaxAmount = purchaseOrder.getTaxTotal().multiply(currencyAmount);
                BigDecimal finalTotalAmount = purchaseOrder.getInTaxTotal().multiply(currencyAmount);

                amount = amount.add(finalAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                taxAmount = taxAmount.add(finalTaxAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                totalAmount = totalAmount.add(finalTotalAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN);

            }else{
                amount = amount.add(purchaseOrder.getExTaxTotal());
                taxAmount = taxAmount.add(purchaseOrder.getTaxTotal());
                totalAmount = totalAmount.add(purchaseOrder.getInTaxTotal());
            }
            
        }
        
        importationFolder.setAmount(amount);
        importationFolder.setTaxAmount(taxAmount);
        importationFolder.setTotalAmount(totalAmount);

        importationFolderRepository.save(importationFolder);

    }

}
