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.FragmentDownloadBinding;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.tool.SpaceItem;

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


public class DownloadFragment extends Fragment {
    FragmentDownloadBinding 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 = FragmentDownloadBinding.inflate(getLayoutInflater());

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

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

        return binding.getRoot();
    }

    private void initData() {
        Mytool.runIO(new Runnable() {
            @Override
            public void run() {
                likelist = MydataBase.getInstance().myDAO().getdownloadlist(true);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (likelist.isEmpty()) {
                            binding.downHintImage.setVisibility(View.VISIBLE);
                            binding.downText.setVisibility(View.VISIBLE);
                        } else {
                            binding.downHintImage.setVisibility(View.GONE);
                            binding.downText.setVisibility(View.GONE);
                        }
                        thumAdapter = new ThumAdapter(requireContext(), likelist);
                        binding.downRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
                        binding.downRv.setAdapter(thumAdapter);
                    }
                });

            }
        });
    }

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