package bob.sun.mpod.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import bob.sun.mpod.R;
import bob.sun.mpod.adapters.VHImageListItem;
import bob.sun.mpod.controller.OnTickListener;
import bob.sun.mpod.adapters.SimpleListMenuAdapter;
import bob.sun.mpod.model.SelectionDetail;

/**
 * Created by sunkuan on 15/4/30.
 */
public class SimpleListFragment extends Fragment implements OnTickListener {

    private RecyclerView listView;
    private SimpleListMenuAdapter adatper;
    int currentItemIndex;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup parent,
                             Bundle savedInstanceState){
        View ret = inflater.inflate(R.layout.layout_simple_list_menu, parent, false);
        listView = (RecyclerView) ret.findViewById(R.id.id_list_view_main_menu);
        listView.setAdapter(adatper);
        listView.setLayoutManager(new LinearLayoutManager(parent.getContext()));
        currentItemIndex = 0;
        adatper.highlightItem(0);
        return ret;
    }

    public void setAdatper(SimpleListMenuAdapter adatper){
        this.adatper = adatper;
    }

    /**
     * Compromise for the playinglist fragment.
     */
    public SimpleListMenuAdapter getAdapter(){
        return adatper;
    }

    @Override
    public void onNextTick() {
//        Log.e("index:", currentItemIndex + "");
        if(currentItemIndex >= listView.getAdapter().getItemCount()-1){
            currentItemIndex = listView.getAdapter().getItemCount()-1;
            return;
        }
        currentItemIndex+=1;
        adatper.highlightItem(currentItemIndex);
        VHImageListItem holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex - 1);
        if (holder != null)
            adatper.onBindViewHolder(holder, currentItemIndex - 1);
        if(currentItemIndex > ((LinearLayoutManager) listView.getLayoutManager()).findLastCompletelyVisibleItemPosition()) {
            listView.smoothScrollToPosition(currentItemIndex);
        }
        holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex);
        if (holder != null) {
            adatper.onBindViewHolder(holder, currentItemIndex);
        }
    }

    @Override
    public void onPreviousTick() {
        if(currentItemIndex < 1){
            return;
        }
        currentItemIndex -= 1;
        adatper.highlightItem(currentItemIndex);
        VHImageListItem holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex + 1);
        if (holder != null)
            adatper.onBindViewHolder(holder, currentItemIndex + 1);
        if(currentItemIndex < ((LinearLayoutManager) listView.getLayoutManager()).findFirstCompletelyVisibleItemPosition()) {
            listView.smoothScrollToPosition(currentItemIndex);
        }
        holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex);
        if (holder != null) {
            adatper.onBindViewHolder(holder, currentItemIndex);
        }

    }

    @Override
    public SelectionDetail getCurrentSelection() {
        SelectionDetail ret = new SelectionDetail();
        if (adatper.getItemCount() == 0){
            return ret;
        }
        switch (adatper.getType()){
            case SimpleListMenuAdapter.SORT_TYPE_TITLE:
                ret.setMenuType(ret.MENU_TYPE_SONGS);
                ret.setDataType(ret.DATA_TYPE_SONG);
                ret.setData(adatper.getItem(currentItemIndex));
                ret.setPlaylist(adatper.getList());
                ret.setIndexOfList(currentItemIndex);
                break;
            case SimpleListMenuAdapter.SORT_TYPE_ARTIST:
                ret.setMenuType(ret.MENU_TYPE_ARTIST);
                ret.setData(ret.DATA_TYPE_STRING);
                ret.setData(adatper.getItem(currentItemIndex));
                break;
            case SimpleListMenuAdapter.SORT_TYPE_ALBUM:
                ret.setMenuType(ret.MENU_TYPE_ALBUM);
                ret.setData(ret.DATA_TYPE_STRING);
                ret.setData(adatper.getItem(currentItemIndex));
                break;
            case SimpleListMenuAdapter.SORT_TYPE_GENRE:
                ret.setMenuType(ret.MENU_TYPE_GENRES);
                ret.setData(adatper.getItem(currentItemIndex));
        }
        return ret;
    }
}

@@ -189,24 +189,49 @@
 cyclerView;%0A
+import android.util.Log;%0A
 import andro
@@ -1586,18 +1586,16 @@
 ) %7B%0A
-//
         
 Log.
@@ -1594,46 +1594,30 @@
     
-Log.e(%22index:%22, currentItemIndex + %22%22)
+VHImageListItem holder
 ;%0A  
@@ -1757,16 +1757,180 @@
 nt()-1;%0A
+            holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex);%0A            adatper.onBindViewHolder(holder, currentItemIndex);%0A
         
@@ -2037,32 +2037,16 @@
         
-VHImageListItem 
 holder =
@@ -2363,39 +2363,52 @@
        listView.
-smoothS
+getLayoutManager().s
 crollToPosition(
@@ -2418,32 +2418,52 @@
 rentItemIndex);%0A
+            return;%0A
         %7D%0A      
@@ -2704,32 +2704,64 @@
 reviousTick() %7B%0A
+        VHImageListItem holder;%0A
         if(curre
@@ -2774,24 +2774,188 @@
 Index %3C 1)%7B%0A
+            holder = (VHImageListItem) listView.findViewHolderForAdapterPosition(currentItemIndex);%0A            adatper.onBindViewHolder(holder, currentItemIndex);%0A
             
@@ -3060,32 +3060,16 @@
         
-VHImageListItem 
 holder =
@@ -3399,15 +3399,28 @@
 iew.
-smoothS
+getLayoutManager().s
 crol
@@ -3442,32 +3442,52 @@
 rentItemIndex);%0A
+            return;%0A
         %7D%0A      

Fix scroll issue - again =.=