package com.prank.happypranksounds.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.prank.happypranksounds.R;
import com.prank.happypranksounds.data.entity.HappyData;
import com.prank.happypranksounds.databinding.ActivityCategoryBinding;
import com.prank.happypranksounds.topon.AdManager;
import com.prank.happypranksounds.topon.onActionListener;
import com.prank.happypranksounds.ui.adapter.SoundAdapter;
import com.prank.happypranksounds.ui.viewmodel.HappyViewModel;
import com.prank.happypranksounds.util.ItemDecoration;

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

public class CategoryActivity extends AppCompatActivity {

    private ActivityCategoryBinding binding;
    private HappyViewModel happyViewModel;
    private SoundAdapter adapter;
    private String name;

    @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() {
        name = getIntent().getStringExtra("name");
        if (name == null) {
            finish();
            return;
        }

        AdManager.loadAllAd();

        happyViewModel = new ViewModelProvider(this).get(HappyViewModel.class);

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

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

    private void initEvent() {

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

    private void loadCategoryList() {
        happyViewModel
                .getCategoryList(name)
                .observe(this, new Observer<List<HappyData>>() {
                    @Override
                    public void onChanged(List<HappyData> happyDataList) {
                        adapter.updateData(happyDataList);
                    }
                });
    }

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