package com.hc.viewmodel;

import android.app.Application;
import android.support.annotation.NonNull;

import com.hc.model.BaseResponse;
import com.hc.request.business.UserService;
import com.hc.request.core.DefaultCallback;

public class UserViewModel extends BaseViewModel {

    private final UserService userService;
    public final SingleLiveEvent<Boolean> hadCollectedSource = new SingleLiveEvent<>();
    public final SingleLiveEvent<Boolean> isCollectedSuccess = new SingleLiveEvent<>();
    public final SingleLiveEvent<String> toastEvent = new SingleLiveEvent<>();

    public UserViewModel(@NonNull Application application) {
        super(application);
        userService = new UserService();
    }

    public void hadCollectedSource(String uid, String sourceId) {
        userService.isCollect(uid, sourceId, new DefaultCallback<Boolean>() {
            @Override
            public void onSuccess(int what, @NonNull Boolean data) {
                super.onSuccess(what, data);
                hadCollectedSource.setValue(data);
            }
        });
    }

    public void collectSource(String uid, String sourceId) {
        userService.collect(uid, sourceId, new DefaultCallback<BaseResponse>() {
                    @Override
                    public void onSuccess(int what, @NonNull BaseResponse data) {
                        super.onSuccess(what, data);
                        toastEvent.setValue(data.getMsg());
                        isCollectedSuccess.setValue(true);
                        hadCollectedSource.setValue(true);
                    }

                    @Override
                    public void onError(int what, Throwable e) {
                        super.onError(what, e);
                        //toastEvent.setValue(e.getMessage());
                        isCollectedSuccess.setValue(false);
                    }

                    @Override
                    public void onFail(int what, String msg) {
                        super.onFail(what, msg);
                        //toastEvent.setValue(msg);
                        isCollectedSuccess.setValue(false);
                    }
                }
        );
    }

    public void deleteCollection(String uid, String sourceId) {
        userService.deleteCollection(uid, sourceId, new DefaultCallback<BaseResponse>() {
            @Override
            public void onSuccess(int what, @NonNull BaseResponse data) {
                super.onSuccess(what, data);
                toastEvent.setValue(data.getMsg());
                isCollectedSuccess.setValue(false);
                hadCollectedSource.setValue(false);
            }

            @Override
            public void onError(int what, Throwable e) {
                super.onError(what, e);
                //toastEvent.setValue(e.getMessage());
                isCollectedSuccess.setValue(false);
            }

            @Override
            public void onFail(int what, String msg) {
                super.onFail(what, msg);
                //toastEvent.setValue(msg);
                isCollectedSuccess.setValue(false);
            }
        });
    }
}
