package com.ar.paintar.activity;

import android.content.Intent;
import android.os.Bundle;

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

import com.ar.paintar.R;
import com.ar.paintar.adapter.PhotoAdapter;
import com.ar.paintar.databinding.ActivityCategoryBinding;
import com.ar.paintar.room.ImageData;
import com.ar.paintar.topon.AdManager;
import com.ar.paintar.topon.onActionListener;
import com.ar.paintar.utils.DatabaseUtil;
import com.ar.paintar.utils.ItemDecoration;

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

public class CategoryActivity extends AppCompatActivity {

    private ActivityCategoryBinding binding;
    private DatabaseUtil databaseUtil;
    private PhotoAdapter adapter;
    private String name;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        EdgeToEdge.enable(this);
        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() {
        AdManager.loadAllAd();

        name = getIntent().getStringExtra("Name");
        if (name == null) {
            finish();
            return;
        }
        title = name.substring(4);
        databaseUtil = new DatabaseUtil(this);

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

        ItemDecoration itemDecoration = new ItemDecoration(8, 9, 5);
        binding.recyclerView.addItemDecoration(itemDecoration);

    }

    private void initEvent() {

        binding.back.setOnClickListener(v -> {
            AdManager.showTopOn(this, new onActionListener() {
                @Override
                public void onAction() {
                    finish();
                }
            });
        });

        binding.text.setText(title);
        loadImage();
    }

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


    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}