package com.free.video.wallpaper.baseapp;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.lifecycle.ViewModel;
import androidx.viewbinding.ViewBinding;

import com.free.video.wallpaper.databinding.BaseActivityBinding;
import com.free.video.wallpaper.vmhelper.MyScope;


public abstract class MyBaseActivity<T extends ViewBinding> extends AppCompatActivity implements View.OnClickListener {

    private final MyScope mMyScope = new MyScope();

    protected T vb;

    private Window window;
    private View decorView;
    protected View mView;
    private BaseActivityBinding rootVb;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = getViewBinding();
        View root = vb.getRoot();
        rootVb = BaseActivityBinding.inflate(getLayoutInflater(), null, false);
        rootVb.frameLayout.addView(root);
        setContentView(rootVb.getRoot());
        window = getWindow();
        decorView = window.getDecorView();
        mView = decorView.getRootView();

        setStatusBar();
        if (isFullScreen()) {
            initFullScreen();
        }
        onCreateInit();
        onInitClick();
    }

    protected abstract T getViewBinding();


    protected abstract void onCreateInit();

    protected abstract void onInitClick();

    public abstract boolean isFullScreen();

    public abstract boolean statusBarLight();


    private void setStatusBar() {
        //深色模式
        WindowInsetsControllerCompat insetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
        insetsController.setAppearanceLightStatusBars(statusBarLight());
    }

    private void initFullScreen() {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        mView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    protected <K extends ViewModel> K getActivityScopeViewModel(@NonNull Class<K> modelClass) {
        return mMyScope.getActivityScopeViewModel(this, modelClass);
    }

    protected <K extends ViewModel> K getApplicationScopeViewModel(@NonNull Class<K> modelClass) {
        return mMyScope.getApplicationScopeViewModel(modelClass);
    }


}