package com.bokecc.livemodule.base;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * @author cc
 * @Description 在 intercept 中冒泡事件给目标，用于子级 view 消费了事件，其他高层级 view 需要处理事件的情况
 */
public class PopEventRelativeLayout extends RelativeLayout {


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

    public PopEventRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PopEventRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private static final String TAG = "PopEventRelativeLayout";


    private boolean intercept = false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.i(TAG,"P onInterceptTouchEvent:"+ev.getAction());

        if (intercept){
            Log.i(TAG,"P onInterceptTouchEvent:true");
            return true;
        }
        boolean b = super.onInterceptTouchEvent(ev);
        Log.i(TAG,"P onInterceptTouchEvent:b="+b);
        return b;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.i(TAG,"P dispatchTouchEvent:"+ev.getAction());
        if (eventListener!=null){
            eventListener.onEvent(ev);
        }
        boolean b = super.dispatchTouchEvent(ev);
        Log.i(TAG,"P dispatchTouchEvent:"+b);

        return b;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG,"P onTouchEvent:"+event.getAction());
        return true;
    }
    private EventListener eventListener;

    public void setEventListener(EventListener eventListener) {
        this.eventListener = eventListener;
    }

    public void setIntercept(boolean intercept){
        this.intercept = intercept;
    }

    public interface EventListener {
        void onEvent(MotionEvent event);
    }
}