package com.atmob.voiceai.module.clonevoice;

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

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.common.runtime.ActivityUtil;
import com.atmob.common.runtime.ContextUtil;
import com.atmob.voiceai.R;
import com.atmob.voiceai.data.api.response.VoiceCloneListResponse;
import com.atmob.voiceai.data.api.response.VoiceCloneResponse;
import com.atmob.voiceai.data.repositories.CloneRepository;
import com.atmob.voiceai.data.repositories.MemberRepository;
import com.atmob.voiceai.helper.ErrorHelper;
import com.atmob.voiceai.module.setting.SettingActivity;
import com.atmob.voiceai.module.subscription.SubscriptionPageActivity;
import com.atmob.voiceai.utils.SpannableUtil;
import com.atmob.voiceai.utils.ToastUtil;

import javax.inject.Inject;

import atmob.reactivex.rxjava3.annotations.NonNull;
import atmob.reactivex.rxjava3.core.SingleObserver;
import atmob.reactivex.rxjava3.disposables.Disposable;
import dagger.hilt.android.lifecycle.HiltViewModel;


@HiltViewModel
public class CloneVoiceViewModel extends BaseViewModel {


    private final MutableLiveData<CharSequence> cloneTip = new MutableLiveData<>();


    private final MemberRepository memberRepository;
    private final CloneRepository cloneRepository;

    private boolean requestCloneFlag = false;

    @Inject
    public CloneVoiceViewModel(MemberRepository memberRepository, CloneRepository cloneRepository) {
        this.memberRepository = memberRepository;
        this.cloneRepository = cloneRepository;
        initCloneTipTxt();
        refreshCloneList();
    }


    public LiveData<CharSequence> getCloneTip() {
        return cloneTip;
    }

    public LiveData<Boolean> getIsMember() {
        return memberRepository.getIsMember();
    }


    private void initCloneTipTxt() {
        String allTxt = ContextUtil.getContext().getString(R.string.clone_voice_tip);
        String targetTxt = ContextUtil.getContext().getString(R.string.clone_voice_tip_create);
        cloneTip.setValue(SpannableUtil.getSpannableStringBuilder(allTxt, targetTxt, ContextUtil.getContext().getResources().getColor(R.color.colorPrimaryVariant), false));
    }

    public void refreshCloneList() {
        if (requestCloneFlag) {
            return;
        }
        cloneRepository.requestCloneList().subscribe(new SingleObserver<VoiceCloneListResponse>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                addDisposable(d);
                requestCloneFlag = true;
            }

            @Override
            public void onSuccess(@NonNull VoiceCloneListResponse voiceCloneListResponse) {
                requestCloneFlag = false;

            }

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

    public void requestCloneVoice() {

//        cloneRepository.requestVoiceClone().subscribe(new SingleObserver<VoiceCloneResponse>() {
//            @Override
//            public void onSubscribe(@NonNull Disposable d) {
//                addDisposable(d);
//            }
//
//            @Override
//            public void onSuccess(@NonNull VoiceCloneResponse voiceCloneResponse) {
//                  refreshCloneList();
//            }
//
//            @Override
//            public void onError(@NonNull Throwable e) {
//                  ErrorHelper.errorThrowableToast(e, ToastUtil.LENGTH_SHORT);
//            }
//        });
    }

    public void deleteClone(int id) {
        cloneRepository.requestCloneDelete(id).subscribe(new SingleObserver<Object>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                addDisposable(d);
            }

            @Override
            public void onSuccess(@NonNull Object o) {
                refreshCloneList();
            }

            @Override
            public void onError(@NonNull Throwable e) {
                ErrorHelper.errorThrowableToast(e, ToastUtil.LENGTH_SHORT);
            }
        });
    }

    public void onGoSubscriptionClick() {
        SubscriptionPageActivity.start(ActivityUtil.getTopActivity());
    }

    public void onSettingClick() {
        SettingActivity.start(ActivityUtil.getTopActivity());
    }
}
