/*
 * Copyright (C) 2007-2017, GoodData(R) Corporation. All rights reserved.
 * This source code is licensed under the BSD-style license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */
package com.gooddata.executeafm.response;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.gooddata.executeafm.afm.Afm;
import com.gooddata.executeafm.afm.LocallyIdentifiable;
import com.gooddata.util.GoodDataToStringBuilder;

import java.util.List;

import static com.gooddata.util.Validate.notEmpty;
import static com.gooddata.util.Validate.notNull;

/**
 * Header of an attribute.
 */
@JsonRootName(AttributeHeader.NAME)
public class AttributeHeader implements Header, LocallyIdentifiable {

    static final String NAME = "attributeHeader";

    private final String name;
    private final String localIdentifier;
    private final String uri;
    private final String identifier;
    private final AttributeInHeader formOf;

    private List<TotalHeaderItem> totalItems;

    /**
     * Creates new header
     * @deprecated use the constructor with {@link AttributeInHeader}
     *
     * @param name name
     * @param localIdentifier local identifier
     * @param uri uri
     * @param identifier identifier
     */
    @Deprecated
    public AttributeHeader(final String name, final String localIdentifier, final String uri, final String identifier) {
        this.name = notEmpty(name, "name");
        this.localIdentifier = notEmpty(localIdentifier, "localIdentifier");
        this.uri = notEmpty(uri, "uri");
        this.identifier = notEmpty(identifier, "identifier");
        this.formOf = null;
    }

    /**
     * Creates new header
     * @param name name
     * @param localIdentifier local identifier
     * @param uri uri
     * @param identifier identifier
     * @param formOf info about attribute which this header's display form is form of
     */
    public AttributeHeader(final String name, final String localIdentifier, final String uri, final String identifier, final AttributeInHeader formOf) {
        this(name, localIdentifier, uri, identifier, formOf, null);
    }

    /**
     * Creates new header
     * @param name name
     * @param localIdentifier local identifier
     * @param uri uri
     * @param identifier identifier
     * @param formOf info about attribute which this header's display form is form of
     * @param totalHeaderItems total header items
     */
    @JsonCreator
    public AttributeHeader(@JsonProperty("name") final String name,
                           @JsonProperty("localIdentifier") final String localIdentifier,
                           @JsonProperty("uri") final String uri,
                           @JsonProperty("identifier") final String identifier,
                           @JsonProperty("formOf") final AttributeInHeader formOf,
                           @JsonProperty("totalItems") final List<TotalHeaderItem> totalHeaderItems) {
        this.name = notEmpty(name, "name");
        this.localIdentifier = notEmpty(localIdentifier, "localIdentifier");
        this.uri = notEmpty(uri, "uri");
        this.identifier = notEmpty(identifier, "identifier");
        this.formOf = notNull(formOf, "formOf");
        this.totalItems = totalHeaderItems;
    }

    /**
     * Header name, an attribute's display form title, or specified alias.
     * @return header name
     */
    public String getName() {
        return name;
    }

    /**
     * Local identifier referencing header's {@link com.gooddata.executeafm.afm.AttributeItem}
     *  within {@link Afm}
     * @return attribute's local identifier
     */
    @Override
    public String getLocalIdentifier() {
        return localIdentifier;
    }

    /**
     * Uri of attribute's display form
     * @return uri
     */
    public String getUri() {
        return uri;
    }

    /**
     * Metadata identifier of attribute's display form
     * @return identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    public AttributeInHeader getFormOf() {
        return formOf;
    }

    /**
     * Totals' headers belonging to the same level as this header.
     * @return lists of totals' header
     */
    public List<TotalHeaderItem> getTotalItems() {
        return totalItems;
    }

    @Override
    public String toString() {
        return GoodDataToStringBuilder.defaultToString(this);
    }

}

@@ -1748,32 +1748,924 @@
  = null;%0A    %7D%0A%0A
+    /**%0A     * Creates new header%0A     * @deprecated use the constructor with %7B@link AttributeInHeader%7D%0A     *%0A     * @param name name%0A     * @param localIdentifier local identifier%0A     * @param uri uri%0A     * @param identifier identifier%0A     * @param totalHeaderItems total header items%0A     */%0A    @Deprecated%0A    public AttributeHeader(final String name,%0A                           final String localIdentifier,%0A                           final String uri,%0A                           final String identifier,%0A                           final List%3CTotalHeaderItem%3E totalHeaderItems) %7B%0A        this.name = notEmpty(name, %22name%22);%0A        this.localIdentifier = notEmpty(localIdentifier, %22localIdentifier%22);%0A        this.uri = notEmpty(uri, %22uri%22);%0A        this.identifier = notEmpty(identifier, %22identifier%22);%0A        this.formOf = null;%0A        this.totalItems = totalHeaderItems;%0A    %7D%0A%0A
     /**%0A     * C

add forgotten backward compatible constructor to AttributeHeader