/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.prelert.job.results;

import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcherSupplier;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Influence field name and list of influence field values/score pairs
 */
public class Influence extends ToXContentToBytes implements Writeable
{
    /**
     * Note all publicly exposed field names are "influencer" not "influence"
     */
    public static final ParseField INFLUENCER = new ParseField("influencer");
    public static final ParseField INFLUENCER_FIELD_NAME = new ParseField("influencerFieldName");
    public static final ParseField INFLUENCER_FIELD_VALUES = new ParseField("influencerFieldValues");

    @SuppressWarnings("unchecked")
    public static final ConstructingObjectParser<Influence, ParseFieldMatcherSupplier> PARSER = new ConstructingObjectParser<>(
            INFLUENCER.getPreferredName(), a -> new Influence((String) a[0], (List<String>) a[1]));

    static {
        PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_NAME);
        PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_VALUES);
    }

    private String field;
    private List<String> fieldValues;

    public Influence(String field, List<String> fieldValues)
    {
        this.field = field;
        this.fieldValues = fieldValues;
    }

    public Influence(StreamInput in) throws IOException {
        this.field = in.readString();
        this.fieldValues = Arrays.asList(in.readStringArray());
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeString(field);
        out.writeStringArray(fieldValues.toArray(new String[fieldValues.size()]));
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject();
        builder.field(INFLUENCER_FIELD_NAME.getPreferredName(), field);
        builder.field(INFLUENCER_FIELD_VALUES.getPreferredName(), fieldValues);
        builder.endObject();
        return builder;
    }

    public String getInfluencerFieldName()
    {
        return field;
    }

    public List<String> getInfluencerFieldValues()
    {
        return fieldValues;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(field, fieldValues);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }

        if (obj == null)
        {
            return false;
        }

        if (getClass() != obj.getClass())
        {
            return false;
        }

        Influence other = (Influence) obj;

        return Objects.equals(field, other.field) &&
                Objects.equals(fieldValues, other.fieldValues);
    }
}

@@ -995,10 +995,11 @@
 able
+ %7B
 %0A
-%7B
 %0A   
@@ -1976,20 +1976,16 @@
 dValues)
-%0A   
  %7B%0A     
@@ -2818,20 +2818,16 @@
 ldName()
-%0A   
  %7B%0A     
@@ -2900,20 +2900,16 @@
 Values()
-%0A   
  %7B%0A     
@@ -2977,20 +2977,16 @@
 shCode()
-%0A   
  %7B%0A     
@@ -3087,20 +3087,16 @@
 ect obj)
-%0A   
  %7B%0A     
@@ -3114,24 +3114,16 @@
  == obj)
-%0A       
  %7B%0A     
@@ -3177,24 +3177,16 @@
 == null)
-%0A       
  %7B%0A     
@@ -3258,24 +3258,16 @@
 Class())
-%0A       
  %7B%0A     
@@ -3341,17 +3341,16 @@
 e) obj;%0A
-%0A
         
@@ -3397,24 +3397,8 @@
 ) &&
-%0A               
  Obj

Write record influencers as they are expected in java (elastic/elasticsearch#379)