package com.atmob.voiceai.data.repositories;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.atmob.app.lib.handler.RxHttpHandler;
import com.atmob.voiceai.data.api.AtmobApi;
import com.atmob.voiceai.data.api.bean.UserVoiceBean;
import com.atmob.voiceai.data.api.request.VoiceHistoryRequest;
import com.atmob.voiceai.data.api.response.VoiceHistoryResponse;
import com.atmob.voiceai.helper.ErrorHelper;
import com.atmob.voiceai.utils.ToastUtil;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import atmob.reactivex.rxjava3.annotations.NonNull;
import atmob.reactivex.rxjava3.core.Single;
import atmob.reactivex.rxjava3.core.SingleObserver;
import atmob.reactivex.rxjava3.disposables.Disposable;
import atmob.rxjava.utils.RxJavaUtil;

@Singleton
public class HistoryRepository {


    private final AtmobApi atmobApi;

    private final MutableLiveData<Boolean> refreshing = new MutableLiveData<>();

    private final int pageSize = 10;
    private boolean smoothScrollToTop = false;

    private boolean refreshFlag;

    private int page = 1;

    private final MutableLiveData<List<UserVoiceBean>> voiceHistoryList = new MutableLiveData<>();

    @Inject
    public HistoryRepository(AtmobApi atmobApi) {
        this.atmobApi = atmobApi;
        refreshCloneHistory(page);
    }

    public boolean isSmoothScrollToTop() {
        return smoothScrollToTop;
    }

    public LiveData<Boolean> getRefreshing() {
        return refreshing;
    }

    public LiveData<List<UserVoiceBean>> getVoiceHistoryList() {
        return voiceHistoryList;
    }

    public void refreshCloneHistory(int page) {
        if (refreshFlag) {
            return;
        }
        voiceHistory(page, pageSize).subscribe(new SingleObserver<VoiceHistoryResponse>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                refreshFlag = true;
                refreshing.setValue(true);
            }

            @Override
            public void onSuccess(@NonNull VoiceHistoryResponse voiceHistoryResponse) {
                refreshFlag = false;
                refreshing.setValue(false);
                if (page == 1) {
                    smoothScrollToTop = true;
                    voiceHistoryList.setValue(voiceHistoryResponse.getList());
                } else {
                    smoothScrollToTop = false;
                    List<UserVoiceBean> list = voiceHistoryList.getValue();
                    if (list != null) {
                        list.addAll(voiceHistoryResponse.getList());
                        voiceHistoryList.setValue(list);
                    }
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                refreshFlag = false;
                refreshing.setValue(false);
                ErrorHelper.errorThrowableToast(e, ToastUtil.LENGTH_SHORT);
            }
        });
    }


    private Single<VoiceHistoryResponse> voiceHistory(int page, int pageSize) {
        return atmobApi.voiceHistory(new VoiceHistoryRequest(page, pageSize))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }
}
