package com.ym.modulecommon.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.Nullable;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;
import com.ym.modulecommon.AppLibraryConfig;
import com.ym.admodule.R;
import com.ym.modulecommon.AdConstant;
import com.ym.modulecommon.module.ReportEntity;
import com.ym.modulecommon.utils.BaseEventUtils;
import com.ym.modulecommon.utils.LogUtils;
import com.ym.modulecommon.utils.StatusBarUtil;
import com.ym.modulecommon.utils.Utils;

/**
 * Created by gaoleichao on 2019/4/17
 */
public abstract class BaseActivity extends RxAppCompatActivity {

    public abstract int layoutID();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppLibraryConfig.ActivityConfig.isProhibitLandscape) {
            try {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 禁用横屏
            }catch (Exception e){
            }
        }
        if (AppLibraryConfig.ActivityConfig.isProhibitScreenshot) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);//禁止截屏
        }
        if (AppLibraryConfig.ActivityConfig.isKeepScreenLight) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//屏幕常亮
        }
        before();
        if (layoutID() != 0) {
            setContentView(layoutID());
        }
        init(savedInstanceState);
        init();
        data();
    }

    public void before() {
        StatusBarUtil.transportStatus(this, getResources().getColor(R.color.transparent));
//        baseHandler = new WeakHandler();
//        StatusBarUtil.setTranslucent(this);
    }


    public abstract void init();

    public void data() {

    }

    public void init(@Nullable Bundle savedInstanceState) {

    }

    //    protected WeakHandler baseHandler;
    protected boolean mIsNeedGoneNavigationBar = false;


    /**
     * 设置是否隐藏导航栏
     *
     * @param isNeed
     */
    protected void setNeedGoneNavigationBar(boolean isNeed) {

    }

    public void openActivity(Class pClass) {
        openActivity(pClass, null);
    }

    public void openActivity(Class pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    /**
     * 设置屏幕亮度
     *
     * @param context
     * @param brightness 1-255
     */
    public void changeAppBrightness(Context context, int brightness) {
        Window window = ((Activity) context).getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
           LogUtils.d("glc", "brightness:" + brightness);
            lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
            ;
        }
        window.setAttributes(lp);
    }

    public int getAppBrightness() {
        int systemBrightness = 0;
        try {
            systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return systemBrightness;
    }

    // 隐藏键盘
    public void hideKeyBoard() {
        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 显示键盘
    public void showKeyBoard(View view) {
        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(view, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideNavigationBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // 全屏显示，隐藏状态栏和导航栏，拉出状态栏和导航栏显示一会儿后消失。
                getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            } else {
                // 全屏显示，隐藏状态栏
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        }
    }

    private String className() {
        return this.getClass().getSimpleName();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (className() != null) {
            BaseEventUtils.INSTANCE.onEvent("page_status", Utils.obj2Str(new ReportEntity(AdConstant.param.ReportSessionId, className(), "onResume")));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (className() != null) {
            BaseEventUtils.INSTANCE.onEvent("page_status", Utils.obj2Str(new ReportEntity(AdConstant.param.ReportSessionId, className(), "onPause")));
        }
        MobclickAgent.onPause(this);
    }

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

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }
}
