package com.ez.ertc.meeting.base;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.utils.StatusBarUtil;
import com.ez.ertc.meeting.utils.UIUtil;
import com.ez.ertc.meeting.widget.WaitingDialog;

/**
 * @author xiezuoyuan
 * @time 2018/10/10 10:00
 */
public abstract class YsBaseActivity extends FragmentActivity {


    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    public static Activity TOP_ACTIVITY = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preOnCreate();
        if (provideLayoutId() != 0)
            setContentView(provideLayoutId());
        initView();
        initData();
    }

    protected abstract int provideLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected boolean registerEventBus() {
        return false;
    }

    protected void preOnCreate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            StatusBarUtil.setStatusBarColor(this, R.color.ezm_white);
        }
    }

    public void showFragment(int resId, Fragment fragment) {
        if (fragment == null) {
            return;
        }
        if (fragment.isVisible()) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(resId, fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    public void hideFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    /**
     * 显示等待框
     */
    public void showWaitingDialog() {
        showWaitingDialog(null, true);
    }

    public void showWaitingDialog(String text) {
        showWaitingDialog(text, true);
    }

    public void showWaitingDialog(String text, boolean cancelable) {
        WaitingDialog.showWaitingDialog(this, text, cancelable);
    }

    /**
     * 关闭等待框
     */
    public void dismissWaitingDialog() {
        WaitingDialog.dismissWaitingDialog(this);
    }

    public void showToast(@StringRes int stringId) {
        UIUtil.toast(getResources().getString(stringId));
    }

    public void showToast(String s) {
        UIUtil.toast(s);
    }

    /**
     * 设置android app 的字体大小不受系统字体大小改变的影响
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        TOP_ACTIVITY = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        TOP_ACTIVITY = null;
    }
}
