package org.odk.collect.android.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
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 paint styles.
 */
public class PaintStyleAdapter extends BaseAdapter {

  private final Context context;
  private int selectedItemPosition = -1;

  private PaintStyleData[] data;

  /**
   * @param context
   *     the current Context.
   */
  public PaintStyleAdapter(final Context context) {
    this.context = context;
    data = new PaintStyleData[]{
        new PaintStyleData(Paint.Style.FILL, context.getString(R.string.draw_paint_style_fill)),
        new PaintStyleData(Paint.Style.STROKE, context.getString(R.string.draw_paint_style_stroke))
    };
  }

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

  @Override
  public PaintStyleData getItem(final int position) {
    return data[position];
  }

  /**
   * @param position
   *     the position of the selected style.
   *
   * @return the style at the specified position.
   */
  public Paint.Style getPaintStyle(final int position) {
    return getItem(position).style;
  }

  @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);
    convertView.setBackgroundColor(Color.TRANSPARENT);
    return convertView;
  }

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

  private View createView(final int position, View convertView, final ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.row_paint_style, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.textView.setText(getItem(position).label);
    return convertView;
  }

  /**
   * @param style
   *     the selected style.
   *
   * @return the selected position.
   */
  public int setSelectedStyle(final Paint.Style style) {
    final int count = getCount();
    for (int i = 0; i < count; i++) {
      if (getItem(i).style.equals(style)) {
        selectedItemPosition = i;
        break;
      }
    }
    notifyDataSetChanged();
    return selectedItemPosition;
  }

  private class ViewHolder {
    private final TextView textView;

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

  private class PaintStyleData {
    private Paint.Style style;
    private String label;

    private PaintStyleData(final Paint.Style style, final String label) {
      this.style = style;
      this.label = label;
    }
  }
}
