package com.maxiee.heartbeat.ui.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.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.maxiee.heartbeat.R;
import com.maxiee.heartbeat.database.api.GetAllEventApi;
import com.maxiee.heartbeat.model.Event;
import com.maxiee.heartbeat.ui.adapter.EventListAdapter;

import java.util.ArrayList;

/**
 * Created by maxiee on 15-6-12.
 */
public class EventListFragment extends Fragment {
    private static final String TAG = EventListFragment.class.getSimpleName();

    private static final int VIEW_MODE_LIST = 0;
    private static final int VIEW_MODE_STAGGERED = 1;

    private RecyclerView mRecyclerView;
    private RelativeLayout mEmptyLayout;
    private ImageView mImageEmpty;
    private ArrayList<Event> mEventList;
    private EventListAdapter mAdapter;
    private int mViewMode;
    private MenuItem mViewModeMenu;
    private LinearLayoutManager mLinearLayoutManager;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = (View) inflater.inflate(R.layout.fragment_event_list, container, false);
        mEmptyLayout = (RelativeLayout) v.findViewById(R.id.empty);
        mImageEmpty = (ImageView) v.findViewById(R.id.image_empty);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
        mLinearLayoutManager = new LinearLayoutManager(mRecyclerView.getContext());
        mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mViewMode = VIEW_MODE_STAGGERED;
        mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
        mEventList = new GetAllEventApi(getActivity()).exec();
        mAdapter = new EventListAdapter(mEventList);
        updateEventList();
        setHasOptionsMenu(true);
        return v;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_eventlist_type, menu);
        mViewModeMenu = menu.findItem(R.id.type);
        mViewModeMenu.setIcon(getViewModeIconRes());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.type) {
            int currentViewMode = mViewMode;
            if (currentViewMode == VIEW_MODE_LIST) {
                mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
                mViewMode = VIEW_MODE_STAGGERED;
            }
            if (currentViewMode == VIEW_MODE_STAGGERED) {
                mRecyclerView.setLayoutManager(mLinearLayoutManager);
                mViewMode = VIEW_MODE_LIST;
            }
            mViewModeMenu.setIcon(getViewModeIconRes());
            // TODO 更新 SP
            return true;
        }
        return false;
    }

    public void updateEventList() {
        if (!mEventList.isEmpty()) {
            mRecyclerView.setVisibility(View.VISIBLE);
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            mEmptyLayout.setVisibility(View.VISIBLE);
            Glide.with(getActivity()).load(R.drawable.empty_bg).into(mImageEmpty);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ArrayList<Event> newEvents = new GetAllEventApi(getContext()).exec();
        if (!newEvents.isEmpty()) {
            mEventList.addAll(0, newEvents);
            mAdapter.notifyDataSetChanged();
        }
    }

    private int getViewModeIconRes() {
        if (mViewMode == VIEW_MODE_LIST) return R.drawable.ic_action_dashboard;
        else if (mViewMode == VIEW_MODE_STAGGERED) return R.drawable.ic_action_list;
        else {
            mViewMode = VIEW_MODE_STAGGERED;
            return R.drawable.ic_action_list;
        }
    }
}

@@ -37,16 +37,90 @@
 ments;%0A%0A
+import android.content.Context;%0Aimport android.content.SharedPreferences;%0A
 import a
@@ -137,16 +137,16 @@
 Bundle;%0A
-
 import a
@@ -1014,16 +1014,87 @@
 ame();%0A%0A
+    private static final String SP_VIEW_MODE = %22event_list_view_mode%22;%0A
     priv
@@ -1569,16 +1569,54 @@
 Manager;
+%0A    private SharedPreferences mPrefs;
 %0A%0A    @O
@@ -2226,34 +2226,131 @@
 );%0A        m
-ViewMode =
+Prefs = getActivity().getPreferences(Context.MODE_PRIVATE);%0A        mViewMode = mPrefs.getInt(SP_VIEW_MODE,
  VIEW_MODE_S
@@ -2349,32 +2349,33 @@
 W_MODE_STAGGERED
+)
 ;%0A        mRecyc
@@ -2359,32 +2359,70 @@
 GGERED);%0A       
+ if (mViewMode == VIEW_MODE_STAGGERED)
  mRecyclerView.s
@@ -2459,32 +2459,127 @@
 LayoutManager);%0A
+        if (mViewMode == VIEW_MODE_LIST) mRecyclerView.setLayoutManager(mLinearLayoutManager);%0A
         mEventLi
@@ -3658,21 +3658,62 @@
     
-// TODO %E6%9B%B4%E6%96%B0 SP
+mPrefs.edit().putInt(SP_VIEW_MODE, mViewMode).apply();
 %0A   

save view mode in SP.