package xn.hxp.ui.plan.change;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

import xn.hxp.databinding.FragmentChangeCabinetBinding;
import xn.hxp.databinding.IncludeCabinetBinding;
import xn.hxp.ui.plan.FragmentCallBack;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;

public class ChangeCabinetFragment extends Fragment {
    private List<HxpCabinetVo> hxpCabinetVoList;
    private FragmentCallBack fragmentCallBack;
    private FragmentChangeCabinetBinding binding;
    private List<ChangeCabinetHelp> changeCabinetHelpList = new ArrayList<>();
    private List<IncludeCabinetBinding> includeCabinetBindingList = new ArrayList<>();

    public ChangeCabinetFragment(List<HxpCabinetVo> hxpCabinetVos, FragmentCallBack fragmentCallBack) {
        this.hxpCabinetVoList = hxpCabinetVos;
        this.fragmentCallBack = fragmentCallBack;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentChangeCabinetBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

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

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

    private void updateUi() {
        changeCabinetHelpList.clear();
        includeCabinetBindingList.clear();
        binding.main.removeAllViewsInLayout();
        SingleSelectCallBack singleSelectCallBack = v -> {
            for (int i = 0; i < changeCabinetHelpList.size(); i++) {
                ChangeCabinetHelp changeCabinetHelp = changeCabinetHelpList.get(i);
                changeCabinetHelp.updateSelect(v);
            }
        };
        for (int i = 0; i < hxpCabinetVoList.size(); i++) {
            HxpCabinetVo hxpCabinetVo = hxpCabinetVoList.get(i);
            IncludeCabinetBinding includeCabinetBinding = IncludeCabinetBinding.inflate(getLayoutInflater());
            includeCabinetBindingList.add(includeCabinetBinding);
            changeCabinetHelpList.add(new ChangeCabinetHelp(includeCabinetBinding, hxpCabinetVo, fragmentCallBack, singleSelectCallBack));
            binding.main.addView(includeCabinetBinding.getRoot());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        updateUi();
    }

}
