package com.wasu.cs.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wasu.cs.model.ChildrenData;
import com.wasu.module.log.WLog;

import java.util.ArrayList;
import java.util.List;

import cn.com.wasu.main.R;

/**
 * Created by jeepc on 2016/10/24.
 */

public class CSTabRecyclerViewAdapter
        extends RecyclerView.Adapter<CSTabRecyclerViewAdapter.ViewHolder> {
    private List<Object> mData = new ArrayList<Object>();
    private OnItemFocusChangeListener mOnItemFocusChangeListener;
    private OnItemClickListener mOnItemClickListener;


    public void setData(List<? extends Object> data) {
        mData.clear();
        mData.addAll(data);
    }

    public void setOnItemFocusChangeListener(OnItemFocusChangeListener onItemFocusChangeListener) {
        this.mOnItemFocusChangeListener = onItemFocusChangeListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_children_song_tab, null);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    private int curPos = -1;

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.name_tv.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (position == 0 && keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    return true;
                }
                return false;
            }
        });
        View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, final boolean hasFocus) {
                if(hasFocus){
                    holder.name_tv.setSelected(false);
                }
                if (mOnItemFocusChangeListener != null) {
                    mOnItemFocusChangeListener.onItemFocusChange(position, hasFocus);
                }
            }
        };
        holder.name_tv.setOnFocusChangeListener(onFocusChangeListener);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
            }
        };
        holder.name_tv.setOnClickListener(onClickListener);
        ChildrenData.ListBean bean = (ChildrenData.ListBean) mData.get(position);
        holder.name_tv.setText(bean.getTitle());
        if(bean.getFee()==0){
            holder.name_tv.setBackgroundResource(R.drawable.selector_children_song_tab_button);
        }else if(bean.getFee()==1){
            holder.name_tv.setBackgroundResource(R.drawable.selector_children_song_tab_fee_button);
        }
    }


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


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView name_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            name_tv = (TextView) itemView.findViewById(R.id.name_tv);
        }


    }

    public interface OnItemFocusChangeListener {
        void onItemFocusChange(int pos, boolean hasfocus);
    }

    public interface OnItemClickListener {
        void onItemClick(int pos);
    }

}

