package com.qhclh.ytzh.work;

import android.content.Intent;
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 com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.WorkBean;
import com.qhclh.ytzh.home.MainActivity;
import com.qhclh.ytzh.index.WorkEvent;
import com.qhclh.ytzh.utils.GlideImageLoader;
import com.qhclh.ytzh.utils.RecyclerViewUtil;
import com.qhclh.ytzh.work.breedpoultry.DatanalysisActivity;
import com.qhclh.ytzh.work.carsmannage.CarsManageActivity;
import com.qhclh.ytzh.work.carsmannage.FumudaiCarsManageActivity;
import com.qhclh.ytzh.work.productiondailyw.WriteDActivity;
import com.qhclh.ytzh.work.scan.FumudaiScanResultActivity;
import com.qhclh.ytzh.work.scan.ScanResultActivity;
import com.qhclh.ytzh.work.tablepoultry.TablepoultryActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

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/11/28.
 */
///< 工作fragment
public class WorkFragment extends BaseFragment {
    private List<String> list_path;
    @BindView(R.id.work_breedingpoultry)
    LinearLayout work_breedingpoultry;
    @BindView(R.id.work_carsmanager)
    LinearLayout work_carsmanager;
    @BindView(R.id.work_scan)
    LinearLayout work_scan;
    @BindView(R.id.work_tablepoultry)
    LinearLayout work_tablepoultry;
    @BindView(R.id.ll_productdaily)
    LinearLayout ll_productdaily;
    @BindView(R.id.ll_productdaily_w)
    LinearLayout ll_productdaily_w;
    @BindView(R.id.work_banner)
    Banner banner;
    @BindView(R.id.rv_work)
    RecyclerView rv_work;

    private WorkAdapter workAdapter;
    private List<WorkBean> workBeanList;
    private GridLayoutManager mLayoutManager;
    @BindView(R.id.work_iv)
    ImageView work_iv;

    @Override
    protected int setLayout() {
        return R.layout.fragment_work;
    }

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

    @Override
    protected void initData() {
        list_path = new ArrayList<>();
        workBeanList = new ArrayList<>();
    }

    @Override
    protected void initOper() {
        RecyclerViewUtil util = new RecyclerViewUtil(getActivity(), rv_work);
        util.setOnItemClickListener(new RecyclerViewUtil.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                if (position < 0) {
                    return;
                }

                long x = workAdapter.getItemId(position);
                if (x == 1) {
                    ///< 种禽日报表
                    startActivity(new Intent(getActivity(), DatanalysisActivity.class));
                }
                if (x == 2) {
                    ///< 种禽周报表
                    startActivity(new Intent(getActivity(), DatanalysisActivity.class));
                }
                if (x == 3) {
                    ///< 商品鸡日报
                    startActivity(new Intent(getActivity(), WriteDActivity.class));
                }
                if (x == 4) {
                    ///< 商品鸡报表
                    startActivity(new Intent(getActivity(), TablepoultryActivity.class));
                }
                if (x == 5) {
                    ///< 商品鸡扫一扫
                    startActivity(new Intent(getActivity(), ScanResultActivity.class));

                }
                if (x == 6) {
                    ///< 商品鸡扫码记录
                    startActivity(new Intent(getActivity(), CarsManageActivity.class));
                }
                if (x == 8) {
                    ///< 父母代扫一扫
                    startActivity(new Intent(getActivity(), FumudaiScanResultActivity.class));

                }
                if (x == 9) {
                    ///< 父母代扫码记录
                    startActivity(new Intent(getActivity(), FumudaiCarsManageActivity.class));
                }



            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    @Override
    public void onResume() {
        super.onResume();
        workBeanList.clear();
        list_path.clear();
        if (MainActivity.workList!=null) {
            if (MainActivity.workList.contains("001")) {
                workBeanList.add(new WorkBean(R.drawable.ribaobiao, getString(R.string.zhognqingribaobiao), 1));
            }
            if (MainActivity.workList.contains("002")) {
                workBeanList.add(new WorkBean(R.drawable.zhoubao, getString(R.string.zhognqingzhoubaobiao), 2));
            }
            if (MainActivity.workList.contains("003")) {
                workBeanList.add(new WorkBean(R.drawable.ribao, getString(R.string.shangpinjiribao), 3));
            }
            if (MainActivity.workList.contains("004")) {
                workBeanList.add(new WorkBean(R.drawable.shanp, getString(R.string.shangpinjibaobiao), 4));
            }
            if (MainActivity.workList.contains("005")) {
                ///< 商品鸡
                workBeanList.add(new WorkBean(R.drawable.saoyisao, getString(R.string.saoyisao), 5));
            }
            if (MainActivity.workList.contains("006")) {
                ///< 商品鸡
                workBeanList.add(new WorkBean(R.drawable.jilu, getString(R.string.saomajilv), 6));
            }
            if (MainActivity.workList.contains("008")) {
                ///< 父母代
                workBeanList.add(new WorkBean(R.drawable.saoyisao, getString(R.string.saoyisao), 8));
            }
            if (MainActivity.workList.contains("009")) {
                ///< 父母代
                workBeanList.add(new WorkBean(R.drawable.jilu, getString(R.string.saomajilv), 9));
            }



            if (MainActivity.workList.contains("007")) {
                work_iv.setVisibility(View.VISIBLE);
            } else {
                work_iv.setVisibility(View.GONE);
            }


            workAdapter = new WorkAdapter(getActivity(), workBeanList);

            mLayoutManager = new GridLayoutManager(getActivity(), 4, GridLayoutManager.VERTICAL, false);//设置为一个4列的纵向网格布局
            rv_work.setLayoutManager(mLayoutManager);
            rv_work.setAdapter(workAdapter);

            list_path.add("https://ss1.bdstatic.com/5aAHeD3nKgcUp2HgoI7O1ygwehsv/media/ch18/png/button_03.png");

            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            banner.setImageLoader(new GlideImageLoader());
            banner.setImages(list_path);
            banner.setBannerAnimation(Transformer.Default);
            banner.setDelayTime(3000);
            banner.isAutoPlay(true);
            banner.setIndicatorGravity(BannerConfig.CENTER);
            banner.start();
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {

                }
            });
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(WorkEvent event) {
        workBeanList.clear();
        list_path.clear();
        list_path.add("https://ss1.bdstatic.com/5aAHeD3nKgcUp2HgoI7O1ygwehsv/media/ch18/png/button_03.png");

        if (event.getWorkList()!=null) {
            if (event.getWorkList().contains("001")) {
                workBeanList.add(new WorkBean(R.drawable.ribaobiao, getString(R.string.zhognqingribaobiao), 1));
            }
            if (event.getWorkList().contains("002")) {
                workBeanList.add(new WorkBean(R.drawable.zhoubao, getString(R.string.zhognqingzhoubaobiao), 2));
            }
            if (event.getWorkList().contains("003")) {
                workBeanList.add(new WorkBean(R.drawable.ribao, getString(R.string.shangpinjiribao), 3));
            }
            if (event.getWorkList().contains("004")) {
                workBeanList.add(new WorkBean(R.drawable.shanp, getString(R.string.shangpinjibaobiao), 4));
            }
            if (event.getWorkList().contains("005")) {
                ///< 商品鸡
                workBeanList.add(new WorkBean(R.drawable.saoyisao, getString(R.string.saoyisao), 5));
            }
            if (event.getWorkList().contains("006")) {
                ///< 商品鸡
                workBeanList.add(new WorkBean(R.drawable.jilu, getString(R.string.saomajilv), 6));
            }
            if (event.getWorkList().contains("008")) {
                ///< 父母代
                workBeanList.add(new WorkBean(R.drawable.saoyisao, getString(R.string.saoyisao), 8));
            }
            if (event.getWorkList().contains("009")) {
                ///< 父母代
                workBeanList.add(new WorkBean(R.drawable.jilu, getString(R.string.saomajilv), 9));
            }
            if (event.getWorkList().contains("007")) {
                work_iv.setVisibility(View.VISIBLE);
            } else {
                work_iv.setVisibility(View.GONE);
            }
            workAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }

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