package com.ym.library.net;

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.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;

import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.OnNavigationBarListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;
import com.ym.library.Constant;
import com.ym.library.module.ReportEntity;
import com.ym.library.rxbus.RxBusUtil;
import com.ym.library.utils.EventUtils;
import com.ym.library.utils.Utils;

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

    public abstract int layoutID();
//    public abstract Context context();

    public  Activity activity(){
        return this;
    }
    public  String className() {
        return activity().getClass().getSimpleName();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 禁用横屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        RxBusUtil.getDefault().register(this);
        before();
        if (layoutID() != 0) {
            setContentView(layoutID());
        }
        after();
        init(savedInstanceState);
        init();
        data();

    }

    public void before() {
//        baseHandler = new WeakHandler();
//        StatusBarUtil.setTranslucent(this);
    }

    protected void after() {
        ImmersionBar.with(this).setOnNavigationBarListener(new OnNavigationBarListener() {
            @Override
            public void onNavigationBarChange(boolean show) {

            }
        }).init();
    }

    public abstract void init();

    public void data() {

    }

    public void init(@Nullable Bundle savedInstanceState) {

    }

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

    @Override
    protected void onResume() {
        super.onResume();
        if (activity() != null) {
            MobclickAgent.onResume(activity());
        }
        if (className() != null) {
            Log.d("BaseActivity","onResume =" + className());
            EventUtils.INSTANCE.onEvent("page_status", Utils.obj2Str(new ReportEntity(Constant.ReportSessionId, className(), "onResume")));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (className() != null) {
            Log.d("BaseActivity","onPause =" + className());
            EventUtils.INSTANCE.onEvent("page_status", Utils.obj2Str(new ReportEntity(Constant.ReportSessionId, className(), "onPause")));
        }
        if (activity() != null) {
            MobclickAgent.onPause(activity());
        }
    }


    /**
     * 设置是否隐藏导航栏
     *
     * @param isNeed
     */
    protected void setNeedGoneNavigationBar(boolean isNeed) {
//        mIsNeedGoneNavigationBar = isNeed;
//        if (mIsNeedGoneNavigationBar) {
//            baseHandler.postDelayed(mHideRunnable, 200);
//            final View decorView = getWindow().getDecorView();
//            decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
//                @Override
//                public void onSystemUiVisibilityChange(int visibility) {
//                    decorView.requestLayout();
//
//                }
//            });
//        }
    }

//    protected Runnable mHideRunnable = new Runnable() {
//        @Override
//        public void run() {
//            int flags;
//            int curApiVersion = Build.VERSION.SDK_INT;
//            // This work only for android 4.4+
//            if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
//                // This work only for android 4.4+
//                // hide navigation bar permanently in android activity
//                // touch the screen, the navigation bar will not show
//                flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
////                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
//
//            } else {
//                // touch the screen, the navigation bar will show
//                flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
//            }
//
//            // must be executed in main thread :)
//            getWindow().getDecorView().setSystemUiVisibility(flags);
//        }
//    };

    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 {
            Log.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);
            }
        }
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(context());
//    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        Glide.get(AppliContext.get()).clearMemory();
        RxBusUtil.getDefault().unregister(this);
    }

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