package com.ar.artraceartpro.ui.activity;

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


import com.ar.artraceartpro.R;
import com.ar.artraceartpro.data.local.entity.ImageData;
import com.ar.artraceartpro.databinding.ActivityCategoryBinding;
import com.ar.artraceartpro.topon.AdManager;
import com.ar.artraceartpro.ui.adapter.ImageRecyclerViewAdapter;
import com.ar.artraceartpro.utils.ItemDecoration;
import com.ar.artraceartpro.viewmodel.ImageViewModel;

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

public class CategoryActivity extends AppCompatActivity {

    private ActivityCategoryBinding binding;
    private ImageRecyclerViewAdapter adapter;
    private ImageViewModel imageViewModel;
    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();

        imageViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication()))
                .get(ImageViewModel.class);

        name = getIntent().getStringExtra("name");
        if (name == null) {
            finish();
            return;
        }
        title = name.substring(4);

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

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

    }

    private void initEvent() {

        binding.back.setOnClickListener(v -> finish());
        binding.text.setText(title);
        loadImage();
    }

    private void loadImage() {
        imageViewModel
                .getAllCategory(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;
    }
}