package com.lh.wallpaper2.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.ImageView;
import android.widget.RelativeLayout;

import com.lh.wallpaper2.R;
import com.lh.wallpaper2.ViewActivity;
import com.lh.wallpaper2.action.FavoriteWallpaperListener;
import com.lh.wallpaper2.adapter.FavoriteWallpaperAdapter;
import com.lh.wallpaper2.room.TaskDao;
import com.lh.wallpaper2.room.Favorite;
import com.lh.wallpaper2.room.AppDatabase;
import com.lh.wallpaper2.file.MyItemSpace;
import java.util.ArrayList;
import java.util.List;


public class FavoriteFragment extends Fragment implements FavoriteWallpaperListener {
    private RecyclerView recyclerviewWallpaper;
    private TaskDao dao;
    private List<Favorite> favoriteList= new ArrayList<>();
    private AppDatabase db;
    private FavoriteWallpaperAdapter favoriteWallpaperAdapter;
    private ImageView likeBackground;
    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);

        likeBackground = view.findViewById(R.id.like_background);
        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<Favorite> favorites = db.taskDao().getAllTasks();
                   requireActivity().runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           if (favorites.isEmpty()){
                               likeBackground.setVisibility(View.VISIBLE);
                               likeText.setVisibility(View.VISIBLE);
                           }else {
                               likeBackground.setVisibility(View.GONE);
                               likeText.setVisibility(View.GONE);
                           }
                           favoriteList.clear();
                           favoriteList.addAll(favorites);
                           favoriteWallpaperAdapter.notifyDataSetChanged();
                       }
                   });
            }
        }).start();
    }
    private void setRecyclerviewWallpaper() {
        favoriteWallpaperAdapter = new FavoriteWallpaperAdapter(favoriteList, requireContext());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerviewWallpaper.setLayoutManager(gridLayoutManager);
        favoriteWallpaperAdapter.setInfoWallpaperListener(this);
        MyItemSpace myItemSpace = new MyItemSpace(30, 50, 40);
        recyclerviewWallpaper.addItemDecoration(myItemSpace);
        recyclerviewWallpaper.setAdapter(favoriteWallpaperAdapter);

    }


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

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