package com.bokecc.livemodule.replay.doc;

import android.content.Context;
import androidx.annotation.Nullable;

import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.bokecc.doc.docsdk.BaseDocCallback;
import com.bokecc.doc.docsdk.bean.ReplayPageInfo;
import com.bokecc.doc.docsdk.core.DocErrorCode;
import com.bokecc.doc.docsdk.core.IDocManager;
import com.bokecc.doc.docsdk.core.IDocManagerListener;
import com.bokecc.doc.docsdk.core.IDocView;
import com.bokecc.doc.docsdk.core.impl.DocManagerFactory;
import com.bokecc.livemodule.base.BaseDocComponent;
import com.bokecc.livemodule.base.DocCaptureListener;
import com.bokecc.livemodule.replay.DWReplayCoreHandler;
import com.bokecc.sdk.mobile.live.doc.ScaleType;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.widget.DocView;

import java.util.ArrayList;

/**
 * 回放直播间文档展示控件
 */
public class ReplayDocComponent extends BaseDocComponent {

    private static final String TAG = "ReplayDocComponent";
    private IDocView docView;
    IDocManager docManager;

    public ReplayDocComponent(Context context) {
        super(context);
    }

    public ReplayDocComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initViews() {
        //使用文档模块获取docview，添加到布局
        if (docManager == null) {
            docManager = new DocManagerFactory().create(mContext, new IDocManagerListener() {
                @Override
                public void onPrepared() {
//                    CustomToast.showToast(mContext, "文档组件SDK准备完成", Toast.LENGTH_SHORT);
                }

                @Override
                public void onError(DocErrorCode code, String msg) {

                }

                @Override
                public void onPageInfoList(ArrayList<ReplayPageInfo> infoList) {
                    //回放 文档翻页数据回调
                    Log.i(TAG, "onPageInfoList?infoList");
                    DWReplayCoreHandler.getInstance().onReplayDocPageInfo(infoList);

                }

                @Override
                public void onMetaDataLoadSuccess() {
                    //回放 文档数据下载完成回调

                }
            }, DWLiveReplay.getInstance());

            //必须准备之后才能获取IDocView对象
            docManager.prepare();
            if (docManager.getDocView() != null) {
                docView = docManager.getDocView();
                View view = docView.getView();
                if (view != null) {
                    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    view.setLayoutParams(params);
                    addView(view);
                }


                docView.setScrollable(true);
                docView.setBackgroundColor("#ffffff");
                docView.setDocViewListener(this);

            }


        }

    }

    @Override
    public void getDocBitmap(final DocCaptureListener listener) {
        if (docManager != null&&listener!=null) {
            if (docManager.getDocView() != null) {
                docManager.getDocView().captureBitmap(new BaseDocCallback<Bitmap, String>() {
                    @Override
                    public void onError(String error) {

                    }

                    @Override
                    public void onSuccess(Bitmap msg) {
                        listener.onBitmap(msg);
                    }
                });
            }
        }
    }

    // 设置文档是否可滑动
    public void setDocScrollable(boolean isDocScrollable) {
        if (docView != null) {
            docView.setScrollable(isDocScrollable);
        }
    }

    // 设置文档的拉伸模式
    public void setScaleType(int type) {
        if (DocView.ScaleType.CENTER_INSIDE.ordinal() == type) {
            docView.setScaleType(ScaleType.CENTER_INSIDE);
        } else if (DocView.ScaleType.FIT_XY.ordinal() == type) {
            docView.setScaleType(ScaleType.FIT_XY);
        } else if (DocView.ScaleType.CROP_CENTER.ordinal() == type) {
            docView.setScaleType(ScaleType.CROP_CENTER);
        }
    }

}
