package com.dskj.trustlend.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.azhon.basic.base.BaseLazyFragment;
import com.azhon.basic.utils.AnimUtil;
import com.bumptech.glide.Glide;
import com.dskj.trustlend.R;
import com.dskj.trustlend.ShenQingActivity;
import com.dskj.trustlend.databinding.FragmentHomeBinding;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.model.AdBean;
import com.dskj.trustlend.utils.ImageLoader;
import com.youth.banner.BannerConfig;


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


/**
 * 主页模块
 * @author xuhuixiang
 */
public class HomeFragment extends BaseLazyFragment<MainViewModel, FragmentHomeBinding>  {
    public static final String TAG = "HomeFragment";
    ArrayList<AdBean> list_path1 = new ArrayList<>();
    ArrayList<String> list_title1 = new ArrayList<>();

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void setUpView() {
        
        dataBinding.dayLy.setOnClickListener(v -> toType(0));
        dataBinding.weekLy.setOnClickListener(v -> toType(1));
        dataBinding.monthLy.setOnClickListener(v -> toType(2));
        initTestDate();
        initBanner();
    }

    private void initTestDate() {
        AdBean adBean = new AdBean();
        adBean.setTitle("标题");
        adBean.setImgUrl("https://img0.baidu.com/it/u=1243");
        list_path1.add(adBean);
        list_title1.add(adBean.getTitle());

    }

    private void toType(int i) {
        Intent intent = new Intent(getActivity(), ShenQingActivity.class);
        intent.putExtra("type",i);
        startActivity(intent);
    }


    @Override
    public void onResume() {
        super.onResume();

    }


    @Override
    protected void setUpData() {
        dataBinding.setModel(viewModel);
        dataBinding.setLifecycleOwner(this);
    }
    private void initBanner() {

        dataBinding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        dataBinding.banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        dataBinding.banner.setImages(list_path1);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        dataBinding.banner.setBannerAnimation(com.youth.banner.Transformer.Default);
        //设置轮播图的标题集合
        dataBinding.banner.setBannerTitles(list_title1);
        //设置轮播间隔时间
        dataBinding.banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        dataBinding.banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        dataBinding.banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(position1 -> toAdClick(dataBinding.banner.getContext(), list_path1.get(position1)))
                //必须最后调用的方法，启动轮播图。
                .start();
    }

    private void toAdClick(Context context, AdBean s) {

    }

    //自定义的图片加载器
    private class MyLoader extends com.youth.banner.loader.ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            ImageLoader.loadCenterCropBanner(getActivity(), (AdBean) path, imageView, R.mipmap.ad2, R.mipmap.ad2);
//            String urls = ((AdBean) path).getImgUrl();
//            Glide.with(getActivity()).load(urls).into(imageView);
        }
    }


    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(getActivity()).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    @Override
    protected void lazyLoad() {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

}
