package com.dskj.daikuan.viewModel.home;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.azhon.basic.lifecycle.BaseViewModel;
import com.dskj.daikuan.InitApp;
import com.dskj.daikuan.api.Api;
import com.dskj.daikuan.api.BaseObserver;
import com.dskj.daikuan.api.Result;
import com.dskj.daikuan.bean.ErrorMesage;
import com.dskj.daikuan.bean.TokenBean;
import com.dskj.daikuan.bean.UserBean;
import com.dskj.daikuan.bean.VideoIndexBean;
import com.dskj.daikuan.bean.VideoIndexResult;
import com.dskj.daikuan.bean.VideoResult;
import com.dskj.daikuan.utils.GetAndroidUniqueMark;
import com.dskj.daikuan.utils.LogUtils;
import com.dskj.daikuan.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));
                    }
                });
    }



}
