/*
 * Copyright (C) 2009 University of Washington
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.odk.collect.android.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.odk.collect.android.R;
import org.odk.collect.android.logic.HierarchyElement;

public class HierarchyElementView extends RelativeLayout {

  public static final int QUESTION_TEXT_ID = 2;
  private TextView mPrimaryTextView;
  private TextView mSecondaryTextView;
  private ImageView mIcon;


  public HierarchyElementView(Context context, HierarchyElement it) {
    super(context);

    setColor(it.getColor());

    mIcon = new ImageView(context);
    mIcon.setImageDrawable(it.getIcon());
    mIcon.setId(1);
    mIcon.setPadding(0, 0, dipToPx(4), 0);

    final Resources resources = context.getResources();

    final LayoutParams iconParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    iconParams.addRule(RelativeLayout.BELOW, QUESTION_TEXT_ID);
    addView(mIcon, iconParams);

    mPrimaryTextView = new TextView(context);
    mPrimaryTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.question_text));
    setPrimaryText(it.getPrimaryText());
    mPrimaryTextView.setId(QUESTION_TEXT_ID);
    mPrimaryTextView.setGravity(Gravity.CENTER_VERTICAL);
    LayoutParams l =
        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    addView(mPrimaryTextView, l);

    mSecondaryTextView = new TextView(context);
    setSecondaryText(it.getSecondaryText());
    mSecondaryTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.answer_text));
    mSecondaryTextView.setGravity(Gravity.CENTER_VERTICAL);

    LayoutParams lp =
        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.BELOW, mPrimaryTextView.getId());
    lp.addRule(RelativeLayout.END_OF, mIcon.getId());
    addView(mSecondaryTextView, lp);

    setPadding(dipToPx(8), dipToPx(4), dipToPx(8), dipToPx(8));

  }

  /**
   * @param text
   *     the primary text.
   */
  public void setPrimaryText(String text) {
    final Spanned spannedPrimaryText = text == null ? new SpannableString("") : Html.fromHtml(text);
    mPrimaryTextView.setText(spannedPrimaryText.toString());
  }


  /**
   * @param text
   *     the secondary text.
   */
  public void setSecondaryText(String text) {
    final Spanned spannedSecondaryText = text == null ? new SpannableString("") : Html.fromHtml(text);
    mSecondaryTextView.setText(spannedSecondaryText.toString());
  }

  /**
   * @param icon
   *     the drawable to be displayed as the icon.
   */
  public void setIcon(Drawable icon) {
    mIcon.setImageDrawable(icon);
  }

  /**
   * @param color
   *     the colour to be displayed in the background of the row.
   */
  public void setColor(int color) {
    setBackgroundColor(color);
  }

  /**
   * @param bool
   *     {@code true} if we should show the secondary text, {@code false} if we should hide it.
   */
  public void showSecondary(boolean bool) {
    if (bool) {
      mSecondaryTextView.setVisibility(VISIBLE);
      setMinimumHeight(dipToPx(64));

    } else {
      mSecondaryTextView.setVisibility(GONE);
      setMinimumHeight(dipToPx(32));

    }
  }

  /**
   * @param dip
   *     the density pixels to convert.
   *
   * @return the density pixels converted to regular pixels for this device.
   */
  public int dipToPx(int dip) {
    return (int) (dip * getResources().getDisplayMetrics().density + 0.5f);
  }

}
