package com.hd.wallpaper.free.walls.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.hd.wallpaper.free.walls.MyWallpaper;
import com.hd.wallpaper.free.walls.R;
import com.hd.wallpaper.free.walls.adapter.AdapterPreView;
import com.hd.wallpaper.free.walls.databinding.FragmentLikeBinding;
import com.hd.wallpaper.free.walls.db.DbManager;
import com.hd.wallpaper.free.walls.json.Data;
import com.hd.wallpaper.free.walls.mylistener.ItemClickListener;
import com.hd.wallpaper.free.walls.mylistener.OnDownloadListener;
import com.hd.wallpaper.free.walls.utils.CommonUtils;
import com.hd.wallpaper.free.walls.utils.ItemSpace;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentDownload extends Fragment implements ItemClickListener {

    private FragmentLikeBinding vb;
    private DataSubscription dataSubscription;

    public FragmentDownload() {

    }

    public static FragmentDownload newInstance() {
        FragmentDownload fragment = new FragmentDownload();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentLikeBinding.inflate(getLayoutInflater());
        vb.tvTitle.setText(getString(R.string.no_download_data));
        initList();
        return vb.getRoot();
    }

    private void initList() {
//        String saveFileDir = CommonKt.INSTANCE.getSaveFileDir(requireContext());
//        File file = new File(saveFileDir);
//        for (File listFile : Objects.requireNonNull(file.listFiles())) {
//             if(listFile.getName().endsWith(".jpg")){
//
//             }
//        }

        AdapterPreView adapterDownload = new AdapterPreView(requireContext(),true);
        dataSubscription = DbManager.setDownloadListener(new OnDownloadListener() {
            @Override
            public void onDownloadList(List<Data> dataList) {
                Log.d(MyWallpaper.TAG, "------onDownloadList size = " + dataList.size());
                if(dataList.size()==0){
                    vb.layoutNoData.setVisibility(View.VISIBLE);
                    vb.listRecycler.setVisibility(View.GONE);
                }else {
                    vb.layoutNoData.setVisibility(View.GONE);
                    vb.listRecycler.setVisibility(View.VISIBLE);
                    adapterDownload.setDataInfo(dataList);
                }
            }
        });
        ItemSpace itemSpace = new ItemSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemSpace);
        adapterDownload.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(adapterDownload);

    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ActivitySet.class);
        intent.putExtra(CommonUtils.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dataSubscription != null) {
            dataSubscription.cancel();
        }
        Log.d(MyWallpaper.TAG,"------favoriteFragment-onDestroyView");
    }
}