package com.tjzhxx.union.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tjzhxx.union.R;
import com.tjzhxx.union.adapter.NewAdapter;
import com.tjzhxx.union.entity.BannerEntity;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.NewBean;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.retrofit.RetrofitUtils;
import com.tjzhxx.union.public_store.retrofit.UnionServices;
import com.tjzhxx.union.public_store.rx.DefaultSubscriber;
import com.tjzhxx.union.public_store.rx.ProgressSubscriber;
import com.tjzhxx.union.system.BaseActivity;
import com.tjzhxx.union.system.WebviewActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

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

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGABanner;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/9/13  11:54 PM
 */
public class PolicyListActivity extends BaseActivity {
    @BindView(R.id.bga_banner)
    BGABanner bgaBanner;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private NewAdapter mAdapter;
    private List<NewBean> newBeans = new ArrayList<>();
    private UnionServices services;
    private List<BannerEntity> bannerEntities = new ArrayList<>();
    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_new_policy;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("政策咨询");
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        bgaBanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(PolicyListActivity.this)
                        .load(model)
                        .placeholder(R.mipmap.bg_placeholder)
                        .error(R.mipmap.bg_placeholder)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        bgaBanner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
                if (bannerEntities != null && bannerEntities.size() > 0) {
                    BannerEntity entity = bannerEntities.get(position);
                    Intent intent = new Intent();
                    intent.setClass(PolicyListActivity.this, WebviewActivity.class);
                    intent.putExtra("url", entity.getWxurl());
                    intent.putExtra("title", "政策咨询");
                    startActivity(intent);
                }
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new NewAdapter(this, newBeans);
        mAdapter.setOnItemClickListener(new NewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, NewBean entity) {
                Intent intent = new Intent();
                intent.setClass(PolicyListActivity.this, WebviewActivity.class);
                intent.putExtra("url", entity.getWxurl());
                intent.putExtra("title", "政策咨询");
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(mAdapter);
        getBanners();
        loadDate();
    }
    /**
     * 加载广告
     */
    private void getBanners() {
        services.querytitlelist()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<BannerEntity>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<BannerEntity>>>(this) {
                    @Override
                    public void success(BaseResponse<List<BannerEntity>> value) {
                        bannerEntities = value.getData();
                        List<String> urls = new ArrayList<>();
                        List<String> titles = new ArrayList<>();
                        if (value.getData() != null) {
                            for (BannerEntity entity : value.getData()) {
                                if (!TextUtils.isEmpty(entity.getPicurl())) {
                                    urls.add(entity.getPicurl());
                                    titles.add(entity.getWxtitle());
                                }
                            }
                            bgaBanner.setData(urls, titles);
                        }
                    }

                });
    }

    private void loadDate() {
        services.querypoliticallistforapp()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<NewBean>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new ProgressSubscriber<BaseResponse<List<NewBean>>>(this) {
                    @Override
                    public void success(BaseResponse<List<NewBean>> value) {
                        if (value.getData() != null) {
                            newBeans.addAll(value.getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                });
    }
}
