package com.fun.originalityprank.fragment;

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

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

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

import com.fun.originalityprank.R;
import com.fun.originalityprank.adapter.LikeAdapter;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.data.MydataBase;
import com.fun.originalityprank.databinding.FragmentLikeBinding;
import com.fun.originalityprank.databinding.ListRvItemBinding;
import com.fun.originalityprank.tool.Mytools;

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


public class LikeFragment extends Fragment {

    FragmentLikeBinding binding;
    private List<AllData> list = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLikeBinding.inflate(inflater,container,false);
        initData();


        binding.likeTitle.setTypeface(Typeface.createFromAsset(requireContext().getAssets(),"WendyOne-Regular.ttf"));


        return binding.getRoot();
    }
    private void initData() {
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                list = MydataBase.getInstance().likeDAO().getLikedata(true);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list.isEmpty()) {
                            binding.likeHintText.setVisibility(View.VISIBLE);
                            binding.likeHintImage.setVisibility(View.VISIBLE);
                            binding.likeRv.setVisibility(View.GONE);
                        } else {
                            binding.likeRv.setVisibility(View.VISIBLE);
                            binding.likeHintText.setVisibility(View.GONE);
                            binding.likeHintImage.setVisibility(View.GONE);
                            binding.likeRv.setAdapter(new LikeAdapter(requireActivity(), list));
                            binding.likeRv.setLayoutManager(new LinearLayoutManager(requireContext()));
                        }
                    }
                });
            }
        });
    }

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