package com.wall.exquisite.wallpaper;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ad.tradpluslibrary.TPAdManager;

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

public class CollectionActivity extends AppCompatActivity {

    private RecyclerView rvFavoriteImages;
    private TextView tvEmpty;
    private AnimalsImageAdapter adapter;
    private CollectionManager favoriteManager;
    private List<Wallpaper_Item> favoriteList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);

        setupStatusBar();
        TPAdManager.INSTANCE.showTPAD(this,()-> null);
        initViews();
        setupRecyclerView();
        loadFavoriteData();
    }

    private void setupStatusBar() {
        Window window = getWindow();
        WindowCompat.setDecorFitsSystemWindows(window, false);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(android.R.color.transparent));

        WindowInsetsControllerCompat insetsController = WindowCompat.getInsetsController(window, window.getDecorView());
        if (insetsController != null) {
            insetsController.setAppearanceLightStatusBars(true);
        }

    }

    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    private void initViews() {
        rvFavoriteImages = findViewById(R.id.rv_favorite_images);
        tvEmpty = findViewById(R.id.tv_empty);

        findViewById(R.id.btn_back).setOnClickListener(v ->  TPAdManager.INSTANCE.showTPAD(this,()-> {
            finish();
            return null;
        }));

        favoriteManager = new CollectionManager(this);
        favoriteList = new ArrayList<>();
    }

    private void setupRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        rvFavoriteImages.setLayoutManager(gridLayoutManager);

        adapter = new AnimalsImageAdapter(this, favoriteList);
        rvFavoriteImages.setAdapter(adapter);

        adapter.setOnItemClickListener((position, wallpaperItem) -> {
            if (wallpaperItem == null) {
                Toast.makeText(CollectionActivity.this, "Data error", Toast.LENGTH_SHORT).show();
                return;
            }

            String highResImageUrl = wallpaperItem.getUrls().getRegular();
            if (highResImageUrl == null || highResImageUrl.isEmpty()) {
                highResImageUrl = wallpaperItem.getUrls().getFull();
            }

            if (highResImageUrl == null || highResImageUrl.isEmpty()) {
                Toast.makeText(CollectionActivity.this, "Image URL is empty", Toast.LENGTH_SHORT).show();
                return;
            }

            Intent intent = new Intent(CollectionActivity.this, FullImageActivity.class);
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_URL, highResImageUrl);
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_ID, wallpaperItem.getId());

            String description = wallpaperItem.getAlt_description();
            if (description == null || description.isEmpty()) {
                description = "Favorited wallpapers";
            }
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_DESC, description);

            startActivity(intent);
        });
    }

    private void loadFavoriteData() {
        favoriteList.clear();
        favoriteList.addAll(favoriteManager.getFavoriteItems());

        Log.d("FavoriteActivity", "加载收藏数据，数量: " + favoriteList.size());

        if (favoriteList.isEmpty()) {
            tvEmpty.setVisibility(View.VISIBLE);
            rvFavoriteImages.setVisibility(View.GONE);

        } else {
            tvEmpty.setVisibility(View.GONE);
            rvFavoriteImages.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadFavoriteData();
    }
}