package com.atmob.voiceai.module.splash;

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

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.common.data.KVUtils;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class SplashViewModel extends BaseViewModel {

    public static final int GUIDE_PAGE_1 = 1;
    public static final int GUIDE_PAGE_2 = 2;
    public static final int GUIDE_PAGE_3 = 3;
    public static final int GUIDE_PAGE_END = 4;

    private static final String IS_FIRST_OPEN = "is_first_open";
    private final MutableLiveData<Boolean> showGuide = new MutableLiveData<>();
    private final SingleLiveEvent<?> showMainEvent = new SingleLiveEvent<>();
    private final MutableLiveData<Integer> currentGuide = new MutableLiveData<>();

    private boolean canNext = false;

    @Inject
    public SplashViewModel() {
        boolean isFirst = KVUtils.getDefault().getBoolean(IS_FIRST_OPEN, true);
        if (isFirst) {
            showGuide.setValue(true);
            currentGuide.setValue(GUIDE_PAGE_1);
        } else {
            showMainEvent.call();
        }
    }

    public LiveData<Integer> getCurrentGuide() {
        return currentGuide;
    }

    public LiveData<?> getShowMainEvent() {
        return showMainEvent;
    }

    public LiveData<Boolean> getShowGuide() {
        return showGuide;
    }


    public void setCanNext(boolean canNext) {
        this.canNext = canNext;
    }

    private void firstStartMain() {
        KVUtils.getDefault().putBoolean(IS_FIRST_OPEN, false);
    }

    public void onContinueClick(int index) {
        if (!canNext) {
            return;
        }
        if (index == GUIDE_PAGE_1) {
            currentGuide.setValue(GUIDE_PAGE_2);
        } else if (index == GUIDE_PAGE_2) {
            currentGuide.setValue(GUIDE_PAGE_3);
        }
    }

    public void onGuideDoneClick() {
        firstStartMain();
        currentGuide.setValue(GUIDE_PAGE_END);
    }
}
