package com.datarecovery.master.module.example;

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

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

import com.atmob.app.lib.base.BaseFragment;
import com.datarecovery.master.data.api.bean.ExampleBean;
import com.datarecovery.master.databinding.FragmentExampleBinding;
import com.datarecovery.master.module.browser.BrowserActivity;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class ExampleFragment extends BaseFragment<FragmentExampleBinding> {


    private ExampleViewModel exampleViewModel;
    private ExampleAdapter exampleAdapter;

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

    private void initView() {
        exampleAdapter = new ExampleAdapter(this);
        binding.ryExample.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.ryExample.setAdapter(exampleAdapter);
        exampleAdapter.setOnItemClickListener(itemBean -> BrowserActivity.start(getActivity(), itemBean.getLink()));
    }

    private void initObserver() {
        exampleViewModel.getExampleList().observe(getViewLifecycleOwner(), list -> exampleAdapter.submit(list));
    }

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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        exampleViewModel = getViewModelProvider().get(ExampleViewModel.class);
        binding.setExampleViewModel(exampleViewModel);
    }
}
