package xn.hxp.ui.plan.change;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.blankj.utilcode.util.LogUtils;
import xn.hxp.ui.plan.FragmentCallBack;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 双显示化学品柜
 */
public class ChangeCabinetAdapter extends FragmentStateAdapter {
    private final List<Fragment> fragments = new LinkedList<>();

    public ChangeCabinetAdapter(@NonNull FragmentActivity fragmentActivity, List<HxpCabinetVo> hxpCabinetVoList, FragmentCallBack fragmentCallBack) {
        super(fragmentActivity);
        int size = (int) Math.ceil(hxpCabinetVoList.size() / 2.0);
        Map<Integer, List<HxpCabinetVo>> doubleCabinetMap = new TreeMap<>();
        int index = 0;
        for (int i = 0; i < size; i++) {
            List<HxpCabinetVo> hxpCabinetVoArrayList = new ArrayList<>();
            hxpCabinetVoArrayList.add(hxpCabinetVoList.get(index));
            index++;
            if (index < hxpCabinetVoList.size()) {
                hxpCabinetVoArrayList.add(hxpCabinetVoList.get(index));
                index++;
            }
            doubleCabinetMap.put(i, hxpCabinetVoArrayList);
        }
        for (int i = 0; i < size; i++) {
            fragments.add(new ChangeCabinetFragment(doubleCabinetMap.get(i), fragmentCallBack));
        }
    }

    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }

    @Override
    public boolean containsItem(long itemId) {
        return false;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

}
