package com.datarecovery.master.data.repositories;

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

import com.datarecovery.master.data.api.AtmobApi;
import com.datarecovery.master.data.api.request.BaseRequest;
import com.datarecovery.master.data.api.response.FuncAuthsResponse;
import com.datarecovery.master.data.api.response.UserCaseResponse;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.utils.RxHttpHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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 DeviceFuncRepository {

    private final List<String> authsList = new ArrayList<>();
    private final AtmobApi atmobApi;

    private final MutableLiveData<Boolean> isShowImageTrialTag = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> isShowFileTrialTag = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> isShowVideoTrialTag = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> isShowAudioTrialTag = new MutableLiveData<>(false);

    private boolean refreshFunAuthsFlag;

    @Inject
    public DeviceFuncRepository(AtmobApi atmobApi) {
        this.atmobApi = atmobApi;
    }

    public void refreshFuncAuths() {
        if (refreshFunAuthsFlag) {
            return;
        }
        atmobApi.funcAuths(new BaseRequest())
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main())
                .subscribe(new SingleObserver<FuncAuthsResponse>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        refreshFunAuthsFlag = true;
                    }

                    @Override
                    public void onSuccess(@NonNull FuncAuthsResponse funcAuthsResponse) {
                        refreshFunAuthsFlag = false;
                        authsList.clear();
                        authsList.addAll(funcAuthsResponse.getAuths());
                        checkTrialTag();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        refreshFunAuthsFlag = false;
                    }
                });
    }

    private void checkTrialTag() {
        if (authsList.contains(MemberType.APP_SUPER_RECOVER)) {
            isShowImageTrialTag.setValue(false);
            isShowFileTrialTag.setValue(false);
            isShowVideoTrialTag.setValue(false);
            isShowAudioTrialTag.setValue(false);
            return;
        }
        isShowImageTrialTag.setValue(!authsList.contains(MemberType.APP_IMAGE_RECOVER));
        isShowFileTrialTag.setValue(!authsList.contains(MemberType.APP_FILE_RECOVER));
        isShowVideoTrialTag.setValue(!authsList.contains(MemberType.APP_VIDEO_RECOVER));
        isShowAudioTrialTag.setValue(!authsList.contains(MemberType.APP_AUDIO_RECOVER));
    }

    public LiveData<Boolean> getIsShowAudioTrialTag() {
        return isShowAudioTrialTag;
    }

    public LiveData<Boolean> getIsShowFileTrialTag() {
        return isShowFileTrialTag;
    }

    public LiveData<Boolean> getIsShowImageTrialTag() {
        return isShowImageTrialTag;
    }

    public LiveData<Boolean> getIsShowVideoTrialTag() {
        return isShowVideoTrialTag;
    }

    public void clearAuths() {
        authsList.clear();
    }


    public Single<UserCaseResponse> userCaseList() {
        return atmobApi.userCaseList(new BaseRequest())
                .compose(RxHttpHandler.handle(true))
                .retryWhen(RxJavaUtil.retryWhen(null, 20, 1000, TimeUnit.MILLISECONDS))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }

}
