package com.atmob.voiceai.module.generating;

import android.text.TextUtils;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.common.runtime.ContextUtil;
import com.atmob.voiceai.R;
import com.atmob.voiceai.data.api.bean.UserVoiceBean;
import com.atmob.voiceai.data.api.bean.VoiceListBean;
import com.atmob.voiceai.data.consts.EventId;
import com.atmob.voiceai.data.repositories.VoiceAIRepository;
import com.atmob.voiceai.handlers.EventHandler;
import com.atmob.voiceai.utils.ThreePair;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import atmob.rxjava.utils.RxJavaUtil;
import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class VoiceGeneratingViewModel extends BaseViewModel {

    private final MutableLiveData<String> generatingTime = new MutableLiveData<>();

    private final SingleLiveEvent<UserVoiceBean> generateSuccess = new SingleLiveEvent<>();
    private final SingleLiveEvent<String> generateError = new SingleLiveEvent<>();

    private final VoiceAIRepository voiceAIRepository;

    private final Observer<ThreePair<Integer, String, UserVoiceBean>> stateObserver;

    @Inject
    public VoiceGeneratingViewModel(VoiceAIRepository voiceAIRepository) {
        this.voiceAIRepository = voiceAIRepository;
        startCountDown();
        stateObserver = state -> {
            if (state == null) {
                return;
            }
            if (state.first == VoiceAIRepository.TextToSpeechState.ERROR) {
                generateError.setValue(state.second);
            } else if (state.first == VoiceAIRepository.TextToSpeechState.GENERATED) {
                generateSuccess.setValue(state.third);
            }
        };
        voiceAIRepository.getTextToSpeechState().observeForever(stateObserver);
        EventHandler.report(EventId.generating_001);
    }

    public LiveData<UserVoiceBean> getGenerateSuccess() {
        return generateSuccess;
    }

    public LiveData<String> getGenerateError() {
        return generateError;
    }

    private void startCountDown() {
        String textToSpeechTxt = voiceAIRepository.getTextToSpeechTxt();
        if (TextUtils.isEmpty(textToSpeechTxt)) {
            return;
        }
        int time = getRangeTime(textToSpeechTxt);
        RxJavaUtil.interval(0, 1, time, TimeUnit.SECONDS, aLong -> generatingTime.setValue(ContextUtil.getContext().getString(R.string.generating_time, time - aLong)), () -> generatingTime.setValue(ContextUtil.getContext().getString(R.string.generating_time_countdown_over)));
    }


    private int getRangeTime(@NonNull String textToSpeechTxt) {
        int length = textToSpeechTxt.length();

        if (length <= 30) {
            return 5;
        } else if (length <= 80) {
            return 10;
        } else if (length <= 120) {
            return 15;
        } else {
            return 20;
        }
    }


    public LiveData<String> getGeneratingTime() {
        return generatingTime;
    }

    public VoiceListBean getCloneBean() {
        return voiceAIRepository.getRequestCloneBean();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        voiceAIRepository.getTextToSpeechState().removeObserver(stateObserver);
    }
}
