package com.atmob.voiceai.data.repositories;


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

import com.atmob.app.lib.handler.RxHttpHandler;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.voiceai.data.api.AtmobApi;
import com.atmob.voiceai.data.api.GenerateApi;
import com.atmob.voiceai.data.api.request.BaseRequest;
import com.atmob.voiceai.data.api.request.CloneDeleteRequest;
import com.atmob.voiceai.data.api.request.VoiceCloneRequest;
import com.atmob.voiceai.data.api.response.VoiceCloneListResponse;
import com.atmob.voiceai.data.api.response.VoiceCloneResponse;

import java.io.File;
import java.util.concurrent.TimeUnit;

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

import atmob.reactivex.rxjava3.core.Single;
import atmob.rxjava.utils.RxJavaUtil;

@Singleton
public class CloneRepository {


    private final AtmobApi atmobApi;
    private final VoiceAIRepository voiceAIRepository;
    private final GenerateApi generateApi;

    @Inject
    public CloneRepository(AtmobApi atmobApi, GenerateApi generateApi, VoiceAIRepository voiceAIRepository) {
        this.atmobApi = atmobApi;
        this.generateApi = generateApi;
        this.voiceAIRepository = voiceAIRepository;
    }

    public Single<VoiceCloneListResponse> requestCloneList() {
        return atmobApi.getCloneList(new BaseRequest())
                .compose(RxHttpHandler.handle(true))
                .retryWhen(RxJavaUtil.exponentialBackOff(null, 3, TimeUnit.SECONDS))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }

    public Single<Object> requestCloneDelete(int id) {
        return atmobApi.cloneDelete(new CloneDeleteRequest(id))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main())
                .doOnSuccess(response -> voiceAIRepository.setCloneExecuteSuccessEvent());

    }

    public Single<VoiceCloneResponse> requestVoiceClone(int type, File file) {
        return generateApi.voiceClone(new VoiceCloneRequest(type, file))
                .compose(RxHttpHandler.handle(false))
                .compose(RxJavaUtil.SingleSchedule.io2Main())
                .doOnSuccess(response -> voiceAIRepository.setCloneExecuteSuccessEvent());
    }
}
