package org.odk.collect.android.views;
/*
 * 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.
 */

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;

/**
 * A Spinner that adjusts its size to fit its currently selected item rather than the largest item in its adapter.
 */
public class DynamicWidthSpinner extends AppCompatSpinner {

  /**
   * @param context
   *     the Context in which the View will be inflated.
   */
  public DynamicWidthSpinner(Context context) {
    super(context);
  }

  /**
   * @param context
   *     the Context in which the View will be inflated.
   * @param attrs
   *     XML defined attributes.
   */
  public DynamicWidthSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  /**
   * @param context
   *     the Context in which the View will be inflated.
   * @param attrs
   *     XML defined attributes.
   * @param defStyleAttr
   *     the style for the View.
   */
  public DynamicWidthSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  /**
   * @param adapter
   *     the adapter to display.
   */
  public void setAdapter(BaseAdapter adapter) {
    super.setAdapter(adapter != null ? new WrapperSpinnerAdapter(adapter) : null);
  }

  private final class WrapperSpinnerAdapter implements SpinnerAdapter {

    private final SpinnerAdapter mBaseAdapter;

    private WrapperSpinnerAdapter(SpinnerAdapter baseAdapter) {
      mBaseAdapter = baseAdapter;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      return mBaseAdapter.getView(getSelectedItemPosition(), convertView, parent);
    }

    public final SpinnerAdapter getBaseAdapter() {
      return mBaseAdapter;
    }

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

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
      return mBaseAdapter.getDropDownView(position, convertView, parent);
    }

    @Override
    public Object getItem(int position) {
      return mBaseAdapter.getItem(position);
    }

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

    @Override
    public int getItemViewType(int position) {
      return mBaseAdapter.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
      return mBaseAdapter.getViewTypeCount();
    }

    @Override
    public boolean hasStableIds() {
      return mBaseAdapter.hasStableIds();
    }

    @Override
    public boolean isEmpty() {
      return mBaseAdapter.isEmpty();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
      mBaseAdapter.registerDataSetObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
      mBaseAdapter.unregisterDataSetObserver(observer);
    }
  }
}
