package com.haven.wallpapers.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.haven.wallpapers.R;
import com.haven.wallpapers.Utils.MyValue;
import com.haven.wallpapers.MyApp;
import com.haven.wallpapers.listener.UrlListener;
import com.haven.wallpapers.adapter.DetailsAdapter;
import com.haven.wallpapers.Utils.MyItemSpace;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.jsonbean.Info;
import com.haven.wallpapers.topon.AdManager;
import com.haven.wallpapers.topon.onActionListener;

import java.util.List;

public class DetailsActivity extends AppCompatActivity implements UrlListener {
    private TextView textView;
    private String name;
    private RecyclerView recyclerView;
    private ImageView imageView;

    private List<Data> dataList;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_details);
        AdManager.loadAllAd();

        textView = findViewById(R.id.details_text);
        recyclerView = findViewById(R.id.details_recyclerView);
        name = getIntent().getStringExtra(MyValue.key_info);

        textView.setText(name); // 如果 name 为 null 或空，直接设置

        setRecyclerView();
        imageView = findViewById(R.id.imageview_back);
        imageView.setOnClickListener(v -> finish());
    }


    private void setRecyclerView() {
        for (Info info : MyApp.getUserList()) {
            if (info.getName().equals(name)) {
                dataList = info.getList();
                break;
            }
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        DetailsAdapter detailsAdapter = new DetailsAdapter(dataList, this);
        detailsAdapter.setUrlWallpaperListener(this);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(detailsAdapter);
        MyItemSpace myItemSpace = new MyItemSpace(30, 50, 40);
        recyclerView.addItemDecoration(myItemSpace);
    }

    @Override
    public void onItemClickAction(Data dataInfo) {

        AdManager.showTopOn(DetailsActivity.this, new onActionListener() {
            @Override
            public void onAction() {
                startActivity(new Intent(DetailsActivity.this, ViewActivity.class).putExtra(MyValue.key_data, dataInfo));
            }
        });
    }
}