package com.ydl.component.service.web;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Rect;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ScreenUtils;
import com.ydl.confide.api.IConfideService;
import com.ydl.confide.home.event.ChangeAnotherExpertEvent;
import com.ydl.webview.H5JsBean;
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydlcommon.modular.ModularServiceManager;
import com.yidianling.common.tools.LogUtil;
import com.yidianling.consultant.preview.GPreviewBuilder;
import com.yidianling.consultant.preview.UserViewInfo;
import com.yidianling.im.api.service.IImService;
import com.yidianling.im.event.CloseBottomWebviewEvent;
import com.yidianling.muse.activity.ChooseMusicActivity;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * webview 点击事件监听 抽象类
 * Created by harvie on 2017/7/4 0004.
 */
public class WVClickAbstractListener implements WebViewClientClickListener {

    public Activity mContext;

    public WVClickAbstractListener(Activity activity) {
        this.mContext = activity;
    }

    @Override
    public void openH5(H5JsBean.H5JsCmd.Params jsData) {
        LogUtil.d("openH5 params: " + jsData.toString());
        H5Params h5Params = new H5Params(jsData.getUrl(), null);
        h5Params.setShareData(jsData.getShare());
        h5Params.setShowMenu(jsData.getDot_flag());
        NewH5Activity.start(mContext, h5Params);
    }

    @Override
    public void courseList() {

    }

    @Override
    public void activeDetail(String id) {

    }

    @Override
    public void contactYi() {

    }

    @Override
    public void contactCourseCustomerService() {

    }

    @Override
    public void openTopicDetail(String topic_id) {

    }

    @Override
    public void openMember(String id) {

    }

    @Override
    public void sendTrend() {

    }

    @Override
    public void sendResultTrend(String cover1, String title1, String purl1, String share_url1) {

    }

    @Override
    public void sendInfo(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void searchList(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void openExpertsHome(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void openTest(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void chat(int id, int toUid, int canTalk, String accessToken, int isFromQingShu) {

    }

    @Override
    public void chatSendMessage(int id, int toUid, int canTalk, String accessToken, int isFromQingShu) {

    }

    @Override
    public void chatTeam(int tid, int doctorId) {

    }

    @Override
    public void jumpLogin(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void openFmDetail(int id) {

    }

    @Override
    public void openArticle() {

    }

    @Override
    public void openFmList() {

    }

    @Override
    public void sendSubscriptionTimeMessage(String to_uid) {

    }

    @Override
    public void pay(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void payReceipt(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void payCourse(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void payTest(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void viewTestResult(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void ydlNative(H5JsBean.H5JsCmd jsData) {

    }

    @Override
    public void openOrderDetail(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void expertProduct(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void order(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void toOrderCt(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void showDocList(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void goodExpert() {

    }

    @Override
    public void copyWechat(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void goWechat() {

    }

    @Override
    public void listenOrderDetail(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void detailSub(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void openAgreement(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void modifyEval(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void visitEval(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void coursePlay(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void closeFloatView() {

    }

    @Override
    public void courseWriteComment(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void courseComment(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void listenTel(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void voiceBroadcast(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void tel(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void onOrderByApp(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void orderSetTime(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void searchServiceDoc(int cateId) {

    }

    @Override
    public void openTestDetail(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void shareAction(H5JsBean.H5JsCmd.Params params) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).showShareMenu(params);
        }
    }

    @Override
    public void openTestList() {

    }

    @Override
    public void feedBack() {

    }

    @Override
    public void phoneCall() {

    }

    @Override
    public void chatSchedule(H5JsBean.H5JsCmd.Params params) {
        ModularServiceManager.INSTANCE.provide(IImService.class)
                .chatScheule(mContext, params.getUser_url(), params.getDoc_url(), params.getDsmId(), params.getTitle(), params.getToUid());
    }

    @Override
    public void invite(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void balance(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void switchDownRefresh(H5JsBean.H5JsCmd.Params params) {
        if (mContext != null && mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).hasShowDownRefresh(params.getSwitch());
        }
    }

    @Override
    public void recharge() {

    }

    @Override
    public void bindPhone() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void listenAgora(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void setTitle(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void saveImage(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void sendToExpert(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void confideConnect(H5JsBean.H5JsCmd.Params jsData) {
        ((IConfideService) ARouter.getInstance().build("/confide/ConfideService").navigation()).connectionJava(jsData.getId(), 3, mContext, null, jsData.getCallType());
    }

    @Override
    public void confidePay(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void back() {
        mContext.finish();
    }

    @Override
    public void commonPay(H5JsBean.H5JsCmd.Params jsData) {

    }

    @Override
    public void hideStatusBar() {

    }

    @Override
    public void showStatusBar() {

    }

    @Override
    public void openRightTopMenu() {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).openRightTopMenu();
        }
    }

    @Override
    public void openShareMenu(H5JsBean.H5JsCmd.Params params) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).openShareMenu(params);
        }
    }

    @Override
    public void closeWebKit() {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).finish();
        }
    }

    @Override
    public void goHome(H5JsBean.H5JsCmd.Params params) {

    }

    @Override
    public void sendUnReadNum(String callbackFuncName, String uid) {

        final StringBuffer sb = new StringBuffer();
        sb.append(callbackFuncName);
        sb.append("('");
        sb.append("33,125");
        sb.append("')");
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).callJSFunc(sb.toString());
        }


    }

    @Override
    public void shouldShowTitleBar(boolean isShowTitleBar) {
        if (mContext instanceof NewH5Activity) {
            if (isShowTitleBar) {
                ((NewH5Activity) mContext).showTitleBar();
            } else {
                ((NewH5Activity) mContext).hideJavaTitleBar();
            }
        }
    }

    @Override
    public void setSelfPageType(int selfType) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).setSelfPageType(selfType);
        }
    }

    @Override
    public void playMeditation(int mediaId, long meditationId, int meditationType, String mediaCoverUrl) {
        if (mContext instanceof NewH5Activity) {
//            YDLRouterManager.Companion.router(ROUTER_MUSE_PLAY,
//                    new YDLRouterParams().putExtra("MEDITATION_ID", String.valueOf(meditationId))
//                            .putExtra("MEDIA_ID", String.valueOf(mediaId))
//                            .putExtra("MEDITATION_TYPE", String.valueOf(meditationType))
//                            .putExtra("MEDIA_COVER_URL", mediaCoverUrl));

            ARouter.getInstance().build("/muse/play")
                    .withLong("MEDITATION_ID", meditationId)
                    .withLong("MEDIA_ID", mediaId)
                    .withInt("MEDITATION_TYPE", meditationType)
                    .withString("MEDIA_COVER_URL", mediaCoverUrl)
                    .navigation();

        }
    }

    @Override
    public void getNextExpertStatus(String doctorID, String title, String uid, String linkUrl) {
        EventBus.getDefault().post(new ChangeAnotherExpertEvent(doctorID, title, uid, linkUrl));
    }

    @Override
    public void switchPushStatus(H5JsBean.H5JsCmd.Params params) {
        if (params.getSwitchStatus() == 1) {
//            MsgPushLifecycle.Companion.setH5Open(true);
        } else {
//            MsgPushLifecycle.Companion.setH5Open(false);
        }
    }

    @Override
    public void resourceToPreview(H5JsBean.H5JsCmd.Params params) {
        List<H5JsBean.MediaInfo> dataList = params.getDataList();
        H5JsBean.DoctorInfo info = params.getInfo();

        List<UserViewInfo> mThumbViewInfoList = new ArrayList<>();

        for (H5JsBean.MediaInfo mediaInfo : dataList) {
            UserViewInfo userViewInfo = new UserViewInfo(mediaInfo.getCover(), mediaInfo.getUrl(), mediaInfo.getSourcesType());
            Rect bounds = new Rect();
            bounds.left = ScreenUtils.getScreenWidth() / 2;
            bounds.top = ScreenUtils.getScreenHeight() / 2;
            bounds.right = ScreenUtils.getScreenWidth() / 2;
            bounds.bottom = ScreenUtils.getScreenHeight() / 2;
            userViewInfo.setBounds(bounds);
            mThumbViewInfoList.add(userViewInfo);
        }

        GPreviewBuilder.form(mContext)
                .setData(mThumbViewInfoList)
                .setCurrentIndex(params.getPreview_index())
                .setFullscreen(true)
                .setToUid(info.getToUid() + "")
                .setDoctorId(info.getDoctorId())
                .setType(GPreviewBuilder.IndicatorType.Dot)
                .start();
    }

    @Override
    public void showDocBooking(H5JsBean.H5JsCmd.Params params) {
        ModularServiceManager.INSTANCE.provide(IImService.class).showConsultServiceDialog(mContext, params.getToUid() + "", params.getDoctorId() + "");
    }

    @Override
    public void switchSound(int mediaId, long meditationId, int meditationType, int businessType,
                            String buried, String mediaUrl, String mediaCoverUrl,
                            String title, String desc, int status) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).switchSound(mediaId, meditationId, meditationType,
                    businessType, buried, mediaUrl, mediaCoverUrl, title, desc, status);
        } else if (mContext instanceof ChooseMusicActivity) {
            ((ChooseMusicActivity) mContext).switchSound(mediaId, meditationId, meditationType,
                    businessType, buried, mediaUrl, mediaCoverUrl, title, desc, status);
        }
    }

    @Override
    public void chatCloseBottomWebView() {
        EventBus.getDefault().post(new CloseBottomWebviewEvent(true));
        ModularServiceManager.INSTANCE.provide(IImService.class).dismissConsultServiceDialog();
    }


    @Override
    public void reloadUrl(String url) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).loadUrl(url);
        }
    }

    @Override
    public void setWebViewBG(String rgb, String alpha) {
        if (mContext instanceof NewH5Activity) {
            ((NewH5Activity) mContext).setBG(rgb, alpha);
        }
    }

    /**
     * 打开时间选择器
     */
    @SuppressLint("CheckResult")
    @Override
    public void openTimePicker(String jsCallBackName, String day, String doctorId, String orderId) {

    }


    @Override
    public void showCommentArticleDialog(H5JsBean.H5JsCmd.Params params) {
        if (mContext instanceof NewH5Activity){
            ((NewH5Activity)mContext).showCommentArticleDialog(params.getTitle());
        }
    }

}
