package com.key.simplekeyboard.fragment;

import android.annotation.SuppressLint;
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.TextView;

import com.key.simplekeyboard.R;
import com.key.simplekeyboard.adapter.ThumAdapter;
import com.key.simplekeyboard.data.MydataBase;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.tools.Mytools;
import com.key.simplekeyboard.tools.SpaceItem;

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

public class LikeFragment extends Fragment {
    private RecyclerView likerv;
    private ThumAdapter thumAdapter;

    private SpaceItem spaceItem = new SpaceItem(12, 12, 12);
    private TextView likehint,title;
    private List<Otherdata> likelist = new ArrayList<>();

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_like, container, false);

        initData();
        likerv = view.findViewById(R.id.like_rv);
        likehint = view.findViewById(R.id.nolike_text);
        title = view.findViewById(R.id.like_title);
        likerv.addItemDecoration(spaceItem);
        title.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "DenkOne-Regular.ttf"));
        return view;
    }

    private void initData() {
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                likelist = MydataBase.getInstance().myDAO().getlikelist(true);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (likelist.isEmpty()) {
                            likehint.setVisibility(View.VISIBLE);
                        } else {
                            likehint.setVisibility(View.GONE);
                        }
                        thumAdapter = new ThumAdapter(requireContext(), likelist);
                        likerv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
                        likerv.setAdapter(thumAdapter);
                    }
                });

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