package com.wallpaper.perfectwallpaper.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Toast;

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.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wallpaper.perfectwallpaper.R;
import com.wallpaper.perfectwallpaper.adapter.ImageAdapter;
import com.wallpaper.perfectwallpaper.adapter.MainViewPager2Adapter;
import com.wallpaper.perfectwallpaper.databinding.ActivityCategoryBinding;
import com.wallpaper.perfectwallpaper.databinding.ActivityMainBinding;
import com.wallpaper.perfectwallpaper.databinding.MainTabItemCustomBinding;
import com.wallpaper.perfectwallpaper.fragment.CategoryFragment;
import com.wallpaper.perfectwallpaper.fragment.FavoriteFragment;
import com.wallpaper.perfectwallpaper.fragment.HomeFragment;
import com.wallpaper.perfectwallpaper.room.ImageEntry;
import com.wallpaper.perfectwallpaper.utils.DatabaseUtils;
import com.wallpaper.perfectwallpaper.utils.ItemDecoration;

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

public class CategoryActivity extends AppCompatActivity {

    private ActivityCategoryBinding binding;
    private ImageAdapter adapter;
    private DatabaseUtils databaseUtils;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        binding = ActivityCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initData();
        initEvent();

    }

    private void initData() {

        name = getIntent().getStringExtra("Name");
        if (name == null) {
            Toast.makeText(this, "The picture does not exist", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        databaseUtils = new DatabaseUtils(this);

        binding.recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new ImageAdapter(this, new ArrayList<>(), this);
        binding.recyclerView.setAdapter(adapter);

        ItemDecoration itemDecoration = new ItemDecoration(16, 19, 10);
        binding.recyclerView.addItemDecoration(itemDecoration);

    }

    private void initEvent() {

        binding.back.setOnClickListener(v -> finish());
        binding.text.setText(name);

        loadImage();
    }

    private void loadImage() {
        databaseUtils
                .getAllCategory(name)
                .observe(this, new Observer<List<ImageEntry>>() {
                    @Override
                    public void onChanged(List<ImageEntry> imageEntries) {
                        adapter.updateData(imageEntries);
                    }
                });
    }


}