package com.ar.ardrawingboard.ui.fragment;

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

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

import com.ar.ardrawingboard.ui.adapter.CategoryFragmentAdapter;
import com.ar.ardrawingboard.databinding.FragmentCategoryBinding;
import com.ar.ardrawingboard.room.AppDatabase;
import com.ar.ardrawingboard.room.BoardEntry;
import com.ar.ardrawingboard.util.ItemDecoration;
import com.ar.ardrawingboard.util.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> originalCategories = Names.getAllImage();
    private final List<String> displayCategories = new ArrayList<>();
    private CategoryFragmentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentCategoryBinding.inflate(inflater, container, false);

        initData();
        initEvent();
        setupRecyclerView();

        loadImages(originalCategories);

        return binding.getRoot();
    }

    private void initData() {
        for (String category : originalCategories) {
            displayCategories.add(category.substring(4));
        }
    }

    private void initEvent() {
    }

    private void setupRecyclerView() {
        binding.recyclerview.setLayoutManager(new GridLayoutManager(getContext(), 2));

        adapter = new CategoryFragmentAdapter(requireActivity(), new ArrayList<>(), originalCategories, displayCategories);
        binding.recyclerview.setAdapter(adapter);

        binding.recyclerview.addItemDecoration(new ItemDecoration(16, 19, 10));
    }

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

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

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