package com.phlx.anchorcollect.ui.splash;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.R;
import com.phlx.anchorcollect.data.VMFactory;
import com.phlx.anchorcollect.databinding.ActivitySplashBinding;
import com.phlx.anchorcollect.db.DbUtil;
import com.phlx.anchorcollect.ui.base.BaseActivity;

import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, SplashVM> {

    private int loginType;//登录后是否跳转，0跳转main 1不跳转

    @Override
    public int initContentView(Bundle bundle) {
        return R.layout.activity_splash;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();

        loginType = getIntent().getIntExtra("loginType", 0);
    }

    @Override
    public SplashVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SplashVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        showDialog("正在同步基础数据，请稍后");

        viewModel.loginType = loginType;
        viewModel.requestDict();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.errorEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                showTip(s);
            }
        });
    }

    private void showTip(String content) {
        new MaterialDialog.Builder(this).title("系统信息").content(content)
                .cancelable(false).canceledOnTouchOutside(false)
                .positiveText("退出").onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.POSITIVE) {
                    dialog.dismiss();
                    finish();
                }
            }
        }).show();
    }

    @Override
    public void onBackPressed() {

    }
}
