package com.tjzhxx.union;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.activity.HomeGuanaiActivity;
import com.tjzhxx.union.activity.NewListActivity;
import com.tjzhxx.union.activity.PayActivity;
import com.tjzhxx.union.activity.PolicyListActivity;
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.entity.request.NewListRequest;
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.system.BaseFragment;
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 butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.Nullable;
import io.reactivex.schedulers.Schedulers;

/**
 * 描述:
 *
 * @author yanghuaxiong
 * @create 2021/9/1 11:44 AM
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.bga_banner)
    BGABanner bgaBanner;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<BannerEntity> bannerEntities = new ArrayList<>();
    private List<NewBean> beans = new ArrayList<>();
    private NewAdapter adapter;
    private UnionServices services;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        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(getActivity())
                        .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(getActivity(), WebviewActivity.class);
                    intent.putExtra("url", entity.getWxurl());
                    intent.putExtra("title", "工会新闻");
                    startActivity(intent);
                }
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new NewAdapter(getActivity(), beans);
        adapter.setOnItemClickListener(new NewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, NewBean entity) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), WebviewActivity.class);
                intent.putExtra("url", entity.getWxurl());
                intent.putExtra("title", "工会新闻");
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
        getBanners();
        querynewlist();
    }

    @OnClick({R.id.function01, R.id.function02, R.id.function03, R.id.function04, R.id.function05, R.id.function06, R.id.function07, R.id.function08})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.function01:
                intent.setClass(getActivity(), NewListActivity.class);
                startActivity(intent);
                break;
            case R.id.function02:
                if (ConstDefine.userInfo != null) {
                    if (ConstDefine.userInfo.getIsactive().equals("1")) {
                        showSnackBar("你已是会员");
                    } else {
                        showSnackBar("请先缴费");
                    }
                } else {
                    if (TextUtils.isEmpty(ConstDefine.city)){
                        showSnackBar("请先授权获取位置信息");
                        return;
                    }else if (!ConstDefine.city.contains("天津")){
                        showSnackBar("你不是天津人");
                        return;
                    }
                    intent.setClass(getActivity(), WebviewActivity.class);
                    intent.putExtra("url", ConstDefine.HttpAdressH5 + "/index.html#/guidepage");
                    intent.putExtra("title", "会员入会");
                    startActivity(intent);
                }
                break;
            case R.id.function03:
                if (ConstDefine.userInfo != null) {
                    if (ConstDefine.userInfo.getIsactive().equals("1")) {
                        showSnackBar("你已是会员");
                    } else {
                        intent.setClass(getActivity(), PayActivity.class);
                        startActivity(intent);
                    }
                } else {
                    showSnackBar("请先登录");
                }
                break;
            case R.id.function04:
                if (ConstDefine.userInfo != null) {
                    if (ConstDefine.userInfo.getIsactive().equals("1")) {
                        intent.setClass(getActivity(), HomeGuanaiActivity.class);
                        startActivity(intent);
                    } else {
                        intent.setClass(getActivity(), PayActivity.class);
                        startActivity(intent);
                    }
                } else {
                    showSnackBar("请先登录");
                }
                break;
            case R.id.function05:
                if (ConstDefine.userInfo == null){
                    showSnackBar("请先登录");
                    return;
                }
                intent.setClass(getActivity(), WebviewActivity.class);
                intent.putExtra("url", ConstDefine.HttpAdressH5+"/index.html#/sportList?type=jnpx");
                intent.putExtra("title", "技能培训");
                intent.putExtra("UserInfo",ConstDefine.userInfo);
                startActivity(intent);
                break;
            case R.id.function06:
                if (ConstDefine.userInfo == null){
                    showSnackBar("请先登录");
                    return;
                }
                intent.setClass(getActivity(), WebviewActivity.class);
                intent.putExtra("url", ConstDefine.HttpAdressH5+"/index.html#/sportList?type=wthd");
                intent.putExtra("title", "文体活动");
                intent.putExtra("UserInfo",ConstDefine.userInfo);
                startActivity(intent);
                break;
            case R.id.function07:
                intent.setClass(getActivity(), PolicyListActivity.class);
                startActivity(intent);
                break;
            case R.id.function08:
                intent.setClass(getActivity(), WebviewActivity.class);
                intent.putExtra("url", ConstDefine.HttpAdressH5+"/index.html#/legalAid");
                intent.putExtra("title", "法律援助");
                startActivity(intent);
                break;
        }
    }

    /**
     * 加载广告
     */
    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>>>(getActivity()) {
                    @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 querynewlist() {
        NewListRequest request = new NewListRequest();
        request.setPageNum(1);
        request.setPageSize(5);
        services.querynewlist(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<NewBean>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<NewBean>>>(getActivity()) {
                    @Override
                    public void success(BaseResponse<List<NewBean>> value) {
                        beans.clear();
                        beans.addAll(value.getResultData());
                        adapter.notifyDataSetChanged();
                    }

                });
    }
}
