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

import android.app.Dialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.utils.DarkModeUtils;
import com.gh.gamecenter.core.utils.ClickUtils;
import com.lightgame.utils.RuntimeUtils;
import com.lightgame.utils.Utils;

import java.lang.reflect.Field;

/**
 * @author CsHeng
 * @Date 17/05/2017
 * @Time 4:30 PM
 */

public class BaseDialogFragment extends DialogFragment {

    protected boolean mIsDarkModeOn;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIsDarkModeOn = DarkModeUtils.INSTANCE.isDarkModeOn(requireContext());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(), getThemeRes());
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener((dialog1, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && !ClickUtils.isFastDoubleClick()) {
                return onBack();
            }
            return false;
        });
        dialog.setCancelable(false);
        return dialog;
    }

    public int getThemeRes() {
        return R.style.DialogWindowTransparent;
    }

    public void toast(@StringRes int res) {
        if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
            toast(getString(res));
    }

    public void toast(String msg) {
        if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
            Utils.toast(getContext(), msg);
    }

    public void toastLong(@StringRes int msg) {
        toastLong(getString(msg));
    }

    public void toastLong(String msg) {
        RuntimeUtils.getInstance().toastLong(getContext(), msg);
    }

    public boolean onBack() {
        return false;
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        Fragment fragment = manager.findFragmentByTag(tag);
        if (fragment != null) {
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.show(fragment);
            transaction.commit();
        } else {
            try {
                Class<?> clazz = DialogFragment.class;
                Field dismissed = clazz.getDeclaredField("mDismissed");
                dismissed.setAccessible(true);
                dismissed.set(this, false);

                Field shownByMe = clazz.getDeclaredField("mShownByMe");
                shownByMe.setAccessible(true);
                shownByMe.set(this, true);
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.add(this, tag);
                transaction.commitAllowingStateLoss();
            } catch (Exception e) {
                super.show(manager, tag);
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        mIsDarkModeOn = DarkModeUtils.INSTANCE.isDarkModeOn(requireContext());
        onDarkModeChanged();
    }

    protected void onDarkModeChanged() {

    }
}



