package com.key.coolkeyboard.fragment;

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

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

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

import com.key.coolkeyboard.R;
import com.key.coolkeyboard.adapter.ThumAdapter;
import com.key.coolkeyboard.data.MydataBase;
import com.key.coolkeyboard.data.Otherdata;
import com.key.coolkeyboard.databinding.FragmentLikeBinding;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.tool.SpaceItem;

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


public class LikeFragment extends Fragment {
    FragmentLikeBinding binding;
    private SpaceItem spaceItem = new SpaceItem(12, 12, 12);
    private List<Otherdata> likelist = new ArrayList<>();
    private ThumAdapter thumAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLikeBinding.inflate(inflater, container, false);

        binding.likeTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Baumans-Regular.ttf"));
        binding.nolikeText.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Baumans-Regular.ttf"));

        initData();
        binding.likeRv.addItemDecoration(spaceItem);

        return binding.getRoot();
    }

    private void initData() {

        Mytool.runIO(new Runnable() {
            @Override
            public void run() {
                likelist = MydataBase.getInstance().myDAO().getlikelist(true);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (likelist.isEmpty()) {
                            binding.likeHintImage.setVisibility(View.VISIBLE);
                            binding.nolikeText.setVisibility(View.VISIBLE);
                        } else {
                            binding.likeHintImage.setVisibility(View.GONE);
                            binding.nolikeText.setVisibility(View.GONE);
                        }
                        thumAdapter = new ThumAdapter(requireContext(), likelist);
                        binding.likeRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
                        binding.likeRv.setAdapter(thumAdapter);
                    }
                });

            }
        });


    }

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