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 AnimalsFragment extends Fragment {

    private RecyclerView rv4dImages;
    private AnimalsImageAdapter 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_animals, container, false);

        rv4dImages = view.findViewById(R.id.rv_4d_images);

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

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


        loadWallpaperData();

        adapter = new AnimalsImageAdapter(getContext(), wallpaperList);
        rv4dImages.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 = "Beautiful wallpapers";
            }
            intent.putExtra(FullImageActivity.EXTRA_IMAGE_DESC, description);

            startActivity(intent);
        });

        return view;
    }

    private void loadWallpaperData() {
        wallpaperList = JsonUtils.loadWallpapersFromJson(getContext(), "Animals.json");
        if (wallpaperList == null) {
            wallpaperList = new ArrayList<>();
            Toast.makeText(getContext(), "Failed to load wallpaper data", Toast.LENGTH_SHORT).show();
        }
    }
}