package com.datarecovery.master.module.wxrecover;

import android.text.TextUtils;

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

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.datarecovery.master.R;
import com.datarecovery.master.data.api.response.FindOrderResponse;
import com.datarecovery.master.data.repositories.AccountRepository;
import com.datarecovery.master.data.repositories.PayRepository;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.utils.ClipboardUtil;
import com.datarecovery.master.utils.ToastUtil;

import java.util.Objects;

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 WeChatViewModel extends BaseViewModel {


    private final SingleLiveEvent<?> finishEvent = new SingleLiveEvent<>();
    private final SingleLiveEvent<?> showLoginEvent = new SingleLiveEvent<>();

    private final MutableLiveData<FindOrderResponse> findOrderDetail = new MutableLiveData<>();
    private final PayRepository payRepository;
    private String type;

    @Inject
    public WeChatViewModel(PayRepository payRepository) {
        this.payRepository = payRepository;
        if (TextUtils.isEmpty(AccountRepository.token)) {
            showLoginEvent.call();
        } else {
            findOrder();
        }
    }

    public LiveData<?> getShowLoginEvent() {
        return showLoginEvent;
    }

    public LiveData<FindOrderResponse> getFindOrderDetail() {
        return findOrderDetail;
    }

    public LiveData<?> getFinishEvent() {
        return finishEvent;
    }


    public void findOrder() {
        payRepository.findOrder(new String[]{MemberType.APP_SUPER_RECOVER, MemberType.APP_WX_MESSAGE_RECOVER, MemberType.APP_WX_FRIEND_RECOVER}).subscribe(new SingleObserver<FindOrderResponse>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                addDisposable(d);
            }

            @Override
            public void onSuccess(@NonNull FindOrderResponse findOrderResponse) {
                findOrderDetail.setValue(findOrderResponse);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });
    }

    public void onBackClick() {
        finishEvent.call();
    }

    public void onCopyLinkClick() {
        FindOrderResponse value = findOrderDetail.getValue();
        if (value == null) {
            return;
        }
        ClipboardUtil.copy(value.getTutorialUrl());
        ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
    }

    public void onCopyOrderNoClick() {
        FindOrderResponse value = findOrderDetail.getValue();
        if (value == null) {
            return;
        }
        ClipboardUtil.copy(value.getOutTradeNo());
        ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
    }

    public void onCopyAuthCodeClick() {
        FindOrderResponse value = findOrderDetail.getValue();
        if (value == null) {
            return;
        }
        ClipboardUtil.copy(value.getAuthCode());
        ToastUtil.show(R.string.copy_success, ToastUtil.LENGTH_SHORT);
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getReportId() {
        if (Objects.equals(type, MemberType.APP_WX_MESSAGE_RECOVER)) {
            return "hf11011";
        } else {
            return "hf11009";
        }
    }
}
