package com.ar.paintar.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ar.paintar.adapter.CategoryAdapter;
import com.ar.paintar.adapter.PhotoAdapter;
import com.ar.paintar.databinding.FragmentCategoryBinding;
import com.ar.paintar.room.AppDatabase;
import com.ar.paintar.room.ImageData;
import com.ar.paintar.utils.DatabaseUtil;
import com.ar.paintar.utils.ItemDecoration;
import com.ar.paintar.utils.Names;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;


public class CategoryFragment extends Fragment {

    private FragmentCategoryBinding binding;
    private final List<String> categories = Names.getAllDir();
    private CategoryAdapter adapter;
    private DatabaseUtil databaseUtil;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCategoryBinding.inflate(inflater, container, false);

        initData();
        initEvent();

        return binding.getRoot();
    }

    private void initData() {

        databaseUtil = new DatabaseUtil(requireContext());

        binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        adapter = new CategoryAdapter(requireContext(), new ArrayList<>(), requireActivity(),categories);
        binding.recyclerView.setAdapter(adapter);

        ItemDecoration itemDecoration = new ItemDecoration(16, 19, 10);
        binding.recyclerView.addItemDecoration(itemDecoration);

    }


    private void initEvent() {

        loadFirstCategoryImage(categories);

    }

    private void loadFirstCategoryImage(List<String> originalList) {
        Map<Integer, List<ImageData>> queryResultsMap = new HashMap<>();
        int totalQueries = originalList.size();

        for (int i = 0; i < totalQueries; i++) {
            String original = originalList.get(i);
            int finalI = i;
            databaseUtil
                    .getFirstCategory(original)
                    .observe(getViewLifecycleOwner(), new Observer<List<ImageData>>() {
                        @Override
                        public void onChanged(List<ImageData> imageEntries) {
                            queryResultsMap.put(finalI, imageEntries);
                            if (queryResultsMap.size() == totalQueries) {
                                List<ImageData> allImageEntries = new CopyOnWriteArrayList<>();
                                for (int j = 0; j < totalQueries; j++) {
                                    List<ImageData> currentEntries = queryResultsMap.get(j);
                                    if (currentEntries != null) {
                                        allImageEntries.addAll(currentEntries);
                                    }
                                }
                                adapter.updateData(allImageEntries);
                            }
                        }
                    });
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}