package com.wallpaper.wallpaperbox.ui.activity;

import android.os.Bundle;
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.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.wallpaper.wallpaperbox.R;
import com.wallpaper.wallpaperbox.data.entity.WallpaperEntity;
import com.wallpaper.wallpaperbox.databinding.ActivityWallpaperListBinding;
import com.wallpaper.wallpaperbox.topon.AdManager;
import com.wallpaper.wallpaperbox.topon.onActionListener;
import com.wallpaper.wallpaperbox.ui.adapter.WallpaperAdapter;
import com.wallpaper.wallpaperbox.ui.viewmodel.WallpaperViewModel;
import com.wallpaper.wallpaperbox.util.ItemDecoration;

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

public class WallpaperListActivity extends AppCompatActivity {
    private ActivityWallpaperListBinding binding;
    private WallpaperAdapter adapter;
    private WallpaperViewModel wallpaperViewModel;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityWallpaperListBinding.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) {
            finish();
            return;
        }

        wallpaperViewModel = new ViewModelProvider(this).get(WallpaperViewModel.class);

        binding.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        adapter = new WallpaperAdapter(wallpaperViewModel, this, new ArrayList<>(), this, 1);
        binding.recyclerView.setAdapter(adapter);

        ItemDecoration itemDecoration = new ItemDecoration(20, 15, 20);
        binding.recyclerView.addItemDecoration(itemDecoration);
    }

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

        getCategoryByName();
    }

    private void getCategoryByName() {
        wallpaperViewModel
                .getCategoryByName(name)
                .observe(this, new Observer<List<WallpaperEntity>>() {
                    @Override
                    public void onChanged(List<WallpaperEntity> wallpaperEntities) {
                        adapter.updateData(wallpaperEntities);
                    }
                });
    }
}