package com.bokecc.livemodule.live.doc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

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.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.widget.DocView;

import java.util.ArrayList;

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

    private static final String TAG = "LiveDocComponent";
    private IDocView docView;

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

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

    IDocManager docManager;

    @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) {
                    //回放 文档翻页数据回调, 直播不会回调
                }

                @Override
                public void onMetaDataLoadSuccess() {
                    //回放 文档数据下载完成回调, 直播不会回调
                }
            }, DWLive.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(false);
                docView.setBackgroundColor("#ffffff");
                docView.setDocViewListener(this);

            }


        }


        // 调试，设置背景颜色
        setBackgroundColor(Color.GRAY);
    }


    /**
     * 对doc进行截图
     *
     * @return Bitmap
     */
    @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);
                    }
                });
            }
        }
    }

    @Override
    public void setDocScrollable(boolean scrollable) {
        if (docView != null) {
            docView.setScrollable(scrollable);
        }
    }


    @Override
    public void setScaleType(int type) {
        if (0 == type) {
            DWLive.getInstance().setDocScaleType(DocView.ScaleType.CENTER_INSIDE);
        } else if (1 == type) {
            DWLive.getInstance().setDocScaleType(DocView.ScaleType.FIT_XY);
        } else if (2 == type) {
            DWLive.getInstance().setDocScaleType(DocView.ScaleType.CROP_CENTER);
        }
    }
}

