package com.sheep.gamegroup.view.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sheep.gamegroup.model.entity.Video;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.MediaMetadataRetrieverUtil;
import com.sheep.jiuyan.samllsheep.R;

import java.io.File;
import java.util.Locale;

import rx.functions.Action1;


/**
 * Created by realicing on 2018/11/29.
 * realicing@sina.com
 * 显示本地视频文件对应的视频帧
 */
public class VideoFramesView extends RelativeLayout {
    public VideoFramesView(Context context) {
        super(context);
        initView();
    }

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

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

    private RecyclerView recyclerView;
    private View line_start;
    private View line_end;

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.custom_video_frames_view, this, true);
        recyclerView = findViewById(R.id.recyclerView);
        line_start = findViewById(R.id.line_start);
        line_end = findViewById(R.id.line_end);
        line_start.setOnTouchListener(new OnTouchListener() {
            private float lastEventX;

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                float maxX = line_end.getX();
                float minX = maxX - (firstEndX - firstStartX);//结束位置 - 可以设置的长度
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastEventX = event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float mx = Math.max(firstStartX, view.getX() + event.getX() - lastEventX);
                        if (mx > maxX)
                            mx = maxX;
                        if (mx < minX) {
                            maxX += mx - minX;
                            line_end.setX(maxX);
                            view.setX(mx);
                        } else {
                            view.setX(mx);
                        }
                        if (onTimeChangeListener != null) {
                            onTimeChangeListener.call((maxX - mx) * video.getDuration() / (rvW - lineW));
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                view.invalidate();
                return true;
            }
        });
        line_end.setOnTouchListener(new OnTouchListener() {
            private float lastEventX;

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                float minX = line_start.getX();
                float maxX = firstEndX - firstStartX + minX;
                float endX = firstStartX + rvW - lineW;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastEventX = event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float mx = Math.min(view.getX() + event.getX() - lastEventX, endX);
                        if (mx < minX)
                            mx = minX;
                        if (mx > maxX) {
                            minX += mx - maxX;
                            line_start.setX(minX);
                            view.setX(mx);
                        } else {
                            view.setX(mx);
                        }
                        if (onTimeChangeListener != null) {
                            onTimeChangeListener.call((mx - minX) * video.getDuration() / (rvW - lineW));
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                view.invalidate();
                return true;
            }
        });
    }

    //开头线的进度
    public float getStartPercent() {
        if (line_start != null && recyclerView != null) {
            return (line_start.getX() - firstStartX) / (rvW - lineW);
        }
        return 0;
    }

    private float firstStartX;//初始时开头的x位置
    private float firstEndX;//初始时结尾的x位置
    private int lineW;//开头与结尾线的宽度
    private int rvW;//图片列表的长度，减去lineW后==总进度的长度

    //结尾线的进度
    public float getEndPercent() {
        if (line_end != null && recyclerView != null) {
            return 1.0f - ((firstStartX + rvW - lineW - line_end.getX()) / (rvW - lineW));
        }
        return 0;
    }

    //总的进度
    public float getPercent() {
        if (line_end != null && recyclerView != null) {
            return (line_end.getX() - line_start.getX()) / (rvW - lineW);
        }
        return 0;
    }

    public String getLineInfo() {
        return String.format(Locale.CHINA, "firstStartX = %f, firstEndX = %f, startX = %f, endX = %f, lineW = %d, rvW = %d, startPercent = %f, endPercent = %f, percent = %f", firstStartX, firstEndX, line_start.getX(), line_end.getX(), line_end.getWidth(), recyclerView.getWidth(), getStartPercent(), getEndPercent(), getPercent());
    }

    private MediaMetadataRetrieverUtil mediaMetadataRetrieverUtil;

    public MediaMetadataRetrieverUtil getMediaMetadataRetrieverUtil() {
        return mediaMetadataRetrieverUtil;
    }

    private Video video;
    /**
     * 显示视频帧图片列表
     *
     * @param data 视频数据，包括地址与时长，宽高等
     * @return
     */
    public VideoFramesView initVideo(Video data) {
        if (data == null || data.getFilePath() == null) {
            LogUtil.println(VideoFramesView.class.getSimpleName(), "showList", "error", "videoPath is null");
            return this;
        }
        File videoFile = new File(data.getFilePath());
        if (!videoFile.exists()) {
            LogUtil.println(VideoFramesView.class.getSimpleName(), "showList", "error", "videoFile not exists");
            return this;
        }
        if (!videoFile.canRead()) {
            LogUtil.println(VideoFramesView.class.getSimpleName(), "showList", "error", "videoFile cant read");
            return this;
        }
        if (data.getDuration() < 1) {
            LogUtil.println(VideoFramesView.class.getSimpleName(), "showList", "error", "duration < 1");
            return this;
        }
        if (mediaMetadataRetrieverUtil == null) {
            mediaMetadataRetrieverUtil = new MediaMetadataRetrieverUtil(data.getFilePath(), data.getDuration());
        }
        this.video = data;
        return this;
    }

    /**
     * 显示视频帧图片列表
     */
    public VideoFramesView showVideoList() {
        if (mediaMetadataRetrieverUtil != null)
            mediaMetadataRetrieverUtil.initRv(recyclerView);
        return this;
    }

    private Action1<Float> onTimeChangeListener;

    public void setOnTimeChangeListener(Action1<Float> action1) {
        onTimeChangeListener = action1;
        postDelayed(new Runnable() {
            @Override
            public void run() {
                firstStartX = line_start.getX();
                firstEndX = line_end.getX();
                lineW = line_start.getWidth();
                rvW = recyclerView.getWidth();
                long duration = mediaMetadataRetrieverUtil.getDuration();
                if(maxVideoDuration > 0) {
                    if(duration > maxVideoDuration) {
                        firstEndX = firstStartX + ((maxVideoDuration * 1.0f / duration) * (rvW - lineW));
                        line_end.setX(firstEndX);
                    }
                }
                onTimeChangeListener.call((float) duration);
            }
        }, 100);
    }

    //开始的时间,单位秒
    public long getStartPoint() {
        return (long) (getStartPercent() * mediaMetadataRetrieverUtil.getDuration());
    }

    //总共的时长,单位秒
    public long getLineDuration() {
        return (long) (getPercent() * mediaMetadataRetrieverUtil.getDuration());
    }
    //结束的时间,单位秒
    public long getEndPoint() {
        return (long) (getEndPercent() * mediaMetadataRetrieverUtil.getDuration());
    }

    //最大剪切视频时长
    private long maxVideoDuration;
    public VideoFramesView setMaxDuration(long maxVideoDuration) {
        this.maxVideoDuration = maxVideoDuration;
        return this;
    }
    public void onDestroy(){
        if(recyclerView != null && recyclerView.getAdapter() instanceof BaseQuickAdapter){
            for (int i = 0; i < recyclerView.getAdapter().getItemCount(); i++) {
                Object object = ((BaseQuickAdapter) recyclerView.getAdapter()).getItem(i);
                if(object instanceof Bitmap){
                    try {
                        ((Bitmap) object).recycle();
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
