package com.wasu.cs.ui;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wasu.cs.widget.FocusGridLayoutManager;
import com.wasu.cs.widget.SpacesItemDecoration;

import java.util.List;

import cn.com.wasu.main.R;

/**
 * Created by chenming on 2016/8/4.
 *
 * @author chenming
 * @Description: 网易新闻专区图文播报fragment
 * @email chenming@wasu.com
 * @date 2016/8/4 13:19
 */

public class FragmentNeteasePicText extends Fragment {

	private RecyclerView mrecyclerview;
	private List<Data> testdatalist;
	private MyAdapter adapter;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View bodyview = inflater.inflate(R.layout.fragment_netease_pictext_body, container, false);
		mrecyclerview = (RecyclerView) bodyview.findViewById(R.id.pidtextrecycler);
		mrecyclerview.setHasFixedSize(true);
		SpacesItemDecoration spacesItemDecoration = new SpacesItemDecoration(9, 3);
		mrecyclerview.addItemDecoration(spacesItemDecoration);
		FocusGridLayoutManager gridLayoutManager = new FocusGridLayoutManager(getActivity(), 3);
		gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
		mrecyclerview.setLayoutManager(gridLayoutManager);
		gridLayoutManager.setSmoothScrollbarEnabled(true);
		adapter = new MyAdapter(testdatalist);
		mrecyclerview.setAdapter(adapter);


		return bodyview;
	}



	private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

		private List<Data> dataList;

		public MyAdapter(List<Data> mValueList) {
			this.dataList = mValueList;
		}

		@Override
		public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_pictext_netease, null);
			MyViewHolder viewHolder = new MyViewHolder(view);
			return viewHolder;

		}

		@Override
		public void onBindViewHolder(MyViewHolder holder, int position) {
			holder.titletext.setText(dataList.get(position).getTitle());
			//holder.simpleDraweeView.setImageURI(dataList.get(position).getUri());
		}


		@Override
		public int getItemCount() {
			return dataList.size();
		}

		class MyViewHolder extends RecyclerView.ViewHolder {
			//SimpleDraweeView simpleDraweeView;
			TextView titletext;

			public MyViewHolder(View itemView) {
				super(itemView);
				//simpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.netease_pictext_pic);
				titletext = (TextView) itemView.findViewById(R.id.netease_pictext_title);
				itemView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(getContext(), "item : " + getPosition() + " clicked", Toast.LENGTH_SHORT).show();
					}
				});
			}
		}
	}

	private static class Data {
		private String title;
		private Uri uri;

		public Data(String title, Uri uri) {
			this.title = title;
			this.uri = uri;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public Uri getUri() {
			return uri;
		}

		public void setUri(Uri uri) {
			this.uri = uri;
		}
	}
}
