package com.xunao.effectdemo.adapter;

import static android.view.View.GONE;

import static com.mobile.auth.gatewayauth.utils.ReflectionUtils.getActivity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xunao.effectdemo.R;
import com.xunao.effectdemo.activity.AudioActivity;
import com.xunao.effectdemo.activity.VideoDemandActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {
    private final String TAG = "AudioAdapter";

    private AnimatorSet mRightOutSet; // 右出动画
    private AnimatorSet mLeftInSet; // 左入动画
    private List<String> wordList;
    private final Context context;
    private String url = "http://www.mobvcasting.com/android/audio/goodmorningandroid.mp3";

    private MediaPlayer mediaPlayer;
    private List<Integer> selectList = new ArrayList<>();

    public AudioAdapter(String[] wordList, Context context) {
        this.wordList = Arrays.asList(wordList);
        this.context = context;
    }

    @NonNull
    @Override
    public AudioAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_audio_demand,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AudioAdapter.ViewHolder viewHolder, int i) {

        if(selectList.contains(i)){
//            viewHolder.tvBack.setVisibility(GONE);
            Log.i(TAG, "点击后" + selectList.toString());
        }

        setAnimators(viewHolder, i);
        setCameraDistance(viewHolder);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnPreparedListener(mp -> Log.i(TAG,"onPrepared:准备完成"));
        mediaPlayer.setOnCompletionListener(mp -> Log.i(TAG,"OnCompletion:播放完成"));
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepareAsync();// 开始在后台缓冲音频文件并返回
        } catch (IOException e) {
            e.printStackTrace();
        }

        viewHolder.tvBack.setOnClickListener(v -> {
            Log.i(TAG, "点击前1" + selectList.toString() + "---" + i);
            if(!selectList.contains(i)){
                selectList.add(i);
                notifyItemChanged(i);
                flipCard(viewHolder);

                AudioActivity audioActivity = (AudioActivity) getActivity();
                assert audioActivity != null;
                audioActivity.addSelectedNum();

                Log.i(TAG, "点击前2" + selectList.toString() + "---" + i);
            }else {
                // 重播
//                mediaPlayer.stop();
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.seekTo(0);
                }else{
                    mediaPlayer.start();
                }
                Log.i(TAG, "重新播放音频");
            }
        });

    }



    @Override
    public int getItemCount() {
        Log.i(TAG, "getItemCount" + wordList.size());
        return wordList.isEmpty() ? 0 : wordList.size();
    }

    // 设置动画
    private void setAnimators(AudioAdapter.ViewHolder viewHolder, int pos) {
        mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.anim_out);
        mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.anim_in);

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

                if(mediaPlayer != null){
                    mediaPlayer.start();
                }
                Log.i(TAG, "播放音频");
            }
        });
    }

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

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

    public void release(){
        mediaPlayer.release();
        mediaPlayer = null;
        Log.i(TAG, "release");
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvFront;
        private TextView tvBack;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvFront = itemView.findViewById(R.id.tv_previous_audio);
            tvBack = itemView.findViewById(R.id.tv_back_audio);
        }
    }
}
