package com.dskj.trustlend.home;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.azhon.basic.lifecycle.BaseViewModel;
import com.dskj.trustlend.InitApp;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.ErrorMesage;
import com.dskj.trustlend.bean.TokenBean;
import com.dskj.trustlend.bean.UserBean;
import com.dskj.trustlend.bean.VideoIndexBean;
import com.dskj.trustlend.bean.VideoIndexResult;
import com.dskj.trustlend.bean.VideoResult;
import com.dskj.trustlend.utils.GetAndroidUniqueMark;
import com.dskj.trustlend.utils.LogUtils;
import com.dskj.trustlend.utils.NetUtil;

import java.util.List;
import java.util.Random;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainViewModel extends BaseViewModel {
    protected MutableLiveData<TokenBean> tokenBeanMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<TokenBean> getTokenBeanMutableLiveData() {
        return tokenBeanMutableLiveData;
    }

    protected MutableLiveData<UserBean> userBeanMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<UserBean> getUserBeanMutableLiveData() {
        return userBeanMutableLiveData;
    }

    private MutableLiveData<ErrorMesage> errorMesageMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<ErrorMesage> getErrorMesageMutableLiveData() {
        return errorMesageMutableLiveData;
    }
    /**
     * getAccessToken
     *
     * @param
     */
    public void getAccessToken() {
        String device_id = GetAndroidUniqueMark.getUniqueId(InitApp.getAppContext());
        String app_id = "38923465";
        String rand_str =( new Random().nextInt(900000)+100000)+"";
//        long timestamps = (System.currentTimeMillis()/1000);
        String signature = NetUtil.getRequestSign(rand_str);
        LogUtils.i("签名数据："+signature);
        Api.getInstance().getAccessToken(app_id, signature, rand_str)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<TokenBean>>() {

                    @Override
                    public void onSuccess(Result<TokenBean> feedbackResp) {
                        getTokenBeanMutableLiveData().postValue(feedbackResp.data);

                    }
                    @Override
                    public void onError(int code, String msg) {
                        getTokenBeanMutableLiveData().postValue(new TokenBean(code+"-"+msg,-1000));
                    }
                });
    }


    /**
     * 注册
     *
     * @param
     */
    public void getLogin(String phone,String password) {

        Api.getInstance().getLogin(phone, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<UserBean>>() {

                    @Override
                    public void onSuccess(Result<UserBean> feedbackResp) {
//                        getTokenBeanMutableLiveData().postValue(feedbackResp.data);
                        getUserBeanMutableLiveData().postValue(feedbackResp.data);
                    }
                    @Override
                    public void onError(int code, String msg) {
                        getErrorMesageMutableLiveData().postValue(new ErrorMesage(code,msg));
                    }
                });
    }



}
