package com.wall.exquisite.wallpaper;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

public class PatternsFragment extends Fragment {

    private RecyclerView rvLiveImages;
    private PatternsImageAdapter adapter;
    private List<Wallpaper_Item> wallpaperList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_patterns, container, false);

        rvLiveImages = view.findViewById(R.id.rv_live_images);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        gridLayoutManager.setAutoMeasureEnabled(true);
        rvLiveImages.setLayoutManager(gridLayoutManager);

//        rvLiveImages.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
//        rvLiveImages.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL));
        rvLiveImages.setHasFixedSize(true);


        loadWallpaperData();


        adapter = new PatternsImageAdapter(getContext(), wallpaperList);
        rvLiveImages.setAdapter(adapter);

        adapter.setOnItemClickListener((position, wallpaperItem) -> {

            if (getContext() == null || wallpaperItem == null) {
                Toast.makeText(getContext(), "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(getContext(), "NULL", Toast.LENGTH_SHORT).show();
                return;
            }

            Intent intent = new Intent(getContext(), 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 = "精美壁纸";
            }
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_DESC, description);


            startActivity(intent);
        });

        return view;
    }

    private void loadWallpaperData() {
        // 从Featured.json加载壁纸数据
        wallpaperList = JsonUtils.loadWallpapersFromJson(getContext(), "Featured.json");
        if (wallpaperList == null) {
            wallpaperList = new ArrayList<>();
            Toast.makeText(getContext(), "Wallpaper data loading failed", Toast.LENGTH_SHORT).show();
        }
    }
}