package org.odk.collect.android.adapters;
/*
 * 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.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

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

import java.util.ArrayList;
import java.util.List;

public class DrawModeAdapter extends BaseAdapter implements AdapterView.OnItemSelectedListener {

  private static final String PEN = "Pen";
  private static final String ERASER = "Eraser";
  private static final String TEXT = "Text";
  private static final String ARROW = "Arrow";
  private static final String RECTANGLE = "Rectangle";
  private static final String RECTANGLE_FILLED = "RectangleFilled";
  private static final String CIRCLE_FILLED = "CircleFilled";
  private static final String CIRCLE = "Circle";
  private static final String ELLIPSE = "Ellipse";
  private static final String ELLIPSE_FILLED = "EllipseFilled";
  private static final String LINE = "Line";

  private final List<DrawModeItem> items;
  private final Context context;
  private final DrawView drawView;
  private int selectedPosition = -1;

  /**
   * @param context
   *     the Context in which the adapter will be displayed.
   * @param drawView
   *     the DrawView the adapter controls.
   */
  public DrawModeAdapter(final Context context, final DrawView drawView) {
    this.context = context;
    this.drawView = drawView;
    items = new ArrayList<>();
    items.add(new DrawModeItem(PEN, context.getString(R.string.draw_mode_pen), 0));
    items.add(new DrawModeItem(ERASER, context.getString(R.string.draw_mode_eraser), 0));
    items.add(new DrawModeItem(TEXT, context.getString(R.string.draw_mode_text), 0));
    items.add(new DrawModeItem(ARROW, context.getString(R.string.draw_mode_arrow), 0));
    items.add(new DrawModeItem(RECTANGLE, context.getString(R.string.draw_mode_rectangle), 0));
    items.add(new DrawModeItem(RECTANGLE_FILLED, context.getString(R.string.draw_mode_rectangle_filled), 0));
    items.add(new DrawModeItem(CIRCLE, context.getString(R.string.draw_mode_circle), 0));
    items.add(new DrawModeItem(CIRCLE_FILLED, context.getString(R.string.draw_mode_circle_filled), 0));
    items.add(new DrawModeItem(ELLIPSE, context.getString(R.string.draw_mode_ellipse), 0));
    items.add(new DrawModeItem(ELLIPSE_FILLED, context.getString(R.string.draw_mode_ellipse_filled), 0));
    items.add(new DrawModeItem(LINE, context.getString(R.string.draw_mode_line), 0));
  }

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

  @Override
  public DrawModeItem getItem(final int position) {
    return items.get(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);
    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;
  }

  @NonNull
  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_mode, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    final DrawModeItem item = getItem(position);
    holder.textView.setText(item.getLabel());
    if (item.getImageResID() == 0) {
      holder.iconView.setVisibility(View.GONE);
    } else {
      holder.iconView.setImageResource(item.getImageResID());
      holder.iconView.setVisibility(View.VISIBLE);
    }
    return convertView;
  }

  @Override
  public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
    final DrawModeItem item = getItem(position);
    switch (item.getKey()) {
      case PEN:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.PEN);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case ERASER:
        drawView.setDrawingMode(DrawingMode.ERASER);
        drawView.setDrawingTool(DrawingTool.LINE);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case TEXT:
        drawView.setDrawingMode(DrawingMode.TEXT);
        drawView.setDrawingTool(DrawingTool.LINE);
        drawView.setPaintStyle(Paint.Style.FILL);
        break;
      case ARROW:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.ARROW);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case RECTANGLE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.RECTANGLE);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case CIRCLE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.CIRCLE);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case ELLIPSE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.ELLIPSE);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case LINE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.LINE);
        drawView.setPaintStyle(Paint.Style.STROKE);
        break;
      case ELLIPSE_FILLED:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.ELLIPSE);
        drawView.setPaintStyle(Paint.Style.FILL);
        break;
      case RECTANGLE_FILLED:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.RECTANGLE);
        drawView.setPaintStyle(Paint.Style.FILL);
        break;
      case CIRCLE_FILLED:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.CIRCLE);
        drawView.setPaintStyle(Paint.Style.FILL);
        break;
    }
    selectedPosition = position;
    notifyDataSetChanged();
  }

  public void setLastSelection(final Spinner spinner) {
    final DrawPreferences drawPreferences = new DrawPreferences(context);
    String modeKey = null;
    final DrawingMode drawingMode = drawPreferences.getDrawMode();
    final DrawingTool drawingTool = drawPreferences.getDrawTool();
    switch (drawingMode) {
      case ERASER:
        modeKey = ERASER;
        break;
      case TEXT:
        modeKey = TEXT;
        break;
      default:
        switch (drawingTool) {
          case PEN:
            modeKey = PEN;
            break;
          case LINE:
            modeKey = LINE;
            break;
          case ARROW:
            modeKey = ARROW;
            break;
          case RECTANGLE:
            modeKey = RECTANGLE;
            break;
          case CIRCLE:
            modeKey = CIRCLE;
            break;
          case ELLIPSE:
            modeKey = ELLIPSE;
            break;
        }
        break;
    }
    for (int i = 0; i < getCount(); i++) {
      final DrawModeItem item = getItem(i);
      if (item.getKey().equals(modeKey)) {
        spinner.setSelection(i);
        break;
      }
    }
  }

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

  }

  private class ViewHolder {
    private final TextView textView;
    private final ImageView iconView;

    ViewHolder(final View view) {
      textView = (TextView) view.findViewById(R.id.row_draw_mode_text);
      iconView = (ImageView) view.findViewById(R.id.row_draw_mode_icon);
    }
  }
}
