package core.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.hikvision.dmb.display.InfoDisplayApi;

import core.ui.common.AbsUIDelegate;
import core.ui.common.UIDelegateImpl;
import io.reactivex.rxjava3.disposables.Disposable;

public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity {

    private UIDelegateImpl uiDelegate;
    public VB binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (2 != InfoDisplayApi.getScreenRotate()) {
            InfoDisplayApi.setScreenRotate(2);
        }
        super.onCreate(savedInstanceState);
        uiDelegate = AbsUIDelegate.Companion.create();
        configImmersiveMode();
        binding = createViewBinding();
        setContentView(binding.getRoot());
        initViews(savedInstanceState);
        initListener();
        initData();
    }

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

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

    private void configImmersiveMode() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.WHITE);
    }

    protected abstract VB createViewBinding();

    protected void initViews(Bundle savedInstanceState) {

    }

    protected void initListener() {

    }

    protected void initData() {

    }


    public void showLoading(String message) {
        uiDelegate.showLoading(this, message, false);
    }

    public void dismissLoading() {
        uiDelegate.dismissLoading();
    }

    public void showToast(String message) {
        uiDelegate.showToast(this, message);
    }

    public void showNetError(Throwable throwable) {
        uiDelegate.showNetError(this, throwable);
    }

    public void addDisposable(Disposable disposable) {
        uiDelegate.addDisposable(disposable);
    }

    public void removeDisposable(Disposable disposable) {
        uiDelegate.removeDisposable(disposable);
    }

    public RecyclerView.ItemDecoration createItemDecoration() {
        return uiDelegate.createItemDecoration(this);
    }
}
