package com.unis.sxzcl.widget.menu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.unis.sxzcl.R;

import java.util.List;

/**
 * Created by mq on 2017/10/23 0023.
 */

public class MenuListView implements OnItemClickListener {

    private Context mContext;
    private RecyclerView recyclerView;
    private MenuListAdapter mAdapter;
    private MenuListCallBack callBack;

    public MenuListView(Context mContext) {
        this.mContext = mContext;
        init();
    }

    private void init() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        recyclerView = new RecyclerView(mContext);
        recyclerView.setLayoutParams(params);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new MenuListAdapter(null);
        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.list_empty_layout, null));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    public View getView() {
        return recyclerView;
    }

    public MenuListView setData(List<String> list) {
        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();
        return this;
    }

    public MenuListView addData(String item) {
        mAdapter.addData(item);
        return this;
    }

    public MenuListView setOnItemClickListener(MenuListCallBack listener) {
        this.callBack = listener;
        return this;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (callBack != null)
            callBack.onMenuItemClickListener(adapter, view, position, recyclerView);
        mAdapter.setSelectPosition(position);
    }

    /**
     * 设置选中项 并回调监听事件
     *
     * @param position
     */
    public void setSelectPosition(int position) {
        if (callBack != null) {
            callBack.onMenuItemClickListener(mAdapter, null, position, recyclerView);
        }
        mAdapter.setSelectPosition(position);
    }

    public interface MenuListCallBack {
        void onMenuItemClickListener(BaseQuickAdapter adapter, View view, int position, RecyclerView recyclerView);
    }
}
