package com.haven.wallpapers.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.haven.wallpapers.R;
import com.haven.wallpapers.Utils.MyValue;
import com.haven.wallpapers.activity.ViewActivity;
import com.haven.wallpapers.listener.LIkeListener;
import com.haven.wallpapers.adapter.LikeAdapter;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.room.LikeDao;
import com.haven.wallpapers.room.AppDatabase;
import com.haven.wallpapers.Utils.MyItemSpace;
import java.util.ArrayList;
import java.util.List;


public class FavoriteFragment extends Fragment implements LIkeListener {
    private RecyclerView recyclerviewWallpaper;
    private LikeDao dao;
    private List<Data> favoriteList= new ArrayList<>();
    private AppDatabase db;
    private LikeAdapter likeAdapter;

    private RelativeLayout likeText;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("mmmmmmmmmmmmmmmmmm", "onCreateView: ");
        View view = inflater.inflate(R.layout.fragment_favorite, container, false);
        recyclerviewWallpaper = view.findViewById(R.id.like_recyclerview);


        likeText = view.findViewById(R.id.like_text);

        db = AppDatabase.getInstance(requireContext());
        dao = db.taskDao();
        setRecyclerviewWallpaper();
        loadFavoriteImages();
        return view;
    }
    @SuppressLint("NotifyDataSetChanged")
    private void loadFavoriteImages() {
        new Thread(new Runnable() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void run() {
                List<Data> favorites = db.taskDao().getAllTasks();
                   requireActivity().runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           if (favorites.isEmpty()){
                               likeText.setVisibility(View.VISIBLE);
                           }else {
                               likeText.setVisibility(View.GONE);
                           }
                           favoriteList.clear();
                           favoriteList.addAll(favorites);
                           likeAdapter.notifyDataSetChanged();
                       }
                   });
            }
        }).start();
    }
    private void setRecyclerviewWallpaper() {
        likeAdapter = new LikeAdapter(favoriteList, requireContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerviewWallpaper.setLayoutManager(gridLayoutManager);
        likeAdapter.setInfoWallpaperListener(this);
        MyItemSpace myItemSpace = new MyItemSpace(10, 10, 4);
        recyclerviewWallpaper.addItemDecoration(myItemSpace);
        recyclerviewWallpaper.setAdapter(likeAdapter);

    }


    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ViewActivity.class);
        intent.putExtra(MyValue.key_data, info);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
         loadFavoriteImages();
    }
}