package com.workfusion.odf.impl.connector;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;

import com.workfusion.odf.api.connector.TransactionSupplier;
import com.workfusion.odf.api.domain.Document;
import com.workfusion.odf.api.domain.Transaction;

/**
 * Fake connector which creates a new transaction and populates it with attributes and documents.
 *
 * @author Andrei Urbanovich
 */
public class TransactionCreateFakeTransactionSupplier implements TransactionSupplier {
    private Logger logger;
    private Map<String, String> params;


    @Inject
    public TransactionCreateFakeTransactionSupplier(final Logger logger, final @Named("botConfigParams") Map<String, String> params) {
        this.logger = logger;
        this.params = params;
    }

    @Override
    public Collection<Transaction> get() {
        List<Transaction> transactions = new ArrayList<>();

        Transaction transaction = new Transaction();
        transaction.setId(generateTransactionId());
        transaction.putAttribute("entityName", "Arcandor");
        transaction.putAttribute("dateOfInvestigation", "September 24, 2018");
        transaction.putAttribute("workerName", "Kyle Hoback, kyle@workfusion.com");
        transaction.putAttribute("overallRecommendation", "Suspicious - Investigate Further");
        transaction.putAttribute("articlesReviewed", "1");
        transaction.putAttribute("suggestedKeywords", "[\"Arcandor AND FRAUD OR MONEY LAUNDERING OR LAUNDERING OR TRAFFICKING OR DRUGS OR ARREST OR SMUGGLING OR BRIBERY OR TERRORISM OR TERRORIST OR EMBEZZLEMENT\"]");
        transaction.putAttribute("highRiskCountriesRelations", "[\"Costa Rica\",\"Macau\",\"Peru\"]");
        transaction.putAttribute("manualTaskLink", "https://demo-spa852.workfusion.com/workfusion/secure/business-process/edit/0a49ebc9-0a59-4567-bc2b-3f90fef5e2f6?actTab=WORKFLOW_DESIGNER");

        List<Document> docs = new ArrayList<>();

        Document doc1 = new Document();
        doc1.setId(generateDocumentId());
        doc1.setName("example.eml");
        doc1.setOriginalDocumentLink("https://wfinst-307-92-db1.workfusion.com/doc-upload/example.eml");
        docs.add(doc1);

        /*
        try {
            Document doc1 = new Document();
            doc1.setId(generateDocumentId());
            doc1.setTextLink("http://www.textlink-1.org");
            doc1.putAttribute("title", "Former Sal. Oppenheim senior bankers found guilty of fraud");
            doc1.putAttribute("publishingDate", "09 Jul 2015");
            doc1.putAttribute("classificationModelScore", "0.43");
            doc1.putAttribute("triggeredCountries", "[\"Costa Rica\",\"Macau\",\"Peru\"]");
            doc1.putAttribute("suspiciousPhrases", "[\"retail group Arcandor which later went bankrupt\"]");
            String originalDocumentLink1 = "https://42.tut.by/619932";
            doc1.setOriginalDocumentLink(originalDocumentLink1);
            String htmlExcerpt = ArticleExtractor.getInstance().getText(new URL(originalDocumentLink1));
            doc1.putAttribute("htmlExcerpt", htmlExcerpt);
            docs.add(doc1);
        } catch (Exception ex) {
        	logger.error("Error getting article from html document #1");
        }

        try {
            Document doc2 = new Document();
            doc2.setId(generateDocumentId());
            doc2.setTextLink("http://www.textlink-2.org");
            doc2.putAttribute("title", "Former Bertelsmann CEO sent directly to jail after embezzlement conviction");
            doc2.putAttribute("publishingDate", "14 Nov 2014");
            doc2.putAttribute("classificationModelScore", "0.14");
            doc2.putAttribute("triggeredCountries", "[\"Peru\",\"Macau\"]");
            doc2.putAttribute("suspiciousPhrases", "[\"Misappropriate-ed company funds\"]");
            String originalDocumentLink2 = "https://42.tut.by/619762";
            doc2.setOriginalDocumentLink(originalDocumentLink2);
            String htmlExcerpt = ArticleExtractor.getInstance().getText(new URL(originalDocumentLink2));
            doc2.putAttribute("htmlExcerpt", htmlExcerpt);
            docs.add(doc2);
        } catch (Exception ex) {
        	logger.error("Error getting article from html document #2");
        }

        try {
            Document doc3 = new Document();
            doc3.setId(generateDocumentId());
            doc3.setTextLink("http://www.textlink-3.org");
            doc3.putAttribute("title", "Title 3");
            doc3.putAttribute("publishingDate", "10 Jan 2015");
            doc3.putAttribute("classificationModelScore", "0.29");
            doc3.putAttribute("triggeredCountries", "[\"Botswana\",\"Zimbabwe\"]");
            doc3.putAttribute("suspiciousPhrases", "[\"Misappropriate-ed company funds\"]");
            doc3.setOriginalDocumentLink("https://42.tut.by/621501");
            docs.add(doc3);
        } catch (Exception ex) {
        	logger.error("Error getting article from html document #3");
        }
        */

        transaction.setDocs(docs);

        transactions.add(transaction);

        return transactions;
    }

    private String generateTransactionId() {
        return UUID.randomUUID().toString();
    }

    private String generateDocumentId() {
        return UUID.randomUUID().toString();
    }
}
