package com.hjx.miaohongentry.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hjx.miaohongentry.R;
import com.hjx.miaohongentry.bean.RowWord;
import com.hjx.miaohongentry.ui.FixGridLayout;
import com.hjx.miaohongentry.util.SpanTextViewUtils;

import java.util.List;

/**
 * Created by l on 2018/2/5.
 */

public class RowWordListAdapter extends BaseAdapter{

    private Context mContext;
    private List<RowWord> mList = null;

    public RowWordListAdapter(Context mContext, List<RowWord> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }


    @Override
    public int getCount() {
       return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int i) {
         return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder mViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.row_word_list_item, null);
            mViewHolder = new ViewHolder();
            mViewHolder.tv_row_idx = convertView.findViewById(R.id.row_idx);
            mViewHolder.ll_row_word = convertView.findViewById(R.id.ll_row_word);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        RowWord rowWord = (RowWord) getItem(position);
        if (rowWord != null) {
            if(!rowWord.getOrderNum().isEmpty()){
                mViewHolder.tv_row_idx.setText(rowWord.getOrderNum() + ".");
            }
            String rowStr = rowWord.getRowStr();
            char rowChar[] = rowStr.toCharArray();
            mViewHolder.ll_row_word.removeAllViews();
            for (int i = 0; i < rowChar.length; i++){
                mViewHolder.ll_row_word.addView(getWordView(rowChar[i]));
            }
        }
        return convertView;
    }

    private TextView getWordView( char word ){
        TextView tv = (TextView) LayoutInflater.from(mContext).inflate(
                R.layout.word_text_view, null);
        tv.setPadding(10,10,10,10);
        tv.setText(""+word);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpanTextViewUtils.gotoMiaohong(mContext,((TextView)view).getText().toString());
            }
        });
        return tv;
    }

    public static class ViewHolder {
        public TextView tv_row_idx;
        public FixGridLayout ll_row_word;
    }
}
