package com.hellosign.sdk.resource.support;

/**
 * The MIT License (MIT)
 * 
 * Copyright (C) 2015 hellosign.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import org.json.JSONObject;

import com.hellosign.sdk.HelloSignException;
import com.hellosign.sdk.resource.support.types.ValidationType;

/**
 * This class represents a HelloSign Form Field object.
 * 
 * @author "Chris Paul (chris@hellosign.com)"
 */
public class FormField extends CustomField {

    private static final String FORM_FIELD_SIGNER = "signer";
    private static final String FORM_FIELD_PAGE = "page";
    private static final String FORM_FIELD_VALIDATION_TYPE = "validation_type";

    public FormField() {
        super();
    }

    public FormField(JSONObject json) throws HelloSignException {
        super(json);
    }

    /**
     * Returns the signer index for this form field.
     * 
     * @return Integer signer index or null if not set
     */
    public Integer getSigner() {
        return getInteger(FORM_FIELD_SIGNER);
    }

    /**
     * Set the signer index that should complete this form field.
     * 
     * @param signer Integer index
     */
    public void setSigner(Integer signer) {
        set(FORM_FIELD_SIGNER, signer);
    }

    /**
     * Set the page number for this component. The (x, y) coordinates will be
     * relative to the top left corner of that page.
     * 
     * @param page Integer page number
     */
    public void setPage(Integer page) {
        set(FORM_FIELD_PAGE, page);
    }

    /**
     * Returns the page number this component is on.
     * 
     * @return Integer page number, or null if not set
     */
    public Integer getPage() {
        return getInteger(FORM_FIELD_PAGE);
    }

    /**
     * Set the validation rule for this field. This will force the signer to
     * enter data that conforms to the validation rule.
     * 
     * @param type ValidationType
     */
    public void setValidationType(ValidationType type) {
        set(FORM_FIELD_VALIDATION_TYPE, type.toString());
    }

    /**
     * Return the validation rule being used for this field
     * 
     * @return ValidationType validation type, null if not set
     */
    public ValidationType getValidationType() {
        return ValidationType.valueOf(getString(FORM_FIELD_VALIDATION_TYPE));
    }

    /**
     * Return the validation rule being used for this field as a string
     * 
     * @return String validation type, null if not set
     */
    public String getValidationTypeString() {
        if (has(FORM_FIELD_VALIDATION_TYPE)) {
            return getValidationType().toString();
        }
        return null;
    }

    /**
     * Set this field as required.
     * 
     * @param isRequired boolean true if field is required by signer
     * @deprecated Use {@link #setIsRequired(Boolean)}
     */
    public void setRequired(boolean isRequired) {
        setIsRequired(isRequired);
    }
}

@@ -1262,16 +1262,75 @@
 eption;%0A
+import com.hellosign.sdk.resource.support.types.FieldType;%0A
 import c
@@ -1882,32 +1882,971 @@
 r(json);%0A    %7D%0A%0A
+    /**%0A     * Constructor to be used when creating a new FormField object.%0A     * %0A     * @param type FormField type%0A     * @param name String Field Label that will be displayed to the signer%0A     * @param signer int index of the signer that should complete this field (0-based index)%0A     * @param x int x coordinate location of this field in pixels%0A     * @param y int y coordinate location of this field in pixels%0A     * @param height int height of this field in pixels%0A     * @param width int height of this field in pixels%0A     * @param page int page number of the document in which this field will be placed (1-based index)%0A     */%0A    public FormField(FieldType type, String name, int signer, int x, int y, int height, int width, int page) %7B%0A        setType(type);%0A        setName(name);%0A        setSigner(signer);%0A        setX(x);%0A        setY(y);%0A        setHeight(height);%0A        setWidth(width);%0A        setPage(page);%0A    %7D%0A%0A
     /**%0A     * R

Add convenience constructor.