/*
 * Copyright (c) 2016 Reto Inderbitzin (mail@indr.ch)
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

package ch.indr.threethreefive.ui.adapters;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashSet;
import java.util.List;

import ch.indr.threethreefive.R;
import ch.indr.threethreefive.libs.PageItem;
import ch.indr.threethreefive.libs.PreferencesType;
import ch.indr.threethreefive.ui.utils.OnTouchClickListener;

import static android.view.View.GONE;

/**
 * https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#defining-the-adapter
 */
public class PageItemsAdapter extends ArrayAdapter<PageItem> implements SharedPreferences.OnSharedPreferenceChangeListener {

  private final PreferencesType preferences;
  private float textSizeTitle;
  private float textSizeSubtitle;
  private HashSet<TextView> titles = new HashSet<>();
  private HashSet<TextView> subtitles = new HashSet<>();

  public PageItemsAdapter(Context context, List<PageItem> pageItems, @NonNull PreferencesType preferences) {
    super(context, 0, pageItems);

    this.preferences = preferences;

    this.textSizeTitle = preferences.textSize().get();
    this.textSizeSubtitle = this.textSizeTitle / 3 * 2;
  }

  @Override @NonNull public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    // Get the data item for this position
    PageItem pageItem = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);
    }

    // Lookup views for data population and touch listening
    TextView textViewTitle = (TextView) convertView.findViewById(R.id.textViewTitle);
    TextView textViewSubtitle = (TextView) convertView.findViewById(R.id.textViewSubtitle);
    HorizontalScrollView scrollView = (HorizontalScrollView) convertView.findViewById(R.id.scrollView1);
    scrollView.scrollTo(0, 0);

    // Put TextViews in HashSet so we can update the appearance later
    titles.add(textViewTitle);
    subtitles.add(textViewSubtitle);

    // Set initial appearance
    textViewTitle.setTextSize(textSizeTitle);
    textViewSubtitle.setTextSize(textSizeSubtitle);

    if (pageItem != null) {
      // This may be dangerous:
      // This subscription will hold a reference to the TextView and the PageItem.
      // PageItems are released when a Page is destroyed, so we should be fine.
      pageItem.title().subscribe(textViewTitle::setText);
      pageItem.subtitle().subscribe(text -> {
        textViewSubtitle.setVisibility(text == null ? GONE : View.VISIBLE);
        textViewSubtitle.setText(text);
      });
      pageItem.description().subscribe(textViewTitle::setContentDescription);
    }

    // Attach touch listeners to perform a list item click
    // If you want to puke, see: https://code.google.com/p/android/issues/detail?id=3414
    textViewTitle.setOnTouchListener(new TouchListener(parent, position));
    textViewSubtitle.setOnTouchListener(new TouchListener(parent, position));
    scrollView.setOnTouchListener(new TouchListener(parent, position));

    // Return the completed view to render on screen
    return convertView;
  }

  @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    this.textSizeTitle = preferences.textSize().get();
    this.textSizeSubtitle = this.textSizeTitle / 3 * 2;
    for (TextView textView : titles) {
      textView.setTextSize(textSizeTitle);
    }
    for (TextView textView : subtitles) {
      textView.setTextSize(textSizeSubtitle);
    }
  }

  private class TouchListener extends OnTouchClickListener {

    private final ViewGroup parent;
    private final int position;

    TouchListener(ViewGroup parent, int position) {
      this.parent = parent;
      this.position = position;
    }

    @Override public boolean onClick(View view) {
      if (!parent.getClass().equals(ListView.class)) {
        return false;
      }
      ListView listView = (ListView) parent;
      listView.performItemClick(view, position, view.getId());
      return true;
    }
  }
}

@@ -304,16 +304,47 @@
 rences;%0A
+import android.graphics.Color;%0A
 import a
@@ -410,24 +410,57 @@
 utInflater;%0A
+import android.view.MotionEvent;%0A
 import andro
@@ -1172,16 +1172,115 @@
 ener %7B%0A%0A
+  // ripple_material_light%0A  private static final int highlightColor = Color.argb(0x1f, 0, 0, 0);%0A%0A
   privat
@@ -3436,16 +3436,51 @@
 em click
+ and visual indicator on focus, tap
 %0A    // 
@@ -3569,28 +3569,29 @@
     
-textViewTitle.setOnT
+final TouchListener t
 ouch
@@ -3590,33 +3590,35 @@
 er touchListener
-(
+ = 
 new TouchListene
@@ -3631,25 +3631,36 @@
 nt, position
-)
+, scrollView
 );%0A    textV
@@ -3654,36 +3654,33 @@
 w);%0A    textView
-Subt
+T
 itle.setOnTouchL
@@ -3691,43 +3691,77 @@
 ner(
-new TouchListener(parent, position)
+touchListener);%0A    textViewSubtitle.setOnTouchListener(touchListener
 );%0A 
@@ -3793,21 +3793,17 @@
 istener(
-new T
+t
 ouchList
@@ -3810,26 +3810,8 @@
 ener
-(parent, position)
 );%0A%0A
@@ -4420,16 +4420,43 @@
 osition;
+%0A    private View itemView;
 %0A%0A    To
@@ -4497,16 +4497,31 @@
 position
+, View itemView
 ) %7B%0A    
@@ -4576,25 +4576,546 @@
 sition;%0A    
-%7D
+  this.itemView = itemView;%0A    %7D%0A%0A    @Override public boolean onTouch(View view, MotionEvent motionEvent) %7B%0A      final int action = motionEvent.getActionMasked();%0A      if (action == MotionEvent.ACTION_DOWN) %7B%0A        itemView.setBackgroundColor(highlightColor);%0A      %7D else if (action == MotionEvent.ACTION_UP) %7B%0A        itemView.setBackgroundResource(0);%0A      %7D else if (action == MotionEvent.ACTION_CANCEL) %7B%0A        itemView.setBackgroundResource(0);%0A      %7D%0A%0A      return super.onTouch(view, motionEvent);%0A    %7D%0A
 %0A%0A    @Overr

Add visual feedback on list item tap