package com.atmob.voiceai.module.generating;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.voiceai.R;
import com.atmob.voiceai.data.repositories.VoiceAIRepository;
import com.atmob.voiceai.databinding.ActivityVoiceGeneratingBinding;
import com.atmob.voiceai.module.result.VoiceResultActivity;
import com.atmob.voiceai.utils.ToastUtil;
import com.tencent.qgame.animplayer.util.ScaleType;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class VoiceGeneratingActivity extends BaseActivity<ActivityVoiceGeneratingBinding> {

    private VoiceGeneratingViewModel voiceGeneratingViewModel;

    public static void start(Context context) {
        Intent intent = new Intent(context, VoiceGeneratingActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initObserver();
        startAnim();
    }

    private void initObserver() {
        voiceGeneratingViewModel.getGenerateSuccess().observe(this, userVoiceBean -> {
            VoiceResultActivity.start(this, userVoiceBean);
            finish();
        });
        voiceGeneratingViewModel.getGenerateError().observe(this, txt -> {
            finish();
            ToastUtil.show(txt, ToastUtil.LENGTH_SHORT);
        });
    }

    private void startAnim() {
        binding.animView.setScaleType(ScaleType.FIT_XY);
        binding.animView.setLoop(Integer.MAX_VALUE);
        binding.animView.startPlay(this.getAssets(), "anim/voice_ai_clone_loading.mp4");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopAnim();
    }

    private void stopAnim() {
        binding.animView.stopPlay();
    }

    private void initView() {

    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        voiceGeneratingViewModel = getViewModelProvider().get(VoiceGeneratingViewModel.class);
        binding.setVoiceGeneratingViewModel(voiceGeneratingViewModel);
    }

    @Override
    protected boolean shouldImmersion() {
        return true;
    }


    @Override
    public void onBackPressed() {

    }
}
