package com.yidianling.im.session.action;

import android.text.TextUtils;

import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydlcommon.base.lifecycle.ILifecycleable;
import com.ydl.ydlcommon.data.http.BaseResponse;
import com.ydl.ydlcommon.data.http.RxUtils;
import com.ydl.ydlcommon.data.http.ThrowableConsumer;
import com.ydl.ydlcommon.utils.remind.ToastHelper;
import com.yidianling.im.R;
import com.yidianling.im.http.ImHttpImpl;
import com.yidianling.im.http.ImRetrofitApi;
import com.yidianling.im.http.param.ExpertParam;
import com.yidianling.nimbase.business.session.actions.BaseAction;
import com.yidianling.im.api.bean.IMExpertBuild;

import org.jetbrains.annotations.NotNull;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/04/23
 * 查看订单按钮
 */

public class OrderAction extends BaseAction {

    public OrderAction() {
        super(R.drawable.im_btn_chat_orders, R.string.im_input_panel_order);
    }

    @Override
    public void onClick() {
        ILifecycleable lifecycleable = null;
        if (getActivity() instanceof ILifecycleable) {
            lifecycleable = (ILifecycleable)(getActivity());
        }

        //先根据uid判断用户类型：专家调一个接口，非专家调另外的接口
        Disposable dis = ImRetrofitApi.Companion.getImJavaApi().getUserType(getAccount())
                .compose(RxUtils.applySchedulers(lifecycleable))
                .subscribe(res -> {
                    if (res.code == 200) {
                        Observable<BaseResponse<IMExpertBuild>> observable;
                        if (TextUtils.equals(res.data.getUserType(), "2")) {//专家
                            observable = ImRetrofitApi.Companion.getImJavaApi().getPersonalChat(getAccount());
                        } else {
                            observable = ImHttpImpl.Companion.getInstance().getExpert(new ExpertParam(Integer.valueOf(getAccount()), 0));
                        }

                        Disposable disposable = observable
                                .compose(RxUtils.applySchedulers())
                                .subscribe(resp -> {
                                    if (resp != null && resp.data != null && resp.data.shareData != null && !TextUtils.isEmpty(resp.data.shareData.order_url)) {
                                        NewH5Activity.start(getActivity(), new H5Params(resp.data.shareData.order_url + "?search=" + resp.data.shareData.doctorName, "订单"));
                                    } else {
                                        if (resp != null) {
                                            ToastHelper.Companion.show(resp.msg);
                                        }
                                    }
                                }, new ThrowableConsumer() {
                                    @Override
                                    public void accept(@NotNull String msg) {
                                        ToastHelper.Companion.show(msg);
                                    }
                                });
                    } else {
                        ToastHelper.Companion.show(res.msg);
                    }
                }, throwable -> {
                    ToastHelper.Companion.show(throwable.getMessage());
                });

    }
}
