package com.daya.live_teaching.viewmodel;

import android.app.Application;

import com.daya.live_teaching.common.ResultCallback;
import com.daya.live_teaching.common.StateLiveData;
import com.daya.live_teaching.model.LoginResult;
import com.daya.live_teaching.model.RequestState;
import com.daya.live_teaching.repository.UserRepository;
import com.daya.live_teaching.rtc.RtcManager;
import com.daya.live_teaching.rtc.VideoResolution;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;


/**
 * 登录界面 视图模型
 */
public class LoginViewModel extends AndroidViewModel {
    private MutableLiveData<LoginResult> loginResultLiveData = new MutableLiveData<>();
    private UserRepository               userRepository;

    public LoginViewModel(@NonNull Application application) {
        super(application);
        userRepository = new UserRepository(application.getApplicationContext());
    }

    public LiveData<RequestState> login(String roomId, boolean isListener, String userName) {
        final StateLiveData stateLiveData = new StateLiveData();
        stateLiveData.loading();
        userRepository.login(roomId, isListener, userName, new ResultCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult result) {
                loginResultLiveData.postValue(result);
                stateLiveData.success();
        }

            @Override
            public void onFail(int errorCode, String errorStr) {
                stateLiveData.failed(errorCode);
            }
        });

        return stateLiveData;
    }

    public LiveData<LoginResult> getLoginResult() {
        return loginResultLiveData;
    }

    /**
     * 设置视频分辨率
     * @param resolution
     */
    public void setVideoResolution(VideoResolution resolution){
        RtcManager.getInstance().setVideoResolution(resolution);
    }

}
