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.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.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 CIRCLE = "Circle";
  private static final String ELLIPSE = "Ellipse";
  private static final String LINE = "Line";

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

  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)));
    items.add(new DrawModeItem(ERASER, context.getString(R.string.draw_mode_eraser)));
    items.add(new DrawModeItem(TEXT, context.getString(R.string.draw_mode_text)));
    items.add(new DrawModeItem(ARROW, context.getString(R.string.draw_mode_arrow)));
    items.add(new DrawModeItem(RECTANGLE, context.getString(R.string.draw_mode_rectangle)));
    items.add(new DrawModeItem(CIRCLE, context.getString(R.string.draw_mode_circle)));
    items.add(new DrawModeItem(ELLIPSE, context.getString(R.string.draw_mode_ellipse)));
    items.add(new DrawModeItem(LINE, "Line"));
  }

  @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();
    }
    holder.textView.setText(getItem(position).getLabel());
    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);
        break;
      case ERASER:
        drawView.setDrawingMode(DrawingMode.ERASER);
        drawView.setDrawingTool(DrawingTool.LINE);
        break;
      case TEXT:
        drawView.setDrawingMode(DrawingMode.TEXT);
        drawView.setDrawingTool(DrawingTool.LINE);
        break;
      case ARROW:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.ARROW);
        break;
      case RECTANGLE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.RECTANGLE);
        break;
      case CIRCLE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.CIRCLE);
        break;
      case ELLIPSE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.ELLIPSE);
        break;
      case LINE:
        drawView.setDrawingMode(DrawingMode.DRAW);
        drawView.setDrawingTool(DrawingTool.LINE);
        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 TextView textView;

    ViewHolder(final View view) {
      textView = (TextView) view;
    }
  }
}
