package com.example.prettywallpapers.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.prettywallpapers.PrettyWallPaper;
import com.example.prettywallpapers.R;
import com.example.prettywallpapers.activity.PreviewActivity;
import com.example.prettywallpapers.base.MySpace;
import com.example.prettywallpapers.list.PrettyPaper;
import com.example.prettywallpapers.myvalue.MyValues;
import com.example.prettywallpapers.prettyadapter.DiscoverAdapter;
import com.example.prettywallpapers.room.MyTool;
import com.example.prettywallpapers.room.PrettyPaperDatabase;
import com.example.prettywallpapers.room.PrettyPaperEntity;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

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

public class HomeFragment extends Fragment {
    private Banner banner;
    private RecyclerView discover_recycle;
    private List<PrettyPaperEntity> list;

    List<String> stringList = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        banner = view.findViewById(R.id.banner);
        stringList.add("https://resource-sg-public.lux-ad.com/wallpaper/148499404d85516a86eeb5c2c30fafbd.jpeg");
        stringList.add("https://resource-sg-public.lux-ad.com/wallpaper/989ccbceb5d165cc4b44ced23af4d5fb.jpeg");
        stringList.add("https://resource-sg-public.lux-ad.com/wallpaper/b655bc8400774e2dc940b5a8291e2ca5.jpeg");
        stringList.add("https://resource-sg-public.lux-ad.com/wallpaper/ffdee201ab115879fd038a7f529d7d2c.jpeg");
        stringList.add("https://resource-sg-public.lux-ad.com/wallpaper/72306be102fb449d09c2f00190f333fd.jpeg");
        banner.setAdapter(new BannerImageAdapter<String>(stringList) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.itemView).load(data).apply(RequestOptions.bitmapTransform(new RoundedCorners(30))).into(holder.imageView);
            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                //Log.d("-----data","------data"+data);
                Intent intent = new Intent(getActivity(), PreviewActivity.class);
                //intent.putExtra(MyValues.TYPE,"1");
                //Log.d("-----MyValues.TYPE","------MyValues.TYPE"+MyValues.TYPE);
                intent.putExtra(MyValues.PAPER_IMAGE,String.valueOf(data));
                Log.d("-----data","------data"+String.valueOf(data));
                startActivity(intent);
            }
        });
        banner.setIndicator(new CircleIndicator(getActivity()));
        discover_recycle = view.findViewById(R.id.discover_recycle);
        setDiscoverRecycle();
        return view;
    }
    private void setDiscoverRecycle() {
        MyTool.runIO(new Runnable() {
            @Override
            public void run() {
               list =  PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().getAllUsers();
                MySpace mySpace = new MySpace(10,20,10);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
                discover_recycle.setLayoutManager(gridLayoutManager);
                discover_recycle.addItemDecoration(mySpace);
                DiscoverAdapter discoverAdapter = new DiscoverAdapter(getContext(), list.subList(153,353));
                discover_recycle.setAdapter(discoverAdapter);
            }
        });

    }
}