package com.datarecovery.master.module.mine;

import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
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.api.response.CustomerUrlResponse;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.data.repositories.AccountRepository;
import com.datarecovery.master.data.repositories.ConfigRepository;
import com.datarecovery.master.data.repositories.DeviceFuncRepository;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.about.AboutActivity;
import com.datarecovery.master.module.customerservice.CustomerServiceActivity;
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.sdk.wechat.WechatHelper;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.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 MineViewModel extends BaseViewModel {

    private final MutableLiveData<Boolean> showLoading = new MutableLiveData<>();
    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;
    private final ConfigRepository configRepository;


    @Inject
    public MineViewModel(AccountRepository accountRepository, DeviceFuncRepository deviceFuncRepository, ConfigRepository configRepository) {
        this.accountRepository = accountRepository;
        this.configRepository = configRepository;
        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<Boolean> getShowLoading() {
        return showLoading;
    }

    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 refreshCustomerUrl() {
        configRepository.getCustomerUrl(AccountRepository.getUserId(), AccountRepository.getKeyLoginPhoneNum())
                .subscribe(new SingleObserver<CustomerUrlResponse>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                        showLoading.setValue(true);
                    }

                    @Override
                    public void onSuccess(@NonNull CustomerUrlResponse customerUrlResponse) {
                        showLoading.setValue(false);
                        goCustomer(customerUrlResponse);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showLoading.setValue(false);
                        ToastUtil.show(R.string.net_error, ToastUtil.LENGTH_SHORT);
                    }
                });
    }

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

    public void onAboutClick() {
        EventHelper.report(EventId.hf1001102);
        AboutActivity.start(ActivityUtil.getTopActivity());
    }

    public void onExitClick() {
        EventHelper.report(EventId.hf1001112);
        showExitDialog.call();
    }

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

    public void onLogoutClick() {
        EventHelper.report(EventId.hf1001109);
        showLogoutDialog.call();
    }

    public void onAccountLogout() {
        accountRepository.logout();
    }

    public void onFeedbackClick() {
        EventHelper.report(EventId.hf1001105);
        UserFeedbackActivity.start(ActivityUtil.getTopActivity());
    }

    public void onMemberClick() {
        EventHelper.report(EventId.hf1001101);
        //指定跳转至微信消息恢复
        isHaveAuths(MemberType.APP_WX_MESSAGE_RECOVER, isTrial -> 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(false);
        } else {
            MemberActivity.start(ActivityUtil.getTopActivity(), type);
        }
    }

    public void onCustomerServiceClick() {
        EventHelper.report(EventId.hf1001108);
        refreshCustomerUrl();
    }

    private void goCustomer(@NonNull CustomerUrlResponse customerResponse) {
        switch (customerResponse.getMethod()) {
            case CustomerUrlResponse.CustomerMethod.METHOD_ALI:
                CustomerServiceActivity.start(ActivityUtil.getTopActivity(), customerResponse.getCustomerUrl());
                break;
            case CustomerUrlResponse.CustomerMethod.METHOD_WX:
                WechatHelper.launchCustomerService(customerResponse.getCustomerUrl());
                break;
            default:
                ToastUtil.show(R.string.no_customer_service, ToastUtil.LENGTH_SHORT);
                break;
        }
    }

    public void onAppealClick() {
        EventHelper.report(EventId.hf1001115);
        refreshCustomerUrl();
    }
}
