package com.hainaos.vc.base.mvvm;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.hainaos.vc.base.BaseTransparentActivity;

import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;

public abstract class BaseMvvmActivity<VM extends ViewModel, VDB extends ViewDataBinding> extends BaseTransparentActivity {

    private static final String TAG = "BaseMvvmActivity";

    protected VM mViewModel;
    protected VDB mViewDataBinding;
    protected Class<VM> vmClass;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //ViewDataBinding
        mViewDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mViewDataBinding.setLifecycleOwner(this);
        //ViewModel
        vmClass = (Class<VM>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        boolean isAbstract = Modifier.isAbstract(vmClass.getModifiers());
        Log.e(TAG, "isLocalClass:" + vmClass.getSimpleName().equals(ViewModel.class.getSimpleName()) + "   isAbstract:" + isAbstract);
        if (!isAbstract) {//不是一个抽象类
            mViewModel = new ViewModelProvider(this).get(vmClass);
        }
        initDataBinding();
        initView();
        initData();
    }

    protected abstract void initDataBinding();

    /**
     * 初始化视图
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();
}
