package com.xxfc.discovery.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dueeeke.videoplayer.player.VideoView;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.statusbar.StatusBarUtil;
import com.ruiwenliu.wrapper.util.ViewHolder;
import com.ruiwenliu.wrapper.util.glide.GlideApp;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.xxfc.discovery.R;
import com.xxfc.discovery.R2;
import com.xxfc.discovery.adapter.DiscoveryVideoAdapter;
import com.xxfc.discovery.bean.beam.DataUtil;
import com.xxfc.discovery.bean.beam.VideoBean;
import com.xxfc.discovery.dialog.DiscoveryVideoDialog;
import com.xxfc.discovery.presenter.DiscoveryPresenter;
import com.xxfc.discovery.widget.OnViewPagerListener;
import com.xxfc.discovery.widget.TikTokController;
import com.xxfc.discovery.widget.ViewPagerLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 短视频
 */
public class ShortVideoActivity extends BaseStatusActivity<DiscoveryPresenter> {

    @BindView(R2.id.rv_short_video)
    RecyclerView rvShortVideo;
    @BindView(R2.id.iv_item_video_play)
    ImageView ivVideoPlay;


    private VideoView mVideoView;
    private TikTokController mTikTokController;
    private int mCurrentPosition;
    private List<VideoBean> mVideoList;


    public static Intent getIntent(Context context) {
        return new Intent(context, ShortVideoActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setRootViewFitsSystemWindows(mActivity, false);

        mVideoView = new VideoView(this);
        mVideoView.setLooping(true);
        mTikTokController = new TikTokController(this);
        mVideoView.setVideoController(mTikTokController);


        DiscoveryVideoAdapter tikTokAdapter = new DiscoveryVideoAdapter();
        ViewPagerLayoutManager layoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);

        rvShortVideo.setLayoutManager(layoutManager);
        rvShortVideo.setAdapter(tikTokAdapter);

        layoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                //自动播放第一条
                startPlay(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurrentPosition == position) {
                    mVideoView.release();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) return;
                startPlay(position);
                mCurrentPosition = position;
            }
        });


        tikTokAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int id = view.getId();
                if (id == R.id.ll_item_video_comment) {
                    showCommentDialog();
                } else if (id == R.id.ll_item_video_share) {

                }
            }
        });


        mVideoList = DataUtil.getTikTokVideoList();
        tikTokAdapter.addData(mVideoList);
    }

    /**
     * 评论
     */
    private void showCommentDialog() {
        new DiscoveryVideoDialog(mActivity) {
            @Override
            public void helper(ViewHolder helper) {
                super.helper(helper);

            }
        }.show();
    }



    @Override
    protected int setLayout() {
        return R.layout.activity_short_video;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        showTitle(false);
    }


    private void startPlay(int position) {
        View itemView = rvShortVideo.getChildAt(0);
        FrameLayout frameLayout = itemView.findViewById(R.id.container);
        GlideApp.with(this)
                .load(mVideoList.get(position).getThumb())
                .placeholder(android.R.color.white)
                .into(mTikTokController.getThumb());

        ViewParent parent = mVideoView.getParent();
        if (parent instanceof FrameLayout) {
            ((FrameLayout) parent).removeView(mVideoView);
        }
        frameLayout.addView(mVideoView);
        mVideoView.setUrl(mVideoList.get(position).getUrl());
        mVideoView.setScreenScale(VideoView.SCREEN_SCALE_CENTER_CROP);
        mVideoView.start();
    }


    @Override
    public void onShowResult(int requestType, BaseBean result) {

    }


    @Override
    public void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mVideoView.resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mVideoView.release();
    }

    @OnClick(R2.id.iv_item_video_play)
    public void onViewClicked(View v) {
        int id = v.getId();
        if (id == R.id.iv_item_video_play) {
//            if (mVideoView.isPlaying()) {
//                mVideoView.pause();
//                ivVideoPlay.animate().alpha(1.0f).start();
//            } else {
//                mVideoView.resume();
//                ivVideoPlay.animate().alpha(0f).start();
//            }
        }
    }
}
