package name.abuchen.portfolio.online.impl;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.osgi.framework.FrameworkUtil;

import com.google.common.base.Strings;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.money.CurrencyConverter;
import name.abuchen.portfolio.money.Values;
import name.abuchen.portfolio.snapshot.ClientSnapshot;
import name.abuchen.portfolio.snapshot.PortfolioSnapshot;
import name.abuchen.portfolio.util.WebAccess;

public class DivvyDiaryUploader
{

    @SuppressWarnings("unchecked")
    public void upload(Client client, CurrencyConverter converter, String apiKey) throws IOException
    {
        if (apiKey == null)
            return;

        ClientSnapshot snapshot = ClientSnapshot.create(client, converter, LocalDate.now());
        PortfolioSnapshot portfolio = snapshot.getJointPortfolio();

        List<JSONObject> payload = portfolio.getPositions().stream() //
                        .filter(p -> p.getInvestmentVehicle() instanceof Security)
                        .filter(p -> !Strings.isNullOrEmpty(((Security) p.getInvestmentVehicle()).getIsin())).map(p -> {
                            JSONObject item = new JSONObject();
                            item.put("isin", ((Security) p.getInvestmentVehicle()).getIsin()); //$NON-NLS-1$
                            item.put("quantity", p.getShares() / Values.Share.divider()); //$NON-NLS-1$
                            return item;
                        }) //
                        .collect(Collectors.toList());

        if (payload.isEmpty())
            return;

        String session = new WebAccess("api.divvydiary.com", "/session") //$NON-NLS-1$ //$NON-NLS-2$
                        .addHeader("X-API-Key", apiKey) //$NON-NLS-1$
                        .addUserAgent("PortfolioPerformance/" //$NON-NLS-1$
                                        + FrameworkUtil.getBundle(PortfolioReportNet.class).getVersion().toString())
                        .get();

        String userId = String.valueOf(((JSONObject) JSONValue.parse(session)).get("id")); //$NON-NLS-1$
        if (Strings.isNullOrEmpty(userId))
            throw new IOException("DivvyDiary.com userId not found"); //$NON-NLS-1$

        WebAccess upload = new WebAccess("api.divvydiary.com", "/users/" + userId + "/depot/import"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        upload.addHeader("X-API-Key", apiKey); //$NON-NLS-1$
        upload.addHeader("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$

        JSONArray json = new JSONArray();
        json.addAll(payload);

        upload.post(JSONValue.toJSONString(json));
    }

}

@@ -484,16 +484,59 @@
 verter;%0A
+import name.abuchen.portfolio.money.Quote;%0A
 import n
@@ -571,16 +571,16 @@
 Values;%0A
-
 import n
@@ -676,32 +676,232 @@
 tfolioSnapshot;%0A
+import name.abuchen.portfolio.snapshot.security.SecurityPerformanceIndicator;%0Aimport name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot;%0Aimport name.abuchen.portfolio.util.Interval;%0A
 import name.abuc
@@ -1315,24 +1315,241 @@
 rtfolio();%0A%0A
+        SecurityPerformanceSnapshot performance = SecurityPerformanceSnapshot.create(client, converter,%0A                        Interval.of(LocalDate.MIN, LocalDate.now()), SecurityPerformanceIndicator.Costs.class);%0A%0A
         List
@@ -1977,32 +1977,32 @@
 ; //$NON-NLS-1$%0A
-
                 
@@ -2081,32 +2081,580 @@
 ; //$NON-NLS-1$%0A
+%0A                            performance.getRecord(p.getSecurity()).ifPresent(record -%3E %7B%0A%0A                                Quote fifo = record.getFifoCostPerSharesHeld();%0A%0A                                JSONObject buyin = new JSONObject();%0A                                buyin.put(%22price%22, fifo.getAmount() / Values.Quote.divider()); //$NON-NLS-1$%0A                                buyin.put(%22currency%22, fifo.getCurrencyCode()); //$NON-NLS-1$%0A                                item.put(%22buyin%22, buyin); //$NON-NLS-1$%0A                            %7D);%0A%0A
                 

Include FIFO purchase price in payload to DivvyDiary