package com.phlx.wool.ui.base;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;


import com.phlx.wool.Configs;
import com.phlx.wool.ui.login.LoginActivity;
import com.phlx.wool.ui.setting.SettingActivity;

import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * Create Date：2019/06/03
 */

public class MainBarVM<M extends BaseModel> extends BaseViewModel<M> {
    //标题文字
    public ObservableField<String> mainTitle = new ObservableField<>("");
    public MainBarVM mainBarVM;
    /**
     * drawerLayout
     */
    public ObservableField<String> userName = new ObservableField<>("");
    public ObservableField<String> userEmail = new ObservableField<>("");
    public ObservableField<String> userPhone = new ObservableField<>("");
    public ObservableField<String> userBreed = new ObservableField<>("");
    public ObservableField<String> version = new ObservableField<>("");

    public MainBarVM(@NonNull Application application) {
        this(application, null);
        mainBarVM = this;
    }

    public MainBarVM(@NonNull Application application, M model) {
        super(application, model);
        mainBarVM = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initUserInfo();

    }

    /**
     * 创建DrawerLayout时，初始化用户信息
     */
    private void initUserInfo() {
        String person_name = SPUtils.getInstance().getString(Configs.SP_NAME);
        if (StringUtils.isEmpty(person_name)) {
            userName.set(SPUtils.getInstance().getString(Configs.SP_USER_NAME));
        } else {
            userName.set(person_name);
        }
        userEmail.set(SPUtils.getInstance().getString(Configs.SP_USER_EMAIL));
        userPhone.set(SPUtils.getInstance().getString(Configs.SP_USER_PHONE));
        userBreed.set(SPUtils.getInstance().getString(Configs.SP_USER_UNIT));
        version.set(Configs.packageName(getApplication()));
    }

    /**
     * 设置标题
     *
     * @param text 标题文字
     */
    protected void setMainTitleText(String text) {
        mainTitle.set(text);
    }


    //ToolBar左边按钮点击事件
    public final BindingCommand mainOnClick = new BindingCommand(this::mainOnClick);


    public BindingCommand mainRightIconOnClick = new BindingCommand(() -> {
        startActivity(SettingActivity.class);
    });


    //注销账号
    public BindingCommand logoutOnClick = new BindingCommand(() -> {
        SPUtils.getInstance().put(Configs.SP_USER_NAME, "");
        SPUtils.getInstance().put(Configs.SP_USER_PASSWORD, "");
        startActivity(LoginActivity.class);
        finish();
    });

    /**
     * 左边按钮的点击事件，子类可重写
     */
    protected void mainOnClick() {
    }


}
