package com.qhclh.ytzh.index.shangpin;

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

import com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.ShangpinPianquBean;
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 ShangpinFragment extends BaseFragment {
    @BindView(R.id.shangpin_banner)
    ImageView shangpin_banner;
    @BindView(R.id.shangpin_zonglan)
    TextView shangpin_zonglan;
    @BindView(R.id.shangpin_zongcunlan)
    TextView shangpin_zongcunlan;
    @BindView(R.id.shangpin_zongkonglan)
    TextView shangpin_zongkonglan;
    @BindView(R.id.shangpin_zongzhishu)
    TextView shangpin_zongzhishu;
    @BindView(R.id.rv_shangpinpianqu)
    RecyclerView rv_shangpinpianqu;
    @BindView(R.id.ll_fourareas)
    LinearLayout ll_fourareas;

    private List<ShangpinPianquBean> list;
    private ShangpinPianquAdapter shangpinPianquAdapter;
    private GridLayoutManager mLayoutManager;
    @Override
    protected int setLayout() {
        return R.layout.fragment_index_shangpin;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        Glide.with(getActivity())
                .load(R.drawable.shangpinjibbb)
                .error(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .into(shangpin_banner);
        shangpin_zonglan.setText("总栏:"+999);
        shangpin_zongcunlan.setText("存栏:"+599);
        shangpin_zongkonglan.setText("空栏:"+400);
        shangpin_zongzhishu.setText("总只数:"+3456780);
        rv_shangpinpianqu.setNestedScrollingEnabled(false);

    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        for (int i =0;i<5;i++){
            ShangpinPianquBean shangpinPianquBean = new ShangpinPianquBean();
            shangpinPianquBean.setId(i+10);
            shangpinPianquBean.setPianqu("片区:"+i);
            shangpinPianquBean.setZonglan("总栏:"+i+"10");
            shangpinPianquBean.setCunlan("存栏:"+i+"5");
            shangpinPianquBean.setKonglan("空栏:"+i+"1");

            list.add(shangpinPianquBean);
        }

        shangpinPianquAdapter = new ShangpinPianquAdapter(getActivity(),list);
        mLayoutManager=new GridLayoutManager(getActivity(),2,GridLayoutManager.VERTICAL,false);//设置为一个2列的纵向网格布局
        rv_shangpinpianqu.setLayoutManager(mLayoutManager);
        rv_shangpinpianqu.setAdapter(shangpinPianquAdapter);
    }

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

    @Override
    public void onResume() {
        super.onResume();
        if (MainActivity.homepageList.contains("003")){
            ll_fourareas.setVisibility(View.VISIBLE);
        }else {
            ll_fourareas.setVisibility(View.GONE);
        }
        if (MainActivity.homepageList.contains("004")){
            rv_shangpinpianqu.setVisibility(View.VISIBLE);
        }else {
            rv_shangpinpianqu.setVisibility(View.GONE);
        }
        if (MainActivity.homepageList.contains("006")){
            shangpin_banner.setVisibility(View.VISIBLE);
        }else {
            shangpin_banner.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(HomepageEvent event){
        if (event.getHomepageList().contains("003")){
            ll_fourareas.setVisibility(View.VISIBLE);
        }else {
            ll_fourareas.setVisibility(View.GONE);
        }
        if (event.getHomepageList().contains("004")){
            rv_shangpinpianqu.setVisibility(View.VISIBLE);
        }else {
            rv_shangpinpianqu.setVisibility(View.GONE);
        }
        if (event.getHomepageList().contains("006")){
            shangpin_banner.setVisibility(View.VISIBLE);
        }else {
            shangpin_banner.setVisibility(View.GONE);
        }
    }

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