package com.atmob.voiceai.module.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

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

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.voiceai.databinding.ActivityMainBinding;
import com.atmob.voiceai.databinding.ItemMainTabLayoutBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class MainActivity extends BaseActivity<ActivityMainBinding> {


    private MainViewModel mainViewModel;
    private static final String TO_TAB_FRAGMENT = "to_tab_fragment";
    private TabLayoutMediator tabLayoutMediator;
    private MainPagerAdapter mainPagerAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    public static void start(Context context, Class<? extends Fragment> cls) {
        Intent intent = new Intent(context, MainActivity.class);
        if (!(context instanceof android.app.Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (cls != null) {
            intent.putExtra(TO_TAB_FRAGMENT, cls);
        }
        context.startActivity(intent);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            Class<? extends Fragment> cls = (Class<? extends Fragment>) intent.getSerializableExtra(TO_TAB_FRAGMENT);
            if (cls != null) {
                binding.mainTabLayout.selectTab(binding.mainTabLayout.getTabAt(mainPagerAdapter.getPosition(cls)));
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initObserver();
    }

    private void initObserver() {

    }


    private void initView() {
        initViewPager();
        initTabLayout();
    }

    private void initViewPager() {
        mainPagerAdapter = new MainPagerAdapter(this);
        binding.mainViewPager.setAdapter(mainPagerAdapter);
        binding.mainViewPager.setUserInputEnabled(false);
        binding.mainViewPager.setOffscreenPageLimit(mainPagerAdapter.getItemCount());
    }


    private void initTabLayout() {
        binding.mainTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ItemMainTabLayoutBinding itemBinding = (ItemMainTabLayoutBinding) tab.getTag();
                if (itemBinding != null) {
                    itemBinding.tabText.setSelected(true);
                    itemBinding.tabIcon.setSelected(true);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ItemMainTabLayoutBinding itemBinding = (ItemMainTabLayoutBinding) tab.getTag();
                if (itemBinding != null) {
                    itemBinding.tabText.setSelected(false);
                    itemBinding.tabIcon.setSelected(false);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tabLayoutMediator = new TabLayoutMediator(binding.mainTabLayout, binding.mainViewPager, true, false, (tab, position) -> {
            MainPagerAdapter.MainPagerItem mainPagerItem = mainPagerAdapter.getMainPagerItemByPosition(position);
            ItemMainTabLayoutBinding itemBinding = ItemMainTabLayoutBinding.inflate(getLayoutInflater());
            itemBinding.tabText.setText(mainPagerItem.getTabName());
            itemBinding.tabIcon.setImageResource(mainPagerItem.getTabIcon());
            tab.setCustomView(itemBinding.getRoot());
            tab.setTag(itemBinding);
        });
        tabLayoutMediator.attach();
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        mainViewModel = getViewModelProvider().get(MainViewModel.class);
    }

    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        super.configImmersion(immersionBar);
        immersionBar.statusBarDarkFont(false);
    }

    @Override
    protected boolean shouldImmersion() {
        return true;
    }
}
