package eu.dnetlib.repo.manager.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.domain.data.RepositoryInterface;
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
import eu.dnetlib.repo.manager.shared.AggregationDetails;
import eu.dnetlib.repo.manager.shared.Timezone;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Converter {

    private static final Logger LOGGER = Logger.getLogger(Converter.class);

    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {

        Repository repository = new Repository();
        
        JSONObject datasource = repositoryObject.getJSONObject("datasource");

        //if( datasource.equals(null))
        //    return null;

        repository.setId(datasource.get("id").toString());
        repository.setOfficialName(datasource.get("officialname").toString());

        repository.setEnglishName( datasource.get("englishname").toString());
        if(repository.getEnglishName().equals("null"))
            repository.setEnglishName("");

        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
        if(repository.getWebsiteUrl().equals("null"))
            repository.setWebsiteUrl("");

        repository.setLogoUrl(datasource.get("logourl").toString());
        if(repository.getLogoUrl().equals("null"))
            repository.setLogoUrl("");

        repository.setContactEmail(datasource.get("contactemail").toString());
        if(repository.getContactEmail().equals("null"))
            repository.setContactEmail("");


        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
        Double timezone = toDouble(datasource.get("timezone").toString());
        repository.setTimezone(timezone!=null?timezone:0.0);
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
        repository.setOdLanguages(datasource.get("languages").toString());
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));

        /*  typology -> platform
         *  datasource class -> typology */
        repository.setTypology(datasource.get("platform").toString());
        if(repository.getTypology().equals("null"))
            repository.setTypology("");
        repository.setDatasourceClass(datasource.get("typology").toString());

        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
        repository.setActivationId(datasource.get("activationId").toString());

        repository.setDescription(datasource.get("description").toString());
        if(repository.getDescription().equals("null"))
            repository.setDescription("");

        repository.setIssn(datasource.get("issn").toString());
        repository.setLissn(datasource.get("lissn").toString());
        if(repository.getLissn().equals("null"))
            repository.setLissn("");
        repository.setEissn(datasource.get("eissn").toString());
        if(repository.getEissn().equals("null"))
            repository.setEissn("");
        repository.setRegisteredBy(datasource.get("registeredby").toString());

        /* managed field */
        repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString()));

        //subjects

        repository.setAggregator(datasource.get("aggregator").toString());
        repository.setCollectedFrom(datasource.get("collectedfrom").toString());

        //TODO change organization to list
        JSONArray organizations = ((JSONArray)datasource.get("organizations"));
        if(organizations.length() != 0) {
            repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
            String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString();
            repository.setCountryCode(countryCode);
        }

        /* identities field  */

        return repository;
    }

    public static Date convertStringToDate(String date){

        if(Objects.equals(date, "null"))
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convertDateToString(Date date){

        if(Objects.equals(date, null))
            return null;

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static Double toDouble(String number){
        if(Objects.equals(number, "null"))
            return 0.0;
        else
            return Double.valueOf(number);
    }

    public static List<RepositorySnippet> jsonToRepositorySnippetList(JSONObject json) throws JSONException {

        List<RepositorySnippet> resultSet = new ArrayList<>();
        JSONArray rs = json.getJSONArray("datasourceInfo");
        for(int i=0;i<rs.length();i++)
            resultSet.add(jsonToRepositorySnippetObject( rs.getJSONObject(i)) );
        return resultSet;
    }

    private static RepositorySnippet jsonToRepositorySnippetObject(JSONObject repositorySnippetObject) throws JSONException {


        RepositorySnippet repositorySnippet = new RepositorySnippet();

//        JSONObject datasource = repositorySnippetObject.getJSONObject("datasource");


        repositorySnippet.setId(repositorySnippetObject.get("id").toString());
        repositorySnippet.setOfficialname(repositorySnippetObject.get("officialname").toString());

        repositorySnippet.setEnglishname( repositorySnippetObject.get("englishname").toString());
        if(repositorySnippet.getEnglishname().equals("null"))
            repositorySnippet.setEnglishname("");

        repositorySnippet.setWebsiteurl(repositorySnippetObject.get("websiteurl").toString());
        if(repositorySnippet.getWebsiteurl().equals("null"))
            repositorySnippet.setWebsiteurl("");

        repositorySnippet.setRegisteredby(repositorySnippetObject.get("registeredby").toString());
        if(repositorySnippet.getRegisteredby().equals("null"))
            repositorySnippet.setRegisteredby("");
        return repositorySnippet;

    }

    public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {

        List<Repository> resultSet = new ArrayList<>();
        JSONArray rs = json.getJSONArray("datasourceInfo");
        for(int i=0;i<rs.length();i++)
            resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
        return resultSet;
    }

    public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONObject json) throws JSONException {

        List<RepositoryInterface> resultSet = new ArrayList<>();
        JSONArray rs = json.getJSONArray("api");
        for(int i=0;i<rs.length();i++)
            resultSet.add(jsonToRepositoryInterfaceObject( rs.getJSONObject(i)) );
        return resultSet;
    }

    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {

        RepositoryInterface repositoryInterface = new RepositoryInterface();

        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
        repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
        repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString());

        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));


       // repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
        //repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));


        Map<String, String> accessParams = new HashMap<>();
        Map<String, String> extraFields = new HashMap<>();

        ObjectMapper mapper = new ObjectMapper();
        JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiParams");

        for(int i=0;i<apiparams.length();i++)
            accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value"));

        repositoryInterface.setAccessParams(accessParams);

        return repositoryInterface;
    }

    public static String repositoryObjectToJson(Repository repository) throws JSONException, JsonProcessingException {

        HashMap<String,Object> repositoryMap = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();

        repositoryMap.put("id",repository.getId());
        repositoryMap.put("openaireId",getOpenaireId(repository.getId()));
        repositoryMap.put("officialname",repository.getOfficialName());
        repositoryMap.put("englishname",repository.getEnglishName());
        repositoryMap.put("websiteurl",repository.getWebsiteUrl());
        repositoryMap.put("logourl",repository.getLogoUrl());
        repositoryMap.put("contactemail",repository.getContactEmail());
        repositoryMap.put("longitude",repository.getLongitude().toString());
        repositoryMap.put("latitude",repository.getLatitude().toString());
        repositoryMap.put("timezone",repository.getTimezone());

        repositoryMap.put("namespaceprefix",repository.getNamespacePrefix()!=null?repository.getNamespacePrefix():"");
        repositoryMap.put("languages",repository.getOdLanguages()!=null?repository.getOdLanguages():"");

        repositoryMap.put("dateofcollection",repository.getDateOfCollection()!=null?convertDateToString(repository.getDateOfCollection()):"");

        /*
        * typology -> platform
        * datasource class -> typology
        * */
        repositoryMap.put("typology",repository.getDatasourceClass());
        repositoryMap.put("platform",repository.getTypology());

        repositoryMap.put("dateofvalidation",repository.getDateOfCollection()!=null?convertDateToString(repository.getDateOfCollection()):"");
        repositoryMap.put("activationId",repository.getActivationId()!=null?repository.getActivationId():"");

        repositoryMap.put("description",repository.getDescription());

        repositoryMap.put("eissn",repository.getEissn()!=null?repository.getEissn():"");
        repositoryMap.put("issn",repository.getIssn()!=null?repository.getIssn():"");
        repositoryMap.put("lissn",repository.getLissn()!=null?repository.getLissn():"");

        repositoryMap.put("registeredby",repository.getRegisteredBy());

        repositoryMap.put("aggregator",repository.getAggregator()!=null?repository.getAggregator():"");
        repositoryMap.put("collectedfrom",repository.getCollectedFrom()!=null?repository.getCollectedFrom():"");

        repositoryMap.put("managed",repository.isRegistered());

        Map<String,String> organization = new HashMap<>();
        organization.put("legalname",repository.getOrganization());
        organization.put("country",repository.getCountryCode());
        organization.put("legalshortname","");
        organization.put("websiteurl","");
        organization.put("logourl","");

        List organizations = new ArrayList();
        organizations.add(organization);
        repositoryMap.put("organizations",organizations);

        //TODO check identitites
        //Map<String,String> identity = new HashMap<>();
        List identities = new ArrayList();
//        identities.add(identities);
        repositoryMap.put("identities",identities);
        repositoryMap.put("subjects","");

        //TODO check fields
       /* jsonObject.put("certificates",repository.getCertificates());
        jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
        jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
        jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
        jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
        jsonObject.put("datauploadtype",repository.getDataUploadType());
        jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
        jsonObject.put("od_contenttypes",repository.getOdContentTypes());
        jsonObject.put("officialname",repository.getOfficialname());
        jsonObject.put("pidsystems",repository.getPidSystems());
        jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
        jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
        jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
        jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
        jsonObject.put("serviceprovider",repository.getServiceProvider());
        jsonObject.put("versioning",repository.getVersioning());
        //datasource.get("platform");
        //datasource.get("subjects");*/

        return mapper.writeValueAsString(repositoryMap);
    }

    public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",repositoryInterface.getId());
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
        jsonObject.put("datasource",repository.getId());
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
        jsonObject.put("typology",repositoryInterface.getTypology());
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
        jsonObject.put("compatibilityOverride",repositoryInterface.getDesiredCompatibilityLevel());

        jsonObject.put("lastCollectionTotal","");

        jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
        jsonObject.put("lastAggregationTotal","");
        jsonObject.put("lastAggregationDate","");
        jsonObject.put("lastDownloadTotal","");
        jsonObject.put("lastDownloadDate","");

        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
        jsonObject.put("removable",repositoryInterface.isRemovable());

        
        JSONArray apiparams = new JSONArray();
        for(String param: repositoryInterface.getAccessParams().keySet()){
            JSONObject jo = new JSONObject();
            jo.put("param",param);
            jo.put("value",repositoryInterface.getAccessParams().get(param));
            apiparams.put(jo);
        }
        jsonObject.put("apiParams",apiparams);


//        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());


        return jsonObject.toString();
    }

    public static ArrayList<String> readFile(String filename) {
        String line;
        ArrayList<String> list = new ArrayList<String>();
        try {
            //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
            InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            while((line = br.readLine()) != null) {
                list.add(line.trim());
            }
            br.close();
        } catch (IOException e) {
            LOGGER.debug("Error opening file!");
            e.printStackTrace();
        }
        return list;
    }

    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject datasourceInfo) throws JSONException {
        JSONArray rs;
        List<AggregationDetails> aggregationDetailsList = new ArrayList<>();

        if (datasourceInfo.get("aggregationHistory") != null && !datasourceInfo.get("aggregationHistory").toString().equals("null")) {
            rs = new JSONArray(datasourceInfo.get("aggregationHistory").toString());

            for (int i = 0; i < rs.length(); i++)
                aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
        }

        return aggregationDetailsList;
    }

    private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {

        AggregationDetails aggregationDetails = new AggregationDetails();

        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
        if(aggregationObject.has("collectionMode"))
            aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
        if(aggregationObject.has("indexedVersion"))
            aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString()));
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
        return aggregationDetails;
    }

    public static AggregationDetails getLastCollectionFromJson(JSONObject datasourceInfo) throws JSONException {

        if( datasourceInfo.get("lastCollection").equals(null))
            return null;

        return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastCollection"));
    }

    public static AggregationDetails getLastTransformationFromJson(JSONObject datasourceInfo) throws JSONException {

        if( datasourceInfo.get("lastTransformation").equals(null))
            return null;

        return jsonToAggregationDetails(datasourceInfo.getJSONObject("lastTransformation"));
    }

    public static List<Timezone> toTimezones(List<String> timezones) {

        List<Timezone> tmz = new ArrayList<>();
        for(String t : timezones){
            String[] s = t.split("\t");
            tmz.add(new Timezone(s[1],Double.parseDouble(s[0])));
        }
        return tmz;
    }

    public static String getOpenaireId(String repositoryId) {
        if (repositoryId != null && repositoryId.contains("::"))
            return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
        return null;
    }

}
