package com.ar.artraceartpro.ui.fragment;

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

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

import com.ar.artraceartpro.data.local.entity.ImageData;
import com.ar.artraceartpro.databinding.FragmentCategoryBinding;
import com.ar.artraceartpro.ui.adapter.ImageRecyclerViewAdapter;
import com.ar.artraceartpro.utils.Files;
import com.ar.artraceartpro.utils.ItemDecoration;
import com.ar.artraceartpro.viewmodel.ImageViewModel;

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


public class CategoryFragment extends Fragment {

    private FragmentCategoryBinding binding;
    private ImageRecyclerViewAdapter categoryAdapter;
    private final List<String> categories = Files.getAllDir();
    private ImageRecyclerViewAdapter recommendedAdapter;
    private ImageViewModel imageViewModel;

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

        initData();
        initEvent();

        return binding.getRoot();
    }

    private void initData() {

        imageViewModel = new ViewModelProvider(requireActivity(), new ViewModelProvider.AndroidViewModelFactory(requireActivity().getApplication()))
                .get(ImageViewModel.class);

        binding.categoryRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1, GridLayoutManager.HORIZONTAL, false));
        categoryAdapter = new ImageRecyclerViewAdapter(new ArrayList<>(), requireContext(), requireActivity(), 0,categories);
        binding.categoryRecyclerView.setAdapter(categoryAdapter);

        binding.recommendedRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recommendedAdapter = new ImageRecyclerViewAdapter(new ArrayList<>(), requireContext(), requireActivity(), 1,categories);
        binding.recommendedRecyclerView.setAdapter(recommendedAdapter);

        ItemDecoration itemDecoration1 = new ItemDecoration(8, 10, 5);
        ItemDecoration itemDecoration2 = new ItemDecoration(50, 19, 5);
        binding.categoryRecyclerView.addItemDecoration(itemDecoration1);
        binding.recommendedRecyclerView.addItemDecoration(itemDecoration2);

    }

    private void initEvent() {
        loadFirstCategory();
        loadRecommend();
    }

    private void loadFirstCategory() {
        imageViewModel
                .getFirstCategory(categories)
                .observe(getViewLifecycleOwner(), new Observer<List<ImageData>>() {
                    @Override
                    public void onChanged(List<ImageData> imageDataList) {
                        if (imageDataList != null) {
                            categoryAdapter.updateData(imageDataList);
                        }
                    }
                });
    }

    private void loadRecommend() {
        int offset = new Random().nextInt(300);
        String name = "png_";
        imageViewModel
                .getRecommend(name, offset)
                .observe(getViewLifecycleOwner(), new Observer<List<ImageData>>() {
                    @Override
                    public void onChanged(List<ImageData> imageDataList) {
                        if (imageDataList != null) {
                            recommendedAdapter.updateData(imageDataList);
                        }
                    }
                });
    }

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