/*
 * Copyright (c) 2015 Diona Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Diona
 * ("Confidential Information"). You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement you
 * entered into with Diona.
 */
package org.odk.collect.android.views;

import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;

import org.odk.collect.android.R;
import org.odk.collect.android.widgets.QuestionWidget;

/**
 * Builds a {@link Button} with correct formatting for forms.
 */
public class ButtonBuilder {

  private boolean readOnly;
  private ViewGroup parent;
  private String text;
  private Context context;

  /**
   * @param context
   *     the Context in which the Button will be inflated.
   */
  public ButtonBuilder(final Context context) {
    this.context = context;
  }

  /**
   * @param readOnly
   *     {@code true} if the Button is read only, {@code false} if it is not.
   *
   * @return an instance of the builder.
   */
  public ButtonBuilder readOnly(final boolean readOnly) {
    this.readOnly = readOnly;
    return this;
  }

  /**
   * @param parent
   *     the parent ViewGroup in which the Button will be inflated.
   *
   * @return an instance of the builder.
   */
  public ButtonBuilder withParent(final ViewGroup parent) {
    this.parent = parent;
    return this;
  }

  /**
   * @param text
   *     the text to display.
   *
   * @return an instance of the builder.
   */
  public ButtonBuilder withText(final String text) {
    this.text = text;
    return this;
  }

  /**
   * @param textRes
   *     the String resource for the text to display.
   *
   * @return an instance of the builder.
   */
  public ButtonBuilder withTextResource(final @StringRes int textRes) {
    text = context.getString(textRes);
    return this;
  }

  /**
   * @return a Button that corresponds to the specified settings.
   */
  public Button build() {
    final Button button = (Button) LayoutInflater.from(context).inflate(R.layout.borderless_button, parent, false);
    button.setId(QuestionWidget.newUniqueId());
    button.setText(text);
    button.setTextColor(ContextCompat.getColor(context, R.color.accent));
    button.setEnabled(!readOnly);
    return button;
  }
}
