// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.document.CollectionDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.MapDataType;
import com.yahoo.document.ReferenceDataType;
import com.yahoo.document.datatypes.*;
import com.yahoo.vespa.documentmodel.SummaryTransform;

/**
 * A summary field derived from a search definition
 *
 * @author  bratseth
 */
public class SummaryClassField {

    private final String name;

    private final Type type;

    /** The summary field type enumeration */
    public enum Type {

        BOOL("bool"),
        BYTE("byte"),
        SHORT("short"),
        INTEGER("integer"),
        INT64("int64"),
        FLOAT16("float16"),
        FLOAT("float"),
        DOUBLE("double"),
        STRING("string"),
        DATA("data"),
        LONGSTRING("longstring"),
        LONGDATA("longdata"),
        XMLSTRING("xmlstring"),
        FEATUREDATA("featuredata"),
        JSONSTRING("jsonstring"),
        TENSOR("tensor");

        private String name;

        Type(String name) {
            this.name = name;
        }

        /** Returns the name of this type */
        public String getName() {
            return name;
        }

        public String toString() {
            return "type: " + name;
        }
    }

    public SummaryClassField(String name, DataType type, SummaryTransform transform) {
        this.name = name;
        this.type = convertDataType(type, transform);
    }

    public String getName() { return name; }

    public Type getType() { return type; }

    /** Converts to the right summary field type from a field datatype and a transform*/
    public static Type convertDataType(DataType fieldType, SummaryTransform transform) {
        FieldValue fval = fieldType.createFieldValue();
        if (fval instanceof StringFieldValue) {
            if (transform != null && transform.equals(SummaryTransform.RANKFEATURES)) {
                return Type.FEATUREDATA;
            } else if (transform != null && transform.equals(SummaryTransform.SUMMARYFEATURES)) {
                return Type.FEATUREDATA;
            } else {
                return Type.LONGSTRING;
            }
        } else if (fval instanceof IntegerFieldValue) {
            return Type.INTEGER;
        } else if (fval instanceof LongFieldValue) {
            return Type.INT64;
        } else if (fval instanceof Float16FieldValue) {
            return Type.FLOAT16;
        } else if (fval instanceof FloatFieldValue) {
            return Type.FLOAT;
        } else if (fval instanceof DoubleFieldValue) {
            return Type.DOUBLE;
        } else if (fval instanceof BoolFieldValue) {
            return Type.BOOL;
        } else if (fval instanceof ByteFieldValue) {
            return Type.BYTE;
        } else if (fval instanceof Raw) {
            return Type.DATA;
        } else if (fval instanceof Struct) {
            return Type.JSONSTRING;
        } else if (fval instanceof PredicateFieldValue) {
            return Type.STRING;
        } else if (fval instanceof TensorFieldValue) {
            return Type.TENSOR;
        } else if (fieldType instanceof CollectionDataType) {
            if (transform != null && transform.equals(SummaryTransform.POSITIONS)) {
                return Type.XMLSTRING;
            } else {
                return Type.JSONSTRING;
            }
        } else if (fieldType instanceof MapDataType) {
            return Type.JSONSTRING;
        } else if (fieldType instanceof ReferenceDataType) {
            return Type.LONGSTRING;
        } else {
            throw new IllegalArgumentException("Don't know which summary type to convert " + fieldType + " to");
        }
    }

    public String toString() {
        return "summary class field " + name;
    }

}

@@ -359,17 +359,649 @@
 tatypes.
-*
+BoolFieldValue;%0Aimport com.yahoo.document.datatypes.ByteFieldValue;%0Aimport com.yahoo.document.datatypes.DoubleFieldValue;%0Aimport com.yahoo.document.datatypes.FieldValue;%0Aimport com.yahoo.document.datatypes.Float16FieldValue;%0Aimport com.yahoo.document.datatypes.FloatFieldValue;%0Aimport com.yahoo.document.datatypes.IntegerFieldValue;%0Aimport com.yahoo.document.datatypes.LongFieldValue;%0Aimport com.yahoo.document.datatypes.PredicateFieldValue;%0Aimport com.yahoo.document.datatypes.Raw;%0Aimport com.yahoo.document.datatypes.StringFieldValue;%0Aimport com.yahoo.document.datatypes.Struct;%0Aimport com.yahoo.document.datatypes.TensorFieldValue
 ;%0Aimport
@@ -1755,16 +1755,22 @@
 private 
+final 
 String n

Use explicit import and final members where you can.