package com.uiui.zyappstore.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.uiui.zyappstore.R;
import com.uiui.zyappstore.activity.ClassAppActivity;
import com.uiui.zyappstore.activity.ManagerAppActivity;
import com.uiui.zyappstore.activity.MoreAppActivity;
import com.uiui.zyappstore.adapter.AppAdapter;
import com.uiui.zyappstore.adapter.BannerViewHolder;
import com.uiui.zyappstore.base.LazyLoadFragment;
import com.uiui.zyappstore.base.RefreshManager;
import com.uiui.zyappstore.bean.AppClassBean;
import com.uiui.zyappstore.bean.AppInfo;
import com.uiui.zyappstore.bean.Banner;
import com.uiui.zyappstore.bean.BaseResponse;
import com.uiui.zyappstore.bean.ImageUrl;
import com.uiui.zyappstore.common.CommonDatas;
import com.uiui.zyappstore.network.NetInterfaceManager;
import com.uiui.zyappstore.utils.DensityUtil;
import com.uiui.zyappstore.utils.LogUtils;
import com.uiui.zyappstore.utils.ToastUtil;
import com.uiui.zyappstore.utils.Utils;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.constants.IndicatorSlideMode;
import com.zhpan.bannerview.constants.PageStyle;
import com.zhpan.bannerview.holder.HolderCreator;
import com.zhpan.bannerview.utils.BannerUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by asus on 2017/10/23.
 * 精品的页面
 */

public class FeaturedFragment extends LazyLoadFragment implements RefreshManager.RefreshInterface {
    private static final String TAG = FeaturedFragment.class.getSimpleName();

    @BindView(R.id.featured_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.cl)
    ConstraintLayout cl;
    @BindView(R.id.iv_banner)
    ImageView iv_banner;
    @BindView(R.id.banner_view)
    BannerViewPager<String, BannerViewHolder> mViewPager;

    @BindView(R.id.cl_exclusive)
    ConstraintLayout cl_exclusive;
    @BindView(R.id.tv_exclusive_app)
    TextView tv_exclusive_app;
    @BindView(R.id.rv_exclusive)
    RecyclerView rv_exclusive;
    @BindView(R.id.iv_exclusive_nodata)
    ImageView iv_exclusive_nodata;

    @BindView(R.id.cl_popular)
    ConstraintLayout cl_popular;
    @BindView(R.id.tv_popular_app)
    TextView tv_popular_app;
    @BindView(R.id.rv_popular)
    RecyclerView rv_popular;
    @BindView(R.id.iv_popular_nodata)
    ImageView iv_popular_nodata;

    @BindView(R.id.cl_0)
    ConstraintLayout cl_0;
    @BindView(R.id.cl_1)
    ConstraintLayout cl_1;
    @BindView(R.id.cl_2)
    ConstraintLayout cl_2;
    @BindView(R.id.cl_3)
    ConstraintLayout cl_3;
    @BindView(R.id.cl_4)
    ConstraintLayout cl_4;
    @BindView(R.id.cl_5)
    ConstraintLayout cl_5;

    @BindView(R.id.iv_0)
    ImageView iv_0;
    @BindView(R.id.iv_1)
    ImageView iv_1;
    @BindView(R.id.iv_2)
    ImageView iv_2;
    @BindView(R.id.iv_3)
    ImageView iv_3;
    @BindView(R.id.iv_4)
    ImageView iv_4;
    @BindView(R.id.iv_5)
    ImageView iv_5;

    private Context mContext;
    private AppAdapter mExclusiveAppAdapter;
    private AppAdapter mPopularAppAdapter;
    private List<AppClassBean> mAppClassList;

    public static final String EXCLUSIVE_APP = "专属应用";
    public static final String POPULAR_APP = "热门应用";

    public FeaturedFragment() {

    }

    @Override
    public void onRefresh() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onLoadBitMap(boolean isLoad) {
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_featured;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        mContext = view.getContext();
        ((SimpleItemAnimator) rv_popular.getItemAnimator()).setSupportsChangeAnimations(false);
        setImageViewSize();
        RefreshManager.getInstance().register(this);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setOnRefreshListener(refreshlayout -> initAPPData());
        mRefreshLayout.autoRefresh();
        mViewPager.setIndicatorSlideMode(IndicatorSlideMode.SMOOTH)
                .setHolderCreator(new HolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new BannerViewHolder();
                    }
                })
                .setIndicatorColor(getResources().getColor(R.color.white), getResources().getColor(R.color.colorAccent))
                .setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
                    @Override
                    public void onPageClick(int position) {

                    }
                })
                .setInterval(5000);
        cl_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAppClass(0);
            }
        });
        cl_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAppClass(1);
            }
        });
        cl_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAppClass(2);
            }
        });
        cl_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAppClass(3);
            }
        });
        cl_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAppClass(4);
            }
        });
        cl_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(getActivity(), ManagerAppActivity.class));
                openAppClass(5);
            }
        });
        tv_exclusive_app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MoreAppActivity.class);
                intent.putExtra(CommonDatas.MORE_APP_TITLE, EXCLUSIVE_APP);
                startActivity(intent);
            }
        });
        tv_popular_app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MoreAppActivity.class);
                intent.putExtra(CommonDatas.MORE_APP_TITLE, POPULAR_APP);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initDatas() {
        mExclusiveAppAdapter = new AppAdapter(mContext);
        mExclusiveAppAdapter.setHasStableIds(true);
        mPopularAppAdapter = new AppAdapter(mContext);
        mPopularAppAdapter.setHasStableIds(true);

//        int orientation = mContext.getResources().getConfiguration().orientation;
//        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        rv_popular.setLayoutManager(gridLayoutManager);
//        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
//            rv_popular.setLayoutManager(gridLayoutManager);
//        } else {
//            rv_popular.setLayoutManager(new LinearLayoutManager(mContext));
//        }
        rv_popular.setAdapter(mPopularAppAdapter);

        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(mContext, 2);
        rv_exclusive.setLayoutManager(gridLayoutManager2);
        rv_exclusive.setAdapter(mExclusiveAppAdapter);
    }

    @Override
    public void loadData(Bundle savedInstanceState) {

    }

    @Override
    public void onConfigurationChanged(@androidx.annotation.NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
            rv_popular.setLayoutManager(gridLayoutManager);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
            rv_popular.setLayoutManager(gridLayoutManager);
        } else {
            rv_popular.setLayoutManager(new LinearLayoutManager(mContext));
        }
        initAPPData();
    }


    @Override
    public void onResume() {
        super.onResume();
        registerReceiver();
        mExclusiveAppAdapter.notifyDataSetChanged();
        mPopularAppAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (myReceiver != null) {
            try {
                mContext.unregisterReceiver(myReceiver);
            } catch (Exception e) {
                LogUtils.e("onDestroy", e.getMessage());
            }
        }
    }

    private void registerReceiver() {
        if (myReceiver == null) {
            myReceiver = new installReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_PACKAGE_ADDED);
            filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
            filter.addDataScheme("package");
            filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            mContext.registerReceiver(myReceiver, filter);
        }
    }

    private installReceiver myReceiver;

    public class installReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //接收安装广播
            if (action.equals(Intent.ACTION_PACKAGE_ADDED)
                    || action.equals(Intent.ACTION_PACKAGE_REPLACED)
                    || action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
//                adapter.setData(checkUpdateOrInstalled(newAppInfoList));
                mPopularAppAdapter.notifyDataSetChanged();
                Log.e("installReceiver", action);
            }

        }
    }

    synchronized private void initAPPData() {
//        int setting_admin_app = Settings.System.getInt(mContext.getContentResolver(), "setting_admin_app", 1);
//        if (setting_admin_app == 0) {
            getAdminApp();
//        } else {
//            getAllAppInfo();
//            getAdminApp();
//        }
        getBanner();
        getAppClass();
    }

    private void getBanner() {
        NetInterfaceManager.getInstance().getBannerObservable()
                .subscribe(new Observer<BaseResponse<Banner>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e("getBanner", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(BaseResponse<Banner> bannerBaseResponse) {
                        Log.e("getBanner", "onNext: " + bannerBaseResponse);
                        if (bannerBaseResponse.code == 200) {
                            String banner_img = bannerBaseResponse.data.getBanner_img();
                            String vertical_img = bannerBaseResponse.data.getVertical_img();
                            if (ScreenUtils.isPortrait()) {
                                setBannerResource(banner_img);
                            } else {
                                setBannerResource(vertical_img);
                            }
                        } else {
                            if (ScreenUtils.isPortrait()) {
                                setBannerResource(R.drawable.port_banner);
                            } else {
                                setBannerResource(R.drawable.land_banner);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("getBanner", "onError: " + e.getMessage());

                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getBanner", "onComplete: ");
                        setImageViewSize();
//                        int bannerWidth = iv_banner.getWidth();
//                        int bannerHeight = iv_banner.getHeight();
//                        Drawable drawable = iv_banner.getDrawable();
//                        if (drawable != null) {
//                            int width = drawable.getMinimumWidth();
//                            int height = drawable.getMinimumHeight();
//                            Log.e(TAG, "getBanner: iv_banner: width = " + bannerWidth);
//                            Log.e(TAG, "getBanner: iv_banner: height = " + bannerHeight);
//                            Log.e(TAG, "getBanner: Drawable: width = " + width);
//                            Log.e(TAG, "getBanner: Drawable: height = " + height);
//                            Log.e(TAG, "getBanner: scale  = " + (float) (1.0 * bannerWidth / width));
//                            Log.e(TAG, "getBanner: " + (int) ((float) (1.0 * bannerWidth / width) * height));
//                            int changeHeight = (int) ((double) (bannerWidth / width) * height);
//                            if (changeHeight != 0) {
//                                ViewGroup.LayoutParams params = iv_banner.getLayoutParams();
//                                params.height = changeHeight;
//                                iv_banner.setLayoutParams(params);
////                                ViewGroup.LayoutParams clParams = cl.getLayoutParams();
////                                clParams.height = changeHeight;
////                                cl.setLayoutParams(clParams);
//                                Log.e(TAG, "getBanner: iv_banner: height = " + iv_banner.getHeight());
//                                Log.e(TAG, "getBanner: cl: height = " + cl.getHeight());
//                            }
//                            iv_banner.setVisibility(View.VISIBLE);
//                        }
                    }
                });
    }

    private void setBannerResource(String url) {
        Glide.with(iv_banner)
                .load(url)
                .transition(new DrawableTransitionOptions().dontTransition())
                .transform(new RoundedCorners(DensityUtil.dpToPx(mContext, 8)))
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        iv_banner.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    private void setBannerResource(int resId) {
        Glide.with(iv_banner)
                .load(resId)
                .transition(new DrawableTransitionOptions().dontTransition())
                .transform(new RoundedCorners(DensityUtil.dpToPx(mContext, 8)))
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        iv_banner.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    private void setImageViewSize() {
        ViewGroup.LayoutParams ivParams = iv_banner.getLayoutParams();
        ViewGroup.LayoutParams clParams = cl.getLayoutParams();
        Log.e(TAG, "initView: getWidth: " + ivParams.width);
        Log.e(TAG, "initView: getHeight:" + ivParams.height);
        Log.e(TAG, "initView: getWidth: " + ivParams.width);
        Log.e(TAG, "initView: getHeight:" + ivParams.height);
        //获取屏幕宽度
        WindowManager m = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        m.getDefaultDisplay().getMetrics(outMetrics);
        Log.e(TAG, "initView: widthPixels: " + outMetrics.widthPixels);
        Log.e(TAG, "initView: heightPixels: " + outMetrics.heightPixels);

        int screenWidth = outMetrics.widthPixels;
        int imageViewWidth = screenWidth - DensityUtil.dpToPx(mContext, 20);
        //获取屏幕像素
        Resources res = mContext.getResources();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
//        if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//            //竖屏
//            BitmapFactory.decodeResource(res, R.drawable.port_banner, opts);
//        } else {
//            //横屏
//            BitmapFactory.decodeResource(res, R.drawable.land_banner, opts);
//        }
        opts.inSampleSize = 1;
        opts.inJustDecodeBounds = false;
        int w = opts.outWidth;
        int h = opts.outHeight;
        Log.e(TAG, "initView: w: " + w);
        Log.e(TAG, "initView: h: " + h);

        int imageViewheight = (int) ((float) (h * 1.0 / w) * imageViewWidth);
        ivParams.width = imageViewWidth;
        ivParams.height = imageViewheight;
        iv_banner.setLayoutParams(ivParams);
        clParams.height = imageViewheight;
        cl.setLayoutParams(clParams);
        Log.e(TAG, "setImageViewSize: clheight :" + cl.getHeight());
        Log.e(TAG, "setImageViewSize: imageViewheight :" + iv_banner.getHeight());
    }

    private void getAllAppInfo() {
        NetInterfaceManager.getInstance()
                .getQueryAllAppObservable()
                .subscribe(new Observer<BaseResponse<List<AppInfo>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAllAppInfo", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AppInfo>> listBaseResponse) {
                        Log.e("getAllAppInfo", "onNext: " + listBaseResponse);
                        setExclusiveAppdata(listBaseResponse);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAllAppInfo", "onError: " + e.getMessage());
                        setExclusiveAppError();
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAllAppInfo", "onComplete: ");
                        mRefreshLayout.finishRefresh();
                    }
                });
    }

    private void getAdminApp() {
        NetInterfaceManager.getInstance()
                .getAdminAppByClassObservable(2)
                .subscribe(new Observer<BaseResponse<List<AppInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e("getAdminApp", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(BaseResponse<List<AppInfo>> listBaseResponse) {
                        Log.e("getAdminApp", "onSubscribe: " + listBaseResponse);
                        setPopularAppdata(listBaseResponse);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("getAdminApp", "onError: " + e.getMessage());
                        setPopularAppError();
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAdminApp", "onComplete: ");
                        mRefreshLayout.finishRefresh();
                    }
                });
    }

    private void getAppClass() {
        NetInterfaceManager.getInstance().getAdminAppClassObservable()
                .subscribe(new Observer<BaseResponse<List<AppClassBean>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAppClass", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AppClassBean>> listBaseResponse) {
                        Log.e("getAppClass", "onNext: " + listBaseResponse);
                        if (listBaseResponse.code == 200) {
                            List<AppClassBean> appClassList = listBaseResponse.data;
                            if (appClassList != null && appClassList.size() != 0) {
                                setClass(appClassList);
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAppClass", "onError: " + e.getMessage());
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAppClass", "onComplete: ");
                    }
                });
    }

    private void setClass(List<AppClassBean> appClassList) {
        if (appClassList == null || appClassList.size() == 0) {
            return;
        }
        this.mAppClassList = appClassList;
        for (int i = 0; i < appClassList.size(); i++) {
            AppClassBean appClassBean = appClassList.get(i);
            int imageviewId = mContext.getResources().getIdentifier("iv_" + (i), "id", mContext.getPackageName());
            int textviewId = mContext.getResources().getIdentifier("tv_" + (i), "id", mContext.getPackageName());
            try {
                ImageView imageView = getActivity().findViewById(imageviewId);
                TextView textView = getActivity().findViewById(textviewId);
                Glide.with(mContext).load(appClassBean.getImg_url()).error(R.drawable.ic_app).into(imageView);
                textView.setText(appClassBean.getName());
            } catch (Exception e) {
                Log.e(TAG, "setClass: " + e.getMessage());
            }
        }
    }

    private void openAppClass(int index) {
        if (mAppClassList == null || mAppClassList.size() == 0) {
            ToastUtil.show("该分类没有应用");
            return;
        }
        AppClassBean appClassBean = mAppClassList.get(index);
        if (appClassBean == null) {
            ToastUtil.show("该分类没有应用");
            return;
        }
        int classId = appClassBean.getId();
        String className = appClassBean.getName();
        Intent intent = new Intent(getActivity(), ClassAppActivity.class);
        intent.putExtra(CommonDatas.APP_CLASS_ID, classId);
        intent.putExtra(CommonDatas.APP_CLASS_NAME, className);
        startActivity(intent);
    }

    private void setExclusiveAppdata(BaseResponse<List<AppInfo>> listBaseResponse) {
        int code = listBaseResponse.code;
        if (code == 200) {
            List<AppInfo> infos = listBaseResponse.data;
            if (infos.size() <= 6) {
                tv_exclusive_app.setVisibility(View.GONE);
            } else {
                tv_exclusive_app.setVisibility(View.VISIBLE);
            }
            List<AppInfo> filterApp = infos.stream().sorted(Comparator.comparingInt(AppInfo::getWeight).reversed()).limit(6).collect(Collectors.toList());
            rv_exclusive.setVisibility(View.VISIBLE);
            iv_exclusive_nodata.setVisibility(View.GONE);
            mExclusiveAppAdapter.setData(filterApp);
            cl_exclusive.setVisibility(View.VISIBLE);
        } else {
            cl_exclusive.setVisibility(View.GONE);
            rv_exclusive.setVisibility(View.GONE);
            iv_exclusive_nodata.setImageDrawable(mContext.getDrawable(R.drawable.app_nodata));
            iv_exclusive_nodata.setVisibility(View.VISIBLE);
        }
    }

    private void setExclusiveAppError() {
        rv_exclusive.setVisibility(View.GONE);
        iv_exclusive_nodata.setImageDrawable(mContext.getDrawable(R.drawable.app_nonetwork));
        iv_exclusive_nodata.setVisibility(View.VISIBLE);
    }

    private void setPopularAppdata(BaseResponse<List<AppInfo>> listBaseResponse) {
        int code = listBaseResponse.code;
        if (code == 200) {
            List<AppInfo> infos = listBaseResponse.data;
            if (infos.size() <= 6) {
                tv_popular_app.setVisibility(View.GONE);
            } else {
                tv_popular_app.setVisibility(View.VISIBLE);
            }
            List<AppInfo> filterApp = infos.stream().sorted(Comparator.comparingInt(AppInfo::getWeight).reversed()).limit(12).collect(Collectors.toList());
            rv_popular.setVisibility(View.VISIBLE);
            iv_popular_nodata.setVisibility(View.GONE);
            mPopularAppAdapter.setData(filterApp);
            cl_popular.setVisibility(View.VISIBLE);
        } else {
            cl_popular.setVisibility(View.GONE);
            rv_popular.setVisibility(View.GONE);
            iv_popular_nodata.setImageDrawable(mContext.getDrawable(R.drawable.app_nodata));
            iv_popular_nodata.setVisibility(View.VISIBLE);
        }
    }

    private void setPopularAppError() {
        rv_popular.setVisibility(View.GONE);
        iv_popular_nodata.setImageDrawable(mContext.getDrawable(R.drawable.app_nonetwork));
        iv_popular_nodata.setVisibility(View.VISIBLE);
    }

    private void getBannerImg() {
        NetInterfaceManager.getInstance()
                .getBannerImageControl()
                .getImageURL(Utils.getSerial())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<List<ImageUrl>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getBannerImg", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<ImageUrl>> listBaseResponse) {
                        int code = listBaseResponse.code;
                        if (code == 200) {
                            List<ImageUrl> urlList = listBaseResponse.data;
                            if (null != urlList && urlList.size() != 0) {
                                List<String> list = new ArrayList<>();
                                for (ImageUrl url : urlList) {
                                    list.add(url.getImg_url());
                                }
                                mViewPager
                                        .setPageMargin(getResources().getDimensionPixelOffset(R.dimen.dp_5))
                                        .setRevealWidth(BannerUtils.dp2px(0))
                                        .setPageStyle(PageStyle.MULTI_PAGE)
                                        .setHolderCreator(new HolderCreator<BannerViewHolder>() {
                                            @Override
                                            public BannerViewHolder createViewHolder() {
                                                return new BannerViewHolder();
                                            }
                                        })
                                        .setIndicatorColor(getResources().getColor(R.color.white), getResources().getColor(R.color.colorAccent))
                                        .setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
                                            @Override
                                            public void onPageClick(int position) {

                                            }
                                        })
                                        .setInterval(5000)
                                        .create(list);
                                mViewPager.setVisibility(View.VISIBLE);
                                iv_banner.setVisibility(View.GONE);

                            } else {
                                iv_banner.setVisibility(View.VISIBLE);
                                mViewPager.setVisibility(View.GONE);
                            }
                        } else {
                            iv_banner.setVisibility(View.VISIBLE);
                            mViewPager.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getBannerImg", "onError: " + e.getMessage());
                        iv_banner.setVisibility(View.VISIBLE);
                        mViewPager.setVisibility(View.GONE);
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getBannerImg", "onComplete: ");
                        mRefreshLayout.finishRefresh();
                    }
                });
    }

}
