package com.xunao.effectdemo.fragment;

import static android.view.View.GONE;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYVideoProgressListener;
import com.xunao.effectdemo.R;
import com.xunao.effectdemo.activity.VideoDemandActivity;
import com.xunao.effectdemo.view.VideoPreviewPlay;

public class VideoDemandFragment extends Fragment {
    private final String TAG = "VideoDemandFragment";

    private AnimatorSet mRightOutSet; // 右出动画
    private AnimatorSet mLeftInSet; // 左入动画
    private RelativeLayout rlContainer;
    private VideoPreviewPlay videoFront;
    private TextView tvBack;
    private boolean mIsShowBack = true;
    private String url = "http://v3.cztv.com/cztv/vod/2018/06/28/7c45987529ea410dad7c088ba3b53dac/h264_1500k_mp4.mp4";
//    private String url = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
    private String data;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_demand, container, false);
        videoFront = view.findViewById(R.id.video_front);
        videoFront.setUp(url, true, "null");
        videoFront.getTitleTextView().setVisibility(View.GONE);
        videoFront.getFullscreenButton().setVisibility(View.GONE);
        videoFront.getBackButton().setVisibility(GONE);
        videoFront.setGSYVideoProgressListener(new GSYVideoProgressListener() {
            @Override
            public void onProgress(int progress, int secProgress, int currentPosition, int duration) {
                Log.i(TAG, "onProgress:" +progress + "----" + secProgress + "----" + currentPosition + "---" + duration);
                if(progress == 100){
                    // 翻转到缩略图并且记录改项已完成
                }
            }
        });
//        videoFront.startPlayLogic();
        tvBack = view.findViewById(R.id.tv_back);
        rlContainer = view.findViewById(R.id.rl_container);
        tvBack.setOnClickListener(v -> {
            flipCard();

            VideoDemandActivity videoDemandActivity = (VideoDemandActivity) getActivity();
            assert videoDemandActivity != null;
            videoDemandActivity.addSelectedNum();

            videoFront.setUp(url, true, "null");
            videoFront.startPlayLogic();
        });
        setAnimators(); // 设置动画
        setCameraDistance(); // 设置镜头距离

        assert getArguments() != null;
        data = getArguments().getString("data");
        tvBack.setText("背面" + data);
        return view;
    }

    // 设置动画
    private void setAnimators() {
        mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(getContext(), R.animator.anim_out);
        mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(getContext(), R.animator.anim_in);

        // 设置点击事件
        mRightOutSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                tvBack.setClickable(false);
            }
        });
        mLeftInSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                tvBack.setClickable(true);
                tvBack.setVisibility(GONE);
            }
        });
    }

    // 改变视角距离, 贴近屏幕
    private void setCameraDistance() {
        int distance = 1000000;
        float scale = getContext().getResources().getDisplayMetrics().density * distance;
        videoFront.setCameraDistance(scale);
        tvBack.setCameraDistance(scale);
        Log.i(TAG, "setCameraDistance:改变视角距离" + scale);
    }

    // 翻转卡片
    public void flipCard() {
        // 正面朝上
        if (!mIsShowBack) {
            mRightOutSet.setTarget(videoFront);
            mLeftInSet.setTarget(tvBack);
            mRightOutSet.start();
            mLeftInSet.start();
            mIsShowBack = true;
            Log.i(TAG, "flipCard:正面朝上");
        } else { // 背面朝上
            mRightOutSet.setTarget(tvBack);
            mLeftInSet.setTarget(videoFront);
            mRightOutSet.start();
            mLeftInSet.start();
            mIsShowBack = false;
            Log.i(TAG, "flipCard:背面朝上");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        videoFront.onVideoResume();
        Log.i(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        videoFront.onVideoPause();
        Log.i(TAG, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        Log.i(TAG, "onDestroy");
    }


    // 当前fragment 可见->不可见 或 不可见->可见时 会调用这个方法
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: " + isVisibleToUser + "--" + data);
        // 不可见时暂停视频
        if(!isVisibleToUser && videoFront != null){
            videoFront.onVideoPause();
        }
    }
}
