package com.casic.dcms.view;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.databinding.ActivitySplashBinding;
import com.casic.dcms.model.UserBean;
import com.casic.dcms.vm.UserViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.ContextKit;

public class SplashScreenActivity extends AndroidxBaseActivity<ActivitySplashBinding> {

    private final Context context = SplashScreenActivity.this;
    private UserViewModel userViewModel;
    private final CountDownTimer countDownTimer = new CountDownTimer(1000, 500) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            userViewModel.getUserData(context);
        }
    };

    @Override
    protected void setupTopBarLayout() {
        ImmersionBar.with(this).init();
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
        userViewModel.userDataResult.observe(this, new Observer<UserBean>() {
            @Override
            public void onChanged(UserBean userBean) {
                if (userBean.getCode() == 200) {
                    ContextKit.navigatePageTo(context, MainActivity.class);
                } else {
                    ContextKit.navigatePageTo(context, LoginActivity.class);
                }
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {
        countDownTimer.start();
    }
}
