package com.atmob.voiceai.module.main;

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


import com.atmob.voiceai.R;
import com.atmob.voiceai.module.clonevoice.CloneVoiceFragment;
import com.atmob.voiceai.module.history.HistoryFragment;
import com.atmob.voiceai.module.voiceai.VoiceAIFragment;

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_voice_ai, R.drawable.selector_icon_main_tab_voice, VoiceAIFragment.class),
            new MainPagerItem(R.string.main_pager_clone_voice, R.drawable.selector_icon_main_tab_clone_voice, CloneVoiceFragment.class),
            new MainPagerItem(R.string.main_pager_history, R.drawable.selector_icon_main_tab_hirstory, HistoryFragment.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;
        }
    }
}