package com.kfzs.duanduan.adp;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.bean.SearchRecordBean;
import com.kfzs.duanduan.db.SearchRecordHelper;
import com.kfzs.duanduan.utils.dlg.ViewFindUtils;

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

/**
 * @ Created by Dlg
 * @ <p>TiTle:  AdpSearchRecord</p>
 * @ <p>Description:搜索页的搜索历史列表Adp</p>
 * @ date:  2017/8/19 10:14
 * @ QQ:    315096953
 */

public class AdpSearchRecord extends BaseAdapter {

    private Context mContext;
    private List<SearchRecordBean> mList;
    /**
     * 这里是点击list列表后的监听
     */
    private AdapterView.OnItemClickListener mOnItemClickListener;
    private final int TAG_ID = R.id.search_tagflow_layout;

    /**
     * @param context
     * @param onItemClickListener 其中只有position有效，是点击时间的反馈
     */
    public AdpSearchRecord(Context context, AdapterView.OnItemClickListener onItemClickListener) {
        mContext = context;
        mList = new ArrayList<>();
        mList = SearchRecordHelper.getInstance().getRecordList();
        mOnItemClickListener = onItemClickListener;
    }

    public void clear() {
        mList.clear();
        SearchRecordHelper.getInstance().clearList();
    }

    public void add(String key) {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        SearchRecordBean searchRecordBean = new SearchRecordBean();
        searchRecordBean.setKey(key.trim());
        searchRecordBean.setDate(new Date());
        mList = SearchRecordHelper.getInstance().addRecordList(searchRecordBean);
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.listview_item_search_history, null);
            ViewFindUtils.hold(convertView, R.id.ibtn_listview_item_search_history_delete)
                    .setOnClickListener(clsDelete);
            convertView.setOnClickListener(clsSelect);
        }
        ViewFindUtils.hold(convertView, R.id.txt_listview_item_search_history_name, TextView.class)
                .setText(getItem(position).getKey());
        ViewFindUtils.hold(convertView, R.id.ibtn_listview_item_search_history_delete)
                .setTag(TAG_ID, position);
        convertView.setTag(TAG_ID, position);
        return convertView;
    }


    /**
     * 删除历史记录
     */
    private View.OnClickListener clsDelete = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SearchRecordHelper.getInstance().deleteSameRecord(mList.get((int) v.getTag(TAG_ID)), mList);
            notifyDataSetChanged();
        }
    };

    /**
     * 删除信息的回调
     */
    private View.OnClickListener clsSelect = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(null, v, (int) v.getTag(TAG_ID), 0);
        }
    };


}
