package com.lh.wallpaper2;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lh.wallpaper2.action.InfoWallpaperListener;
import com.lh.wallpaper2.action.UrlWallpaperListener;
import com.lh.wallpaper2.adapter.DetailsAdapter;
import com.lh.wallpaper2.file.MyItemSpace;
import com.lh.wallpaper2.json.Info;
import com.lh.wallpaper2.json.Url;

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

public class DetailsActivity extends AppCompatActivity implements UrlWallpaperListener  {
    private TextView textView;
    private Info info;
    private RecyclerView recyclerView;
    private ImageView imageView;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_details);
        textView = findViewById(R.id.details_text);
        recyclerView = findViewById(R.id.details_recyclerView);
        info = (Info) getIntent().getSerializableExtra(StaticValue.key_info);
        Log.d("99999999999", "onCreate: " + info.getList().size());
        if (info != null) {
            String name = info.getName();
            if (name != null && !name.isEmpty()) {
                // 将首字母转换为大写，其他字母保持不变
                String capitalized = name.substring(0, 1).toUpperCase() + name.substring(1);
                textView.setText(capitalized);
            } else {
                textView.setText(name); // 如果 name 为 null 或空，直接设置
            }
            Log.d("99999999999", "onCreate: " + info.getName());
        }
        setRecyclerView();
        imageView = findViewById(R.id.imageview_back);
        imageView.setOnClickListener(v -> finish());
    }


    private void setRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        DetailsAdapter detailsAdapter = new DetailsAdapter(info.getList(), this);
        detailsAdapter.setUrlWallpaperListener(this);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(detailsAdapter);
        MyItemSpace myItemSpace = new MyItemSpace(30, 50, 40);
        recyclerView.addItemDecoration(myItemSpace);
    }

    @Override
    public void onItemClickAction(ArrayList<Url> dataInfo, int position) {

        startActivity(new Intent(this, ViewActivity.class).putExtra(StaticValue.key_picture, dataInfo).putExtra(StaticValue.key_position,position));
    }
}