package com.datarecovery.master.module.mine;

import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Transformations;

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.common.runtime.ActivityUtil;
import com.atmob.common.runtime.ContextUtil;
import com.datarecovery.master.R;
import com.datarecovery.master.data.repositories.AccountRepository;
import com.datarecovery.master.data.repositories.DeviceFuncRepository;
import com.datarecovery.master.module.about.AboutActivity;
import com.datarecovery.master.module.feedback.UserFeedbackActivity;
import com.datarecovery.master.module.homepage.HomePageViewModel;
import com.datarecovery.master.module.login.LoginActivity;
import com.datarecovery.master.module.member.MemberActivity;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.module.wxrecover.WeChatRecoverActivity;
import com.datarecovery.master.utils.BoxingUtil;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;


@HiltViewModel
public class MineViewModel extends BaseViewModel {

    private final SingleLiveEvent<?> showExitDialog = new SingleLiveEvent<>();
    private final SingleLiveEvent<?> showLogoutDialog = new SingleLiveEvent<>();
    private final LiveData<String> loginTips;
    private final AccountRepository accountRepository;
    private final DeviceFuncRepository deviceFuncRepository;

    @Inject
    public MineViewModel(AccountRepository accountRepository, DeviceFuncRepository deviceFuncRepository) {
        this.accountRepository = accountRepository;
        this.deviceFuncRepository = deviceFuncRepository;
        loginTips = Transformations.map(accountRepository.getLoginPhoneNum(), phone -> {
            if (TextUtils.isEmpty(phone)) {
                return ContextUtil.getContext().getString(R.string.mine_no_login);
            } else {
                if (phone.length() > 4) {
                    phone = phone.substring(phone.length() - 4);
                }
                return ContextUtil.getContext().getString(R.string.mine_logged, phone);
            }
        });
    }

    public LiveData<?> getShowExitDialog() {
        return showExitDialog;
    }

    public LiveData<?> getShowLogoutDialog() {
        return showLogoutDialog;
    }

    public LiveData<String> getLoginTips() {
        return loginTips;
    }

    public LiveData<Boolean> getIsLogin() {
        return accountRepository.getIsLogin();
    }

    public void onLoginClick() {
        if (BoxingUtil.boxing(accountRepository.getIsLogin().getValue())) {
            return;
        }
        LoginActivity.start(ActivityUtil.getTopActivity(), "hf11018");
    }

    public void onAboutClick() {
        AboutActivity.start(ActivityUtil.getTopActivity());
    }

    public void onExitClick() {
        showExitDialog.call();
    }

    public void onAccountExit() {
        accountRepository.requestUserLogout();
    }

    public void onLogoutClick() {
        showLogoutDialog.call();
    }

    public void onAccountLogout() {

    }

    public void onFeedbackClick() {
        UserFeedbackActivity.start(ActivityUtil.getTopActivity());
    }

    public void onMemberClick() {
        //指定跳转至微信消息恢复
        isHaveAuths(MemberType.APP_WX_MESSAGE_RECOVER, () -> WeChatRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_WX_MESSAGE_RECOVER));
    }

    public void isHaveAuths(@MemberType String type, HomePageViewModel.NextStepCallback stepCallback) {
        if (deviceFuncRepository.isHaveAuth(type)) {
            if (stepCallback != null) stepCallback.onNextStep();
        } else {
            MemberActivity.start(ActivityUtil.getTopActivity(), type);
        }
    }
}
