package org.odk.collect.android.adapters;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import org.odk.collect.android.R;


/*
 * 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.
 */

/**
 * An adapter for displaying different text sizes.
 */
public class TextSizeAdapter extends BaseAdapter {

  private static final int DEFAULT_TEXT_SIZE = 14;
  private final Context context;
  private int selectedPosition;
  private final int[] sizes = {12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};

  /**
   * @param context
   *     the current Context.
   */
  public TextSizeAdapter(final Context context) {
    this.context = context;
  }

  @Override
  public int getCount() {
    return sizes.length;
  }

  @Override
  public Integer getItem(final int position) {
    return sizes[position];
  }

  @Override
  public long getItemId(final int position) {
    return position;
  }

  @Override
  public View getView(final int position, View convertView, final ViewGroup parent) {
    convertView = createView(position, convertView, parent, true);
    convertView.setBackgroundColor(Color.TRANSPARENT);
    return convertView;
  }

  @Override
  public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
    convertView = createView(position, convertView, parent, false);
    if (selectedPosition == position) {
      convertView.setBackgroundColor(Color.LTGRAY);
    } else {
      convertView.setBackgroundColor(Color.WHITE);
    }
    return convertView;
  }

  @NonNull
  private View createView(final int position, View convertView, final ViewGroup parent, final boolean defaultSize) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.row_text_size, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    final String textSize = Integer.toString(getItem(position));
    holder.textView.setText(textSize);
    if (defaultSize) {
      holder.textView.setTextSize(DEFAULT_TEXT_SIZE);
    } else {
      holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Float.parseFloat(textSize));
    }
    return convertView;
  }

  /**
   * @param textSize
   *     the text size to be selected.
   * @param spinner
   *     the Spinner containing the adapter.
   */
  public void setSelectedItem(final int textSize, final Spinner spinner) {
    final int count = getCount();
    for (int i = 0; i < count; i++) {
      final int tempSize = getItem(i);
      if (tempSize == textSize) {
        spinner.setSelection(i);
        break;
      }
    }
  }

  /**
   * @param selectedPosition
   *     the position to be marked as selected.
   */
  public void setSelectedPosition(final int selectedPosition) {
    this.selectedPosition = selectedPosition;
  }

  private class ViewHolder {
    private final TextView textView;

    private ViewHolder(final View view) {
      this.textView = (TextView) view.findViewById(R.id.row_text_size_text);
    }
  }
}
