package com.example.iot_controlhost.adapter;

import android.util.SparseArray;

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

import com.example.iot_controlhost.base.BaseMode;
import com.example.iot_controlhost.ui.fragment.AutoFragment;
import com.example.iot_controlhost.ui.fragment.IntelligenceFragment;
import com.example.iot_controlhost.ui.fragment.ManualFragment;

/**
 * @Description: 主界面三大模式切换-适配器
 * @Author：DuanKaiji
 */
public class ModelsAdapter extends FragmentStateAdapter {
    private static final SparseArray<BaseMode> fragments = new SparseArray<>();

    public ModelsAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        BaseMode fragment = null;
        switch (position) {
            case 0:
                fragment = new ManualFragment();
                break;
            case 1:
                fragment = new AutoFragment();
                break;
            case 2:
                fragment = new IntelligenceFragment();
                break;
        }
        fragments.put(position, fragment);
        return (Fragment) fragment;
    }

    public static BaseMode getModeByPosition(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}
