package com.lc.myapplication.security.mysoundstwo.fragment;

import android.graphics.Typeface;
import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.myapplication.security.mysoundstwo.R;
import com.lc.myapplication.security.mysoundstwo.adapter.FavouriteAdapter;
import com.lc.myapplication.security.mysoundstwo.adapter.RadioAdapter;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;

import java.util.List;


public class FavoriteFragment extends Fragment {
    private ImageView fav_null;
    private RecyclerView fav_recycle;
    private List<ScaryPrankLikeEntity> list;
    private boolean islike = true;
    private TextView fav_title;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_favorite, container, false);
        fav_null = view.findViewById(R.id.fav_null_img);
        fav_title = view.findViewById(R.id.fav_title);
        fav_title.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"myfront1.ttf"));
        fav_recycle = view.findViewById(R.id.fav_recycle);
        setRecycle();
        return view;
    }

    private void setRecycle() {
        MyTool.RunIO(new Runnable() {
            @Override
            public void run() {
                list = ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().getLike(islike);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!list.isEmpty()){
                            fav_null.setVisibility(View.GONE);
                            FavouriteAdapter favouriteAdapter = new FavouriteAdapter(getActivity(),list);
                            fav_recycle.setAdapter(favouriteAdapter);
                            fav_recycle.setLayoutManager(new GridLayoutManager(getContext(),1));
                        }else {
                            fav_null.setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        });

    }

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