package com.datarecovery.master.data.repositories;


import android.text.TextUtils;

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.Objects;

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 boolean refreshFunAuthsFlag;

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


    public boolean isHaveAuth(@MemberType String auth) {
        if (TextUtils.isEmpty(auth) || authsList.size() == 0) {
            return false;
        }
        if (!Objects.equals(auth, MemberType.APP_IMAGE_CLEAN) && authsList.contains(MemberType.APP_SUPER_RECOVER)) {
            return true;
        }
        if (Objects.equals(auth, MemberType.APP_WX_MESSAGE_RECOVER) || Objects.equals(auth, MemberType.APP_WX_FRIEND_RECOVER)) {
            return authsList.contains(MemberType.APP_WX_MESSAGE_RECOVER) || authsList.contains(MemberType.APP_WX_FRIEND_RECOVER);
        }
        return authsList.contains(auth);
    }

    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());
                    }

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

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


    public Single<UserCaseResponse> userCaseList() {
        return atmobApi.userCaseList(new BaseRequest())
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }

}
