package com.gh.gamecenter.login.user;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.gh.gamecenter.common.retrofit.ApiResponse;
import com.gh.gamecenter.login.entity.UserInfoEntity;

import org.json.JSONObject;

/**
 * Created by khy on 28/11/17.
 */

public class UserViewModel extends AndroidViewModel {

    public static final String KEY_EDIT_TYPE = "editType";

    /**
     * value 用于传递给api，请勿修改。
     */
    public static final String TYPE_GENDER = "gender";
    public static final String TYPE_NAME = "name";
    public static final String TYPE_CONTACT = "contact";
    public static final String TYPE_QQ = "qq";
    public static final String TYPE_MOBILE = "mobile";
    public static final String TYPE_PORTRAIT = "icon";
    public static final String TYPE_REGION = "region";
    public static final String TYPE_ID_CARD = "id_card";
    public static final String TYPE_INTRODUCE = "introduce";
    public static final String TYPE_BACKGROUND = "background";
    public static final String TYPE_ICON_BORDER = "icon_border";

    private UserRepository mUserRepository;

    private final LiveData<ApiResponse<UserInfoEntity>> mLoginLiveUserInfo;
    private final LiveData<ApiResponse<UserInfoEntity>> mEditLiveUserInfo;
    private final MediatorLiveData<Boolean> mUploadBackground;
    private final MediatorLiveData<Boolean> mUploadAvatarBorder;

    public UserViewModel(@NonNull Application application, UserRepository repository) {
        super(application);
        mUserRepository = repository;
        mLoginLiveUserInfo = repository.getLoginUserInfo();
        mEditLiveUserInfo = repository.getEditObsUserInfo();
        mUploadBackground = repository.getUploadBackgroundLiveData();
        mUploadAvatarBorder = repository.getUploadAvatarBorderLiveData();
    }

    public void retryCheckLogin() {
        mUserRepository.checkLogin();
    }

    public LiveData<ApiResponse<UserInfoEntity>> getLoginObsUserinfo() {
        return mLoginLiveUserInfo;
    }

    public LiveData<ApiResponse<UserInfoEntity>> getEditObsUserinfo() {
        return mEditLiveUserInfo;
    }

    public MediatorLiveData<Boolean> getUploadBackground() {
        return mUploadBackground;
    }

    public MediatorLiveData<Boolean> getUploadAvatarBorder() {
        return mUploadAvatarBorder;
    }

    public void login(JSONObject content, LoginTag loginTag) {
        mUserRepository.login(content, loginTag);
    }

    public void changeUserInfo(String content, String type) {
        changeUserInfo(content, type, false);
    }

    public void changeUserInfo(String content, String type, Boolean isForcedToCertificate) {
        mUserRepository.changeUserInfo(content, type, isForcedToCertificate);
    }

    // 用EventBus通知退出登录会存在事件丢失问题(页面没创建),暂时直接调UserRepository做退出登录处理
    @Deprecated
    public void logout() {
        mUserRepository.logout();
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        @NonNull
        private final Application mApplication;

        private final UserRepository mRepository;

        public Factory(@NonNull Application application) {
            mApplication = application;
            mRepository = UserRepository.getInstance();
        }

        @Override
        public <T extends ViewModel> T create(Class<T> modelClass) {
            //noinspection unchecked
            return (T) new UserViewModel(mApplication, mRepository);
        }
    }
}
