package io.lumify.themoviedb;

import io.lumify.core.exception.LumifyException;
import io.lumify.core.mapreduce.LumifyElementMapperBase;
import io.lumify.core.model.properties.LumifyProperties;
import org.apache.hadoop.io.BytesWritable;
import org.securegraph.Vertex;
import org.securegraph.VertexBuilder;
import org.securegraph.Visibility;
import org.securegraph.accumulo.AccumuloAuthorizations;
import org.securegraph.property.StreamingPropertyValue;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ImportImgMRMapper extends LumifyElementMapperBase<SequenceFileKey, BytesWritable> {
    public static final String MULTI_VALUE_KEY = ImportJsonMR.class.getName();
    public static final String SOURCE = "TheMovieDb.org";
    private Visibility visibility;
    private AccumuloAuthorizations authorizations;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
        this.visibility = new Visibility("");
        this.authorizations = new AccumuloAuthorizations();
    }

    @Override
    protected void safeMap(SequenceFileKey key, BytesWritable value, Context context) throws Exception {
        String conceptType;
        String sourceVertexId;
        String edgeLabel;

        context.setStatus(key.getRecordType() + ":" + key.getId());

        switch (key.getRecordType()) {
            case PERSON:
                conceptType = TheMovieDbOntology.CONCEPT_TYPE_PROFILE_IMAGE;
                edgeLabel = TheMovieDbOntology.EDGE_LABEL_HAS_PROFILE_IMAGE;
                sourceVertexId = TheMovieDbOntology.getPersonVertexId(key.getId());
                break;
            case MOVIE:
                conceptType = TheMovieDbOntology.CONCEPT_TYPE_POSTER_IMAGE;
                edgeLabel = TheMovieDbOntology.EDGE_LABEL_HAS_POSTER_IMAGE;
                sourceVertexId = TheMovieDbOntology.getMovieVertexId(key.getId());
                break;
            case PRODUCTION_COMPANY:
                conceptType = TheMovieDbOntology.CONCEPT_TYPE_LOGO;
                edgeLabel = TheMovieDbOntology.EDGE_LABEL_HAS_LOGO;
                sourceVertexId = TheMovieDbOntology.getProductionCompanyVertexId(key.getId());
                break;
            default:
                throw new LumifyException("Invalid record type: " + key.getRecordType());
        }

        String edgeId = TheMovieDbOntology.getHasImageEdgeId(key.getId(), key.getImagePath());
        String title = key.getTitle();
        String vertexId = TheMovieDbOntology.getImageVertexId(key.getImagePath());
        VertexBuilder m = prepareVertex(vertexId, visibility);
        LumifyProperties.CONCEPT_TYPE.addPropertyValue(m, MULTI_VALUE_KEY, conceptType, visibility);
        LumifyProperties.SOURCE.addPropertyValue(m, MULTI_VALUE_KEY, SOURCE, visibility);
        StreamingPropertyValue rawValue = new StreamingPropertyValue(new ByteArrayInputStream(value.getBytes()), byte[].class);
        rawValue.store(true);
        rawValue.searchIndex(false);
        LumifyProperties.RAW.addPropertyValue(m, MULTI_VALUE_KEY, rawValue, visibility);
        LumifyProperties.TITLE.addPropertyValue(m, MULTI_VALUE_KEY, "Image of " + title, visibility);
        Vertex profileImageVertex = m.save(authorizations);

        Vertex sourceVertex = prepareVertex(sourceVertexId, visibility).save(authorizations);

        addEdge(edgeId, sourceVertex, profileImageVertex, edgeLabel, visibility, authorizations);
        LumifyProperties.ENTITY_HAS_IMAGE_VERTEX_ID.addPropertyValue(sourceVertex, MULTI_VALUE_KEY, profileImageVertex.getId(), visibility, authorizations);

        context.getCounter(TheMovieDbImportCounters.IMAGES_PROCESSED).increment(1);
    }
}

@@ -3302,16 +3302,23 @@
   Vertex
+Builder
  sourceV
@@ -3322,16 +3322,24 @@
 ceVertex
+Mutation
  = prepa
@@ -3378,128 +3378,8 @@
 ity)
-.save(authorizations);%0A%0A        addEdge(edgeId, sourceVertex, profileImageVertex, edgeLabel, visibility, authorizations)
 ;%0A  
@@ -3457,16 +3457,24 @@
 ceVertex
+Mutation
 , MULTI_
@@ -3511,16 +3511,172 @@
 getId(),
+ visibility);%0A        Vertex sourceVertex = sourceVertexMutation.save(authorizations);%0A%0A        addEdge(edgeId, sourceVertex, profileImageVertex, edgeLabel,
  visibil

remove direct interaction with graph in mr job