package com.qhclh.ytzh.index.fumudai;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.FumudaiBean;
import com.qhclh.ytzh.home.MainActivity;
import com.qhclh.ytzh.index.HomepageEvent;
import com.qhclh.ytzh.utils.RecyclerViewUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/22.
 */

public class FumudaiFragment extends BaseFragment {
    @BindView(R.id.fumudai_banner)
    ImageView fumudai_banner;
    @BindView(R.id.rv_fuwudai)
    RecyclerView rv_fuwudai;

    private List<FumudaiBean> fumudaiBeenList;
    private FumudaiAdapter fumudaiAdapter;
    private GridLayoutManager mLayoutManager;
    @Override
    protected int setLayout() {
        return R.layout.fragment_index_fumudai;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        Glide.with(getActivity())
                .load(R.drawable.fumudaiaaa)
                .error(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .into(fumudai_banner);
    }

    @Override
    protected void initData() {
        fumudaiBeenList = new ArrayList<>();
        fumudaiBeenList.add(new FumudaiBean(11,"空栏:"+123,R.mipmap.ic_launcher_round));
        fumudaiBeenList.add(new FumudaiBean(12,"育成期:"+34,R.mipmap.ic_launcher_round));
        fumudaiBeenList.add(new FumudaiBean(23,"产蛋期:"+78,R.mipmap.ic_launcher_round));

        fumudaiAdapter = new FumudaiAdapter(getActivity(),fumudaiBeenList);
        mLayoutManager=new GridLayoutManager(getActivity(),2,GridLayoutManager.VERTICAL,false);//设置为一个2列的纵向网格布局
        rv_fuwudai.setLayoutManager(mLayoutManager);
        rv_fuwudai.setAdapter(fumudaiAdapter);
    }

    @Override
    protected void initOper() {
        RecyclerViewUtil util=new RecyclerViewUtil(getActivity(),rv_fuwudai);
        util.setOnItemClickListener(new RecyclerViewUtil.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                System.out.println("aaa++++"+position);
                ///< 设置的id
                System.out.println("aaa+++++++"+fumudaiAdapter.getItemId(position));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MainActivity.homepageList!=null) {
            if (MainActivity.homepageList.contains("005")) {
                rv_fuwudai.setVisibility(View.VISIBLE);
            } else {
                rv_fuwudai.setVisibility(View.GONE);
            }
            if (MainActivity.homepageList.contains("006")) {
                fumudai_banner.setVisibility(View.VISIBLE);
            } else {
                fumudai_banner.setVisibility(View.GONE);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(HomepageEvent event){
        if (event.getHomepageList()!=null) {
            if (event.getHomepageList().contains("005")) {
                rv_fuwudai.setVisibility(View.VISIBLE);
            } else {
                rv_fuwudai.setVisibility(View.GONE);
            }
            if (event.getHomepageList().contains("006")) {
                fumudai_banner.setVisibility(View.VISIBLE);
            } else {
                fumudai_banner.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
