package com.bonait.bnframework.modules.home.fragment.from;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.SearchView;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.bonait.bnframework.modules.home.adapter.sp_adapter;
import com.qmuiteam.qmui.widget.QMUITopBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CpxzActivity extends BaseActivity {
    @BindView(R.id.topbar)
    QMUITopBar mTopBar;//顶部标题
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;//菜谱列表
    @BindView(R.id.search_view)
    SearchView search_view;//查询框
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cpxz);//属性绑定
        ButterKnife.bind(this);
        initTopBar();
        Init();
        initData();
    }


    private void initTopBar() {
        mTopBar.setTitle("菜谱列表");
        mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void Init()
    {
        search_view.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                ToastUtils.info("点击搜索按钮时触发"+s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                DataBus.getInstance().GetGoods(s);
                ToastUtils.info("搜索内容改变时触发,"+s);
                return false;
            }
        });
    }

    private void initData() {
        try{
            MyLayoutManager layout = new MyLayoutManager();
            layout.setAutoMeasureEnabled(true);
            recyclerView.setLayoutManager(layout);
            DataBus.getInstance().spadapter = new sp_adapter(this);
            recyclerView.setAdapter(DataBus.getInstance().spadapter);
            DataBus.getInstance().GetGoods("");
            DataBus.getInstance().spadapter.mListener=new MyClickListener() {
                @Override
                public void clickListener(View v, Object data) {
                    BPA_GOODS good=(BPA_GOODS)data;
                    DataBus.getInstance().SelectGood=good;
                    if( DataBus.getInstance().mListener!=null)
                    {
                        DataBus.getInstance().mListener.clickListener(v,good);
                    }
                    finish();
                }

                @Override
                public void clickListenerNew(View v, int k, Object data) {

                }
            };
        }catch(Exception e){
            ToastUtils.info("异常信息:"+e.getMessage());
        }
    }

    public void initEvent()
    {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}