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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.linktab.LinkMode;
import com.bonait.bnframework.common.message.MessageLooper;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.bonait.bnframework.modules.home.adapter.liaochang_adapter;
import com.orhanobut.logger.Logger;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class Silos_item_fragment extends BaseFragment {
    @BindView(R.id.recycler_view_lll)
    RecyclerView recycler_view_lll;
    private Context context;
    private Activity activity=null;
    @Override
    protected View onCreateView() {

        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_silos_item, null);
        ButterKnife.bind(this, root);
        context = getContext();
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        InitView();
        InitEvent();
    }

    //当前集合数据
    public int index = -1;

    public void SetData(int _index,Activity _activity) {
        index = _index;
        activity=_activity;
    }

    public void InitEvent()
    {
        MessageManager.getInstance().registerMessageReceiver(getActivity(), MessageName.RefreshSilos, new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    InitViewData();
                }
            }
        });
    }

    /**
     * 刷新
     */
    public void InitViewData()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(activity!=null)
                {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            InitView();
                        }
                    });
                }
            }
        }).start();
    }
    /**
     * 初始化界面
     */
    public void InitView() {

        if (index != -1) {
            StaggeredGridLayoutManager layout11 = new StaggeredGridLayoutManager
                    (4, StaggeredGridLayoutManager.VERTICAL);
            recycler_view_lll.setLayoutManager(layout11);

            liaochang_adapter adapter = new liaochang_adapter(context, DataBus.getInstance().SilosData.get(index).content);
            recycler_view_lll.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
        MessageManager.getInstance().unRegisterMessageReceiver(getActivity());
    }

    /**
     * 当在activity设置viewPager + BottomNavigation + fragment时，
     * 为防止viewPager左滑动切换界面，与fragment左滑返回上一界面冲突引起闪退问题，
     * 必须加上此方法，禁止fragment左滑返回上一界面。
     * <p>
     * 切记！切记！切记！否则会闪退！
     * <p>
     * 当在fragment设置viewPager + BottomNavigation + fragment时，则不会出现这个问题。
     */
    @Override
    protected boolean canDragBack() {
        return false;
    }
}