package com.datarecovery.master.module.filerecover.fragment;


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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.atmob.app.lib.base.BaseFragment;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.databinding.FragmentFileRecoverListBinding;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.filerecover.FileRecoverViewModel;
import com.datarecovery.master.utils.FilesSearch;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class FileRecoverFragment extends BaseFragment<FragmentFileRecoverListBinding> {


    public static final String ID = "ID";


    private FileRecoverFragmentViewModel recoverFragmentViewModel;
    private FileRecoverViewModel fileRecoverViewModel;

    private FileRecoverAdapter fileRecoverAdapter;


    public static FileRecoverFragment newInstance(int stringId) {
        FileRecoverFragment fileRecoverFragment = new FileRecoverFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ID, stringId);
        fileRecoverFragment.setArguments(bundle);
        return fileRecoverFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initView();
        initObserver();
    }

    private void initView() {
        fileRecoverAdapter = new FileRecoverAdapter(this, recoverFragmentViewModel.getId());
        fileRecoverAdapter.setOnItemClick(file -> {
            fileRecoverViewModel.itemClick(file);
            EventHelper.report(EventId.EVENT_3000600);
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        binding.rvFileRecoverList.setLayoutManager(linearLayoutManager);
        binding.rvFileRecoverList.setAdapter(fileRecoverAdapter);
    }

    private void initObserver() {
        LiveData<List<FilesSearch.DocumentFile>> detectedList = null;
        if (recoverFragmentViewModel.getId() == R.string.word) {
            detectedList = fileRecoverViewModel.getDetectedWordList();
        } else if (recoverFragmentViewModel.getId() == R.string.excel) {
            detectedList = fileRecoverViewModel.getDetectedExcelList();
        } else if (recoverFragmentViewModel.getId() == R.string.ppt) {
            detectedList = fileRecoverViewModel.getDetectedPPTList();
        } else if (recoverFragmentViewModel.getId() == R.string.pdf) {
            detectedList = fileRecoverViewModel.getDetectedPDFList();
        }
        if (detectedList != null) {
            detectedList.observe(getViewLifecycleOwner(), list -> fileRecoverAdapter.submit(list));
        }
        fileRecoverViewModel.getScrollTop().observe(getViewLifecycleOwner(), o -> binding.rvFileRecoverList.scrollToPosition(0));
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        recoverFragmentViewModel = getViewModelProvider().get(FileRecoverFragmentViewModel.class);
        fileRecoverViewModel = getActivityViewModelProvider().get(FileRecoverViewModel.class);
    }

    private void initData() {
        Bundle arguments = getArguments();
        if (arguments == null) {
            return;
        }
        recoverFragmentViewModel.setId(arguments.getInt(ID));
    }


    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        immersionBar.statusBarDarkFont(true);
    }

}
