package com.datarecovery.master.module.order;

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

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

import com.atmob.app.lib.base.BaseFragment;
import com.datarecovery.master.databinding.FragmentOrderBinding;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class OrderFragment extends BaseFragment<FragmentOrderBinding> {


    private OrderViewModel orderViewModel;

    private OrderItemAdapter orderItemAdapter;


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

    private void initView() {
        orderItemAdapter = new OrderItemAdapter(getViewLifecycleOwner());
        binding.ryOrder.setAdapter(orderItemAdapter);
        binding.ryOrder.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.swiperLayout.setOnRefreshListener(() -> orderViewModel.refreshOrderPageList());
    }

    private void initObserver() {
        orderViewModel.getOrderList().observe(getViewLifecycleOwner(), list -> orderItemAdapter.submit(list));
        orderViewModel.getRefreshOrderListEvent().observe(getViewLifecycleOwner(), o -> binding.swiperLayout.setRefreshing(false));
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        orderViewModel = getViewModelProvider().get(OrderViewModel.class);
        binding.setOrderViewModel(orderViewModel);
    }

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