package com.gh.gamecenter.fragment;

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

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.gh.common.constant.Config;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.base.fragment.LazyFragment;
import com.gh.gamecenter.common.base.fragment.ToolbarFragment;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.game.GameFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @author CsHeng
 */
public class SearchToolWrapperFragment extends LazyFragment {

    public final static String WRAPPER_FRAGMENT_NAME = "WrapperFragmentName";

    protected SearchToolbarFragment mSearchToolbarFragment;
    private Fragment mContentFragment;

    @Override
    protected void onRealLayoutInflated(@NotNull View inflatedView) {
        super.onRealLayoutInflated(inflatedView);

        Fragment toolbarFragment = getChildFragmentManager().findFragmentById(R.id.wrapper_toolbar);
        Fragment contentFragment = getChildFragmentManager().findFragmentById(R.id.wrapper_main_content);

        mSearchToolbarFragment = toolbarFragment != null ? ((SearchToolbarFragment) (toolbarFragment)) : new SearchToolbarFragment();
        setSearchHints();
        try {
            Bundle arguments = getArguments();
            if (arguments != null) {
                String className = Objects.requireNonNull(arguments.getString(WRAPPER_FRAGMENT_NAME));
                mContentFragment = contentFragment != null ? contentFragment : (Fragment) Class.forName(className).newInstance();

                Bundle bundle = new Bundle();
                if (GameFragment.class.getName().equals(className)) {
                    bundle.putString(EntranceConsts.KEY_LOCATION, "游戏库");
                }
                mSearchToolbarFragment.setArguments(bundle);
                Bundle mContentBundle = (Bundle) requireArguments().clone();
                mContentFragment.setArguments(mContentBundle);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        getChildFragmentManager().beginTransaction()
                .replace(R.id.wrapper_toolbar, mSearchToolbarFragment)
                .replace(R.id.wrapper_main_content, Objects.requireNonNull(mContentFragment)).commitAllowingStateLoss();

        // 游戏库更新搜索栏底色
//        mCachedView.post(() -> {
//            mSearchToolbarFragment.updateStyle(mNightMode);
//            mSearchToolbarFragment.updateSearchToolbarColor(ContextCompat.getColor(requireContext(), R.color.background_white));
//        });

    }

    @Override
    protected int getRealLayoutId() {
        return R.layout.fragment_wrapper_toolbar;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBReuse reuse) {
        if ("Refresh".equals(reuse.getType())) {
            setSearchHints();
        }
    }

    protected void setSearchHints() {
        if (mSearchToolbarFragment == null) return;

        if (Config.getSettings() != null
                && Config.getSettings().getSearch() != null
                && Config.getSettings().getSearch().getDefaultData() != null
                && Config.getSettings().getSearch().getDefaultData().size() > 0) {
            mSearchToolbarFragment.setHint(new ArrayList<>(Config.getSettings().getSearch().getDefaultData()));
        }
    }

    @Override
    public void onFragmentResume() {
        super.onFragmentResume();

        DisplayUtils.setLightStatusBar(requireActivity(), !mIsDarkModeOn);
    }

    @Override
    public boolean onBackPressed() {
        if (mContentFragment instanceof ToolbarFragment) {
            return ((ToolbarFragment) mContentFragment).onBackPressed();
        }
        return super.onBackPressed();
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        if (mSearchToolbarFragment != null) {
            if (isSupportVisible()) DisplayUtils.setLightStatusBar(requireActivity(), !mIsDarkModeOn);
            View statusBar = getView().findViewById(R.id.statusBar);
            if (statusBar != null) {
                statusBar.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white));
            }
        }
    }
}