package com.bokecc.livemodule.localplay.doc;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

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.localplay.DWLocalReplayCoreHandler;
import com.bokecc.livemodule.view.CustomToast;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.replay.DWLiveLocalReplay;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.widget.DocView;

import java.util.ArrayList;

/**
 * 回放直播间文档展示控件
 */
public class LocalReplayDocComponent extends LinearLayout {
    private final int SCALE_CENTER_INSIDE = 0;
    private final int SCALE_FIT_XY = 1;
    private final int SCALE_CROP_CENTER = 2;

    private int mCurrentScaleType = SCALE_CENTER_INSIDE;
    private Context mContext;

//    private DocView mDocView;

    private IDocView docView;

    IDocManager docManager;

    public LocalReplayDocComponent(Context context) {
        super(context);
        mContext = context;
        initViews();
    }

    public LocalReplayDocComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initViews();
    }

    private 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() {
                    //回放 文档数据下载完成回调

                }
            }, DWLiveLocalReplay.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);

            }


        }

//        mDocView = new DocView(mContext);
//        mDocView.setScrollable(false); // 为了保证悬浮窗能正常处理触摸事件，必须将文档WebView响应滑动禁用调，且小窗模式建议使用文档的适应窗口模式
//        mDocView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        addView(mDocView);
//
//        DWLocalReplayCoreHandler replayCoreHandler = DWLocalReplayCoreHandler.getInstance();
//        if (replayCoreHandler != null) {
//            replayCoreHandler.setDocView(mDocView);
//        }
    }

    // 设置文档的拉伸模式
    public void setScaleType(int type) {
        mCurrentScaleType = type;
        if (SCALE_CENTER_INSIDE == mCurrentScaleType) {
            DWLocalReplayCoreHandler.getInstance().setDocScaleType(DocView.ScaleType.CENTER_INSIDE);
        } else if (SCALE_FIT_XY == mCurrentScaleType) {
            DWLocalReplayCoreHandler.getInstance().setDocScaleType(DocView.ScaleType.FIT_XY);
        } else if (SCALE_CROP_CENTER == mCurrentScaleType) {
            DWLocalReplayCoreHandler.getInstance().setDocScaleType(DocView.ScaleType.CROP_CENTER);
        }
    }


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