package com.app.paintingstool.myact;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.app.paintingstool.R;
import com.app.paintingstool.databinding.ActivityLoveBinding;
import com.app.paintingstool.myadapter.LovedAdapter;
import com.app.paintingstool.mydao.AppDatabase;
import com.app.paintingstool.mydao.FavoriteImage;
import com.app.paintingstool.myuntils.MyItemDecoration;

import java.util.List;

public class LoveActivity extends AppCompatActivity {
    private ActivityLoveBinding binding;
    private AppDatabase database;
    private LovedAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityLoveBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> {
            finish();
        });
        binding.favRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        binding.favRecycler.addItemDecoration(new MyItemDecoration(this, 16, 24, 20));
        database = AppDatabase.getInstance(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<FavoriteImage> favoriteImageList = database.favoriteImageDao().getAllImages();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new LovedAdapter(favoriteImageList, LoveActivity.this, LoveActivity.this);
                        binding.favRecycler.setAdapter(adapter);
                        if (favoriteImageList.isEmpty()){
                            binding.reDef2.setVisibility(View.VISIBLE);
                        }else {
                            binding.reDef2.setVisibility(View.GONE);
                        }
                    }
                });
            }
        }).start();
    }
}