package com.datarecovery.master.module.main;

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

import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.datarecovery.master.R;
import com.datarecovery.master.databinding.ActivityMainBinding;
import com.datarecovery.master.databinding.ItemMainTabLayoutBinding;
import com.datarecovery.master.utils.ToastUtil;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class MainActivity extends BaseActivity<ActivityMainBinding> {


    private MainViewModel mainViewModel;

    private TabLayoutMediator tabLayoutMediator;
    private MainPagerAdapter mainPagerAdapter;
    private long mExitTime;

    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);
    }


    @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);
                }
//                Class<? extends Fragment> pagerClass = mainPagerAdapter.getMainPagerItemByPosition(tab.getPosition()).getPagerClass();
//                if (pagerClass.equals(FriendsFragment.class)) {
//
//                } else if (pagerClass.equals(LocationFragment.class)) {
//
//                } else if (pagerClass.equals(MessageFragment.class)) {
//
//                } else if (pagerClass.equals(MineFragment.class)) {
//
//                }
            }

            @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 onDestroy() {
        super.onDestroy();
        if (tabLayoutMediator != null) {
            tabLayoutMediator.detach();
        }
    }

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            ToastUtil.show(R.string.exit_application_text, ToastUtil.LENGTH_LONG);
            mExitTime = System.currentTimeMillis();
        } else {
            try {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            } catch (Exception ignore) {
            }
        }
    }

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