package com.datarecovery.master.module.main;

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

import com.datarecovery.master.R;
import com.datarecovery.master.module.example.ExampleFragment;
import com.datarecovery.master.module.homepage.HomePageFragment;
import com.datarecovery.master.module.mine.MineFragment;

import java.util.Arrays;
import java.util.List;

public class MainPagerAdapter extends FragmentStateAdapter {

    private final List<MainPagerItem> mainPagerItems = Arrays.asList(
            new MainPagerItem(R.string.main_pager_front_page, R.drawable.selector_icon_main_tab_home_page, HomePageFragment.class),
            new MainPagerItem(R.string.main_pager_example, R.drawable.selector_icon_main_tab_example, ExampleFragment.class),
            new MainPagerItem(R.string.main_pager_mine, R.drawable.selector_icon_main_tab_mine, MineFragment.class)
    );

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

    public int getPosition(Class<? extends Fragment> cls) {
        if (mainPagerItems == null || mainPagerItems.isEmpty()) {
            return 0;
        }
        for (int i = 0; i < mainPagerItems.size(); i++) {
            MainPagerItem mainPagerItem = mainPagerItems.get(i);
            if (mainPagerItem.pagerClass == cls) {
                return i;
            }
        }
        return 0;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        MainPagerItem mainPagerItem = mainPagerItems.get(position);
        Class<? extends Fragment> pagerClass = mainPagerItem.getPagerClass();
        return createFragmentByClass(pagerClass);
    }

    private Fragment createFragmentByClass(Class<? extends Fragment> pagerClass) {
        try {
            return pagerClass.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();
    }

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

    public MainPagerItem getMainPagerItemByPosition(int position) {
        if (position < 0 || position >= mainPagerItems.size()) {
            return null;
        }
        return mainPagerItems.get(position);
    }

    public static class MainPagerItem {
        private final int tabName;
        private final int tabIcon;
        private final Class<? extends Fragment> pagerClass;

        public MainPagerItem(int tabName, int tabIcon, Class<? extends Fragment> pagerClass) {
            this.tabName = tabName;
            this.tabIcon = tabIcon;
            this.pagerClass = pagerClass;
        }

        public int getTabName() {
            return tabName;
        }

        public int getTabIcon() {
            return tabIcon;
        }

        public Class<? extends Fragment> getPagerClass() {
            return pagerClass;
        }
    }
}