package cn.tee3.avd;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.LinkedList;

import cn.tee3.R;

public class WhiteboardView extends RelativeLayout implements MAnnotationInternal.Listener, WhiteboardToolView.DrawToolbarListener, WhiteboardDrawView.IUploadAnnotationInternalListener, WhiteboardDrawView.IWhiteboardTouchEventNotify {
    private Context context;
    private View view;
    private static final String TAG = "WhiteboardView";
    private MAnnotationInternal annotationInternal;

    public WhiteboardDrawView drawView;
    public WhiteboardToolView toolView;
    private int screenWidth;
    private int screenHeight;
    private MWhiteboard.Whiteboard whiteboard;
    private boolean isEnableDraw;
    private boolean showBgm;
    private int drawViewCanvasColor;
    private String userId;
    private DrawType drawType = DrawType.FOLLOW_SETTING_SIZE;

    /* 画线工作模式 */
    public enum DrawType {
        KEEP_WIDTH_HEIGHT_RATIO,    // 白板大小保持发布白板原有的宽高比率
        FOLLOW_SETTING_SIZE         // 白板大小跟随父布局指定的大小
    }

    public WhiteboardView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public WhiteboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    private void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.cn_tee3_whiteboard_view, this, true);
        drawView = view.findViewById(R.id.drawView);
        toolView = view.findViewById(R.id.toolView);
    }

    public void setWhiteboardColor(int color) {
        drawViewCanvasColor = color;
    }

    /** 设置白板宽高格式：发布白板的原始宽高比尺寸和跟随设定的宽高值 */
    public WhiteboardView setDrawType(DrawType type) {
        this.drawType = type;
        return this;
    }

    protected WhiteboardView setAnnotationInternal(MAnnotationInternal annotationInternal) {
        this.annotationInternal = annotationInternal;
        annotationInternal.setListener(this);
        return this;
    }

    protected WhiteboardView setUserId(String userId) {
        this.userId = userId;
        return this;
    }

    protected void setParams(MWhiteboard.Whiteboard whiteboard, MAnnotationInternal mAnnotationInternal, boolean isLocalWhiteboard) {
        if (whiteboard == null) {
            return;
        }
        this.whiteboard = whiteboard;
        if (annotationInternal == null) {
            annotationInternal = mAnnotationInternal;
            annotationInternal.setListener(this);
        }
        Log.i("qqq", "setParams: " + whiteboard.toString() + " ======= isLocalWhiteboard: " + isLocalWhiteboard);
        drawView.setParams(this, whiteboard, annotationInternal.getAnnotationList(whiteboard._boardId), isLocalWhiteboard);
        drawView.setWhiteboardColor(drawViewCanvasColor);
        drawView.setUserId(userId);
        drawView.setDrawType(drawType);
        drawView.setUpLoadAnnotationCallback(WhiteboardView.this);
        drawView.setTouchEventNotify(this);

        toolView.setDrawToolViewVisibleListener(WhiteboardView.this);
        myGestureDetectorListener = new MyGestureDetectorListener();
        gestureDetector = new GestureDetector(context, myGestureDetectorListener);

        if (onViewCreatedListener != null) {
            onViewCreatedListener.onCreated();
        }
    }

    protected void clearPaint() {
        drawView.clearPaint();
    }

    protected void clear() {
        drawView.clear();
    }

    public void showToolbar(boolean isShow) {
        if (toolView != null) {
            toolView.setVisibility(isShow ? VISIBLE : GONE);
        }
    }

    public MWhiteboard.Whiteboard getWhiteboard() {
        return whiteboard;
    }

    protected Context getViewContext() {
        return context;
    }

    protected LinkedList<String> getLines() {
        return annotationInternal.getAnnotationList(whiteboard._boardId);
    }

    public IDrawToolbarListener drawToolbarListener;

    public void setDrawToolViewVisibleListener(IDrawToolbarListener listener) {
        this.drawToolbarListener = listener;
    }

    public interface IDrawToolbarListener {
        void isDrawToolbarVisibleNotify(boolean isVisible);
    }

    public OnViewCreatedListener onViewCreatedListener;

    public WhiteboardView setOnViewCreatedListener(OnViewCreatedListener listener) {
        this.onViewCreatedListener = listener;
        return this;
    }

    public interface OnViewCreatedListener {
        void onCreated();
    }

    public IOnTouchEventListener touchEventListener;

    public void setOnTouchEventListener(IOnTouchEventListener listener) {
        this.touchEventListener = listener;
    }

    public interface IOnTouchEventListener {
        void onStartZoomOperation();

        void onStopZoomOperation();

        void onSingleClick();
    }

    /********************************************************************************   单击事件 的监听   ****************************************************************************************************/

    private GestureDetector gestureDetector;
    private MyGestureDetectorListener myGestureDetectorListener;

    public class MyGestureDetectorListener extends GestureDetector.SimpleOnGestureListener {
        public MyGestureDetectorListener() {
            super();
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (touchEventListener != null) {
                if (toolView.isDrawToolViewVisible) {
                    return false;
                } else {
                    touchEventListener.onSingleClick();
                }
                return true;
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector != null) {
            gestureDetector.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    /********************************************************************************   触摸事件 的监听   ****************************************************************************************************/
    @Override
    public void onStartZoomOperation() {
        if (touchEventListener != null) {
            touchEventListener.onStartZoomOperation();
        }
    }

    @Override
    public void onStopZoomOperation() {
        if (touchEventListener != null) {
            touchEventListener.onStopZoomOperation();
        }
    }

    @Override
    public void onSingleClick() {
        if (touchEventListener != null) {
            touchEventListener.onSingleClick();
        }
    }

    /********************************************************************************   Annotation 的监听   ****************************************************************************************************/
    @Override
    public void onAnnotationDelNotify(String j_boardId, String annotation_id) {
        if (drawView != null) {
            drawView.annotationDel(j_boardId, annotation_id);
        }
    }

    @Override
    public void onAnnotationAddNotifyStr(String j_boardId, String j_annotationstr) {
        if (drawView != null) {
            drawView.annotationAdd(j_boardId, j_annotationstr);
        }
    }

    @Override
    public void onAnnotationUpdateNotifyStr(String j_boardId, String j_annotationstr) {
        if (drawView != null) {
            drawView.annotationUpdate(j_boardId, j_annotationstr);
        }
    }

    @Override   /** web端清空自己的曲线，在这里更新曲线 */
    public void onAnnotationClearNotify(String j_boardId, String j_userId) {
        if (drawView != null) {
            drawView.updateAnnotation(annotationInternal.getAnnotationList(whiteboard._boardId));
        }
    }

    /********************************************************************************   WhiteboardToolView 的监听器：展开、折叠、工具选择等   ****************************************************************************************************/
    @Override
    public void onToolbarListener(WhiteboardToolView.DrawToolbarObj bean) {
        if (drawView != null) {
            drawView.setToolType(bean);
        }
    }

    @Override
    public void isDrawToolbarVisible(boolean isVisible) {
        if (drawView != null) {
            drawView.setDrawToolViewVisible(isVisible);
        }
        if (drawToolbarListener != null) {
            drawToolbarListener.isDrawToolbarVisibleNotify(isVisible);
        }
    }

    /********************************************************************************   WhiteboardDrawView 的监听器：通知别人更新画图   ****************************************************************************************************/
    /** 自己画完后通知别人 */
    @Override
    public void onUploadAddAnnotation(String boardId, String annotationStr) {
        if (annotationInternal != null) {
            annotationInternal.addAnnotationObj(boardId, annotationStr);
        }
    }

    /** 本地删除线或箭头通知远端 */
    @Override
    public void onUploadRemoveAnnotation(String boardId, String annotationId) {
        if (annotationInternal != null) {
            annotationInternal.removeAnnotationObj(boardId, annotationId);
        }
    }

    /** 清空白板 */
    @Override
    public void onClearAnnotations(String boardId) {
        if (annotationInternal != null) {
            annotationInternal.clearAnnotations(boardId);
        }
    }

    /** 撤销 */
    @Override
    public void onUndo(String boardId) {
        if (annotationInternal != null) {
            annotationInternal.undo(boardId);
        }
    }

    /** 重绘 */
    @Override
    public void onRedo(String boardId) {
        if (annotationInternal != null) {
            annotationInternal.redo(boardId);
        }
    }
}
