package org.odk.collect.android.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import org.odk.collect.android.R;
import org.odk.collect.android.utilities.DrawPreferences;
import org.odk.collect.android.views.drawing.DrawView;

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

public class DrawSizeAdapter extends BaseAdapter implements AdapterView.OnItemSelectedListener, DrawView
    .OnDrawViewListener {

  private static final int MAX_WIDTH = 15;

  private final Context context;
  private final DrawView drawView;
  private final DrawPreferences drawPreferences;
  private int drawColor;
  private int selectedPosition;

  public DrawSizeAdapter(final Context context, final DrawView drawView) {
    this.context = context;
    this.drawView = drawView;
    this.drawColor = drawView.getDrawColor();
    drawPreferences = new DrawPreferences(context);
    drawView.addOnDrawViewListener(this);
  }

  @Override
  public int getCount() {
    return MAX_WIDTH;
  }

  @Override
  public Integer getItem(final int position) {
    return position + 1;
  }

  @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 == selectedPosition) {
      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_draw_size, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.bindTo(getItem(position));
    return convertView;
  }

  @Override
  public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
    this.selectedPosition = position;
    drawPreferences.setDrawWidthPosition(position);
    if (drawView != null) {
      drawView.setDrawWidth(getItem(position));
    }
    notifyDataSetChanged();
  }

  @Override
  public void onNothingSelected(final AdapterView<?> parent) {

  }

  @Override
  public void onStartDrawing() {

  }

  @Override
  public void onEndDrawing() {

  }

  @Override
  public void onClearDrawing() {

  }

  @Override
  public void onRequestText() {

  }

  @Override
  public void onAllMovesPainted() {

  }

  @Override
  public void onDrawColorChanged(final int drawColor) {
    this.drawColor = drawColor;
    notifyDataSetChanged();
  }

  private class ViewHolder {
    private final DrawSizeRow drawSizeRow;

    ViewHolder(final View view) {
      drawSizeRow = (DrawSizeRow) view.findViewById(R.id.row_draw_size_line);
    }

    private void bindTo(final int size) {
      drawSizeRow.setStrokeWidth(size);
      drawSizeRow.setPaintColor(drawColor);
    }
  }
}
