package com.cooleshow.student.presenter.mine;

import android.app.Activity;

import com.cooleshow.base.constanst.UploadConstants;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.base.utils.helper.upload.UploadHelper;

import com.cooleshow.student.api.APIService;
import com.cooleshow.student.bean.StudentUserInfo;
import com.cooleshow.student.contract.PersonalSettingContract;
import com.cooleshow.usercenter.bean.SetDetailBean;

import java.io.File;

/**
 * 创建日期：2022/5/16 16:10
 *
 * @author Ryan
 * 类说明：
 */
public class PersonalSettingPresenter extends BasePresenter<PersonalSettingContract.PersonalSettingView> implements PersonalSettingContract.Presenter {



    public void queryUserInfo() {
        getView().showLoading();
        addSubscribe(create(APIService.class).queryUserInfo(), new BaseObserver<StudentUserInfo>(getView()) {
            @Override
            protected void onSuccess(StudentUserInfo data) {
                if (getView() != null) {
                    getView().queryUserInfoSuccess(data);
                }
            }
            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }
        });
    }



    public void submitSetDetail(String jsonStr){
        getView().showLoading();
        addSubscribe(create(APIService.class).submitSetDetail(RequestBodyUtil.convertToRequestBodyJson(jsonStr)), new BaseObserver<SetDetailBean>(getView()) {
            @Override
            protected void onSuccess(SetDetailBean data) {
                if (getView() != null) {
                    getView().submitSetDetailSuccess(data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        });
    }

    public void upLoadImage(Activity activity, String filePath) {
        UploadHelper uploadHelper = new UploadHelper(activity, UploadConstants.UPLOAD_TYPE_OTHER);
        uploadHelper.uploadFile(new File(filePath));
        uploadHelper.setUpLoadCallBack(new UploadHelper.UpLoadCallBack() {
            @Override
            public void onSuccess(String url) {
                if (getView() != null) {
                    getView().upLoadImageSuccess(url);
                }
            }

            @Override
            public void onFailure() {
                if (getView() != null) {
                    getView().upLoadImageFailure();
                }
            }
        });
    }
}
