package com.kk.taurus.avplayer.play;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewTreeObserver;

import com.kk.taurus.avplayer.adapter.VideoListAdapter;
import com.kk.taurus.avplayer.bean.VideoBean;
import com.kk.taurus.avplayer.utils.PUtil;
import com.kk.taurus.playerbase.entity.DataSource;
import com.kk.taurus.playerbase.log.PLog;

/**
 * Created by Taurus on 2018/4/15.
 */

public class ListPlayLogic {

    private Context mContext;
    private RecyclerView mRecycler;
    private VideoListAdapter mAdapter;

    private int mScreenH;

    private int mPlayPosition = -1;
    private int mVerticalRecyclerStart;

    public ListPlayLogic(Context context, RecyclerView recycler, VideoListAdapter adapter){
        this.mContext = context;
        this.mRecycler = recycler;
        this.mAdapter = adapter;
        init();
    }

    private void init() {
        mScreenH = PUtil.getScreenH(mRecycler.getContext());
        mRecycler.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] location = new int[2];
                mRecycler.getLocationOnScreen(location);
                mVerticalRecyclerStart = location[1];
                mRecycler.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE){
                    int itemVisibleRectHeight = getItemVisibleRectHeight(mPlayPosition);
                    if(itemVisibleRectHeight <= 0){
                        PLog.d("ListPlayLogic","onScrollStateChanged stop");
                        AssistPlayer.get().stop();
                        mAdapter.notifyItemChanged(mPlayPosition);
                        mPlayPosition = -1;
                    }
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });
    }

    public void updatePlayPosition(int position){
        notifyPrePosition();
        this.mPlayPosition = position;
    }

    private void notifyPrePosition() {
        if(mPlayPosition>=0)
            mAdapter.notifyItemChanged(mPlayPosition);
    }

    public void attachPlay(){
        mRecycler.post(new Runnable() {
            @Override
            public void run() {
                VideoListAdapter.VideoItemHolder itemHolder = getItemHolder(mPlayPosition);
                if(itemHolder!=null){
                    PLog.d("ListPlayLogic","attachPlay...");
                    AssistPlayer.get().play(itemHolder.layoutContainer, null);
                }
            }
        });
    }

    public void playPosition(final int position){
        VideoBean item = getItem(position);
        final DataSource dataSource = new DataSource(item.getPath());
        dataSource.setTitle(item.getDisplayName());
        final VideoListAdapter.VideoItemHolder holder = getItemHolder(position);
        if(holder!=null){
            mRecycler.post(new Runnable() {
                @Override
                public void run() {
                    PLog.d("ListPlayLogic","playPosition : position = " + position);
                    AssistPlayer.get().play(holder.layoutContainer, dataSource);
                }
            });
        }
    }

    private VideoBean getItem(int position){
        return mAdapter.getItem(position);
    }

    private VideoListAdapter.VideoItemHolder getItemHolder(int position){
        RecyclerView.ViewHolder viewHolder = mRecycler.findViewHolderForLayoutPosition(position);
        if(viewHolder!=null && viewHolder instanceof VideoListAdapter.VideoItemHolder){
            return ((VideoListAdapter.VideoItemHolder)viewHolder);
        }
        return null;
    }

    /**
     * 获取Item中渲染视图的可见高度
     * @param position
     * @return
     */
    private int getItemVisibleRectHeight(int position){
        VideoListAdapter.VideoItemHolder itemHolder = getItemHolder(position);
        if(itemHolder==null)
            return 0;
        int[] location = new int[2];
        itemHolder.layoutBox.getLocationOnScreen(location);
        int height = itemHolder.layoutBox.getHeight();

        int visibleRect;
        if(location[1] <= mVerticalRecyclerStart){
            visibleRect = location[1] - mVerticalRecyclerStart + height;
        }else{
            if(location[1] + height >= mScreenH){
                visibleRect = mScreenH - location[1];
            }else{
                visibleRect = height;
            }
        }
        return visibleRect;
    }

    /**
     * 获取两个索引条目中渲染视图可见高度最大的条目
     * @param position1
     * @param position2
     * @return
     */
    private int getVisibleRectMaxPosition(int position1, int position2){
        VideoListAdapter.VideoItemHolder itemHolder1 = getItemHolder(position1);
        VideoListAdapter.VideoItemHolder itemHolder2 = getItemHolder(position2);
        if(itemHolder1==null && itemHolder2==null){
            return RecyclerView.NO_POSITION;
        }
        if(itemHolder1==null){
            return position2;
        }
        if(itemHolder2==null){
            return position1;
        }
        int visibleRect1 = getItemVisibleRectHeight(position1);
        int visibleRect2 = getItemVisibleRectHeight(position2);
        return visibleRect1 >= visibleRect2?position1:position2;
    }

    /**
     * 判断给定的索引条目，渲染视图的可见高度是否满足播放条件.
     * @param position
     * @return
     */
    private boolean isVisibleRectAvailablePlay(int position){
        VideoListAdapter.VideoItemHolder itemHolder = getItemHolder(position);
        if(itemHolder==null)
            return false;
        int height = itemHolder.layoutBox.getHeight();
        return getItemVisibleRectHeight(position) > (height/2);
    }

    private boolean isCompleteVisibleRect(int position){
        VideoListAdapter.VideoItemHolder itemHolder = getItemHolder(position);
        if(itemHolder==null)
            return false;
        int height = itemHolder.layoutBox.getHeight();
        return getItemVisibleRectHeight(position) == height;
    }


}

@@ -1690,62 +1690,168 @@
     
+%7D%0A
     
-if(newState==RecyclerView.SCROLL_STATE_IDLE)%7B%0A    
+        @Override%0A            public void onScrolled(RecyclerView recyclerView, int dx, int dy) %7B%0A                super.onScrolled(recyclerView, dx, dy);%0A
     
@@ -1943,28 +1943,24 @@
             
-    
 if(itemVisib
@@ -1991,36 +1991,32 @@
                 
- 
-   
 PLog.d(%22ListPlay
@@ -2064,36 +2064,32 @@
                 
-   
- 
 AssistPlayer.get
@@ -2095,28 +2095,24 @@
 t().stop();%0A
-    
             
@@ -2182,20 +2182,16 @@
         
- 
-   
 mPlayPos
@@ -2222,204 +2222,9 @@
     
-    %7D%0A                %7D%0A            %7D%0A            @Override%0A            public void onScrolled(RecyclerView recyclerView, int dx, int dy) %7B%0A                super.onScrolled(recyclerView, dx, dy);%0A
+%7D
 %0A   

update demo