package com.gh.gamecenter.common.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.base.adapter.FragmentAdapter;
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout;
import com.gh.gamecenter.common.view.TabIndicatorView;
import com.google.android.material.tabs.TabLayout;
import com.lightgame.view.NoScrollableViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by khy on 15/03/18.
 */

public abstract class BaseActivity_TabLayout extends ToolBarActivity implements ViewPager.OnPageChangeListener {

    public static final String PAGE_INDEX = "PAGE_INDEX";

    protected TabLayout mTabLayout;
    protected NoScrollableViewPager mViewPager;
    protected TabIndicatorView mTabIndicatorView;
    protected View mDividerLineView;

    protected List<Fragment> mFragmentsList;

    protected List<String> mTabTitleList;

    protected int mCheckedIndex = 0;

    protected abstract void initFragmentList(List<Fragment> fragments);

    protected abstract void initTabTitleList(List<String> tabTitleList);

    protected int provideIndicatorWidth() {
        return 20;
    }

    protected View provideTabView(int position, String tabTitle) {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tablayout_viewpager;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTabLayout = findViewById(R.id.activity_tab_layout);
        mViewPager = findViewById(R.id.activity_view_pager);
        mTabIndicatorView = findViewById(R.id.activity_tab_indicator);
        mDividerLineView = findViewById(R.id.dividerLine);

        if (getIntent() != null) mCheckedIndex = getIntent().getIntExtra(PAGE_INDEX, 0);
        mTabTitleList = new ArrayList<>();
        initTabTitleList(mTabTitleList);
        mFragmentsList = new ArrayList<>(restoreFragments());
        if (mFragmentsList.isEmpty() || mFragmentsList.size() != mTabTitleList.size()) {
            mFragmentsList.clear();
            initFragmentList(mFragmentsList);
        }

        mViewPager.setOffscreenPageLimit(mFragmentsList.size());
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), mFragmentsList, mTabTitleList));
        mViewPager.setCurrentItem(mCheckedIndex);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabIndicatorView.setupWithTabLayout(mTabLayout);
        mTabIndicatorView.setupWithViewPager(mViewPager);
        mTabIndicatorView.setIndicatorWidth(provideIndicatorWidth());

        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab == null) continue;
            String tabTitle = tab.getText() != null ? tab.getText().toString() : "";
            View tabView = provideTabView(i, tabTitle);
            if (tabView == null)
                tabView = BaseFragment_TabLayout.createDefaultTabCustomView(this, tabTitle);
            tab.setCustomView(tabView);
        }

        BaseFragment_TabLayout.initTabStyle(mTabLayout, mCheckedIndex);
    }

    private ArrayList<Fragment> restoreFragments() {
        String tag = "android:switcher:" + mViewPager.getId() + ":";
        ArrayList<Fragment> fragments = new ArrayList<>();
        int childCount = mTabTitleList.size();
        for (int index = 0; index < childCount; index++) {
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag + index);
            if (fragment != null) {
                fragments.add(fragment);
            }
        }

        return fragments;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        View container = findViewById(R.id.activity_tab_container);
        if (container != null) {
            container.setBackgroundColor(ContextCompat.getColor(this, R.color.background_white));
            for (int i = 0; i < mTabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = mTabLayout.getTabAt(i);
                if (tab != null) {
                    BaseFragment_TabLayout.updateTabStyle(tab, tab.isSelected());
                }
            }
        }
        if (mDividerLineView != null) {
            mDividerLineView.setBackgroundColor(ContextCompat.getColor(this, R.color.divider));
        }
    }
}
