package xn.xxp.main.monitor;

import android.app.Dialog;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.hikvision.hatomplayer.DefaultHatomPlayer;
import com.hikvision.hatomplayer.HatomPlayer;
import com.hikvision.hatomplayer.PlayCallback;
import com.hikvision.hatomplayer.PlayConfig;
import com.hikvision.hatomplayer.core.Quality;

import core.util.FastClickDelegate;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import xn.xxp.R;


public class MonitorDialog extends Dialog {

    private final MonitorInfo monitorInfo;
    private HatomPlayer hatomPlayer;
    private TextView textView;

    public MonitorDialog(@NonNull Context context, MonitorInfo monitorInfo, OnDismissListener dismissListener) {
        super(context, R.style.FullScreenDialog);
        this.monitorInfo = monitorInfo;
        setOnDismissListener(dismissListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_monitor);
        Window window = getWindow();
        if (window != null) {
            window.setWindowAnimations(R.style.DialogAnimation);
        }
        initVideoPlayer();
    }

    private void initVideoPlayer() {
        TextureView textureView = findViewById(R.id.textureView);
        textView = findViewById(R.id.back);
        textView.setOnClickListener(new FastClickDelegate(new Function1<View, Unit>() {
            @Override
            public Unit invoke(View view) {
                textView.setText("正在退出...");
                releasePlayer();
                return null;
            }
        }));
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                    @Override
                    public Object doInBackground() throws Throwable {
                        hatomPlayer = new DefaultHatomPlayer();
                        PlayConfig playConfig = new PlayConfig();
                        playConfig.hardDecode = true;
                        playConfig.privateData = true;
                        hatomPlayer.setPlayConfig(playConfig);
                        hatomPlayer.setPlayStatusCallback(new PlayCallback.PlayStatusCallback() {
                            @Override
                            public void onPlayerStatus(@NonNull PlayCallback.Status status, String s) {
                                LogUtils.d(status, s);
                            }
                        });
                        hatomPlayer.setSurfaceTexture(surface);
                        hatomPlayer.setDataSource(monitorInfo.getMonitorVo().streamUrl, null);
                        hatomPlayer.changeStream(Quality.STREAM_SUPER_CLEAR);
                        hatomPlayer.start();
                        return null;
                    }

                    @Override
                    public void onSuccess(Object result) {

                    }
                });
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        releasePlayer();
    }

    /**
     * 释放播放器资源
     */
    private void releasePlayer() {
        if (hatomPlayer != null) {
            dismiss();
            ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                @Override
                public Object doInBackground() throws Throwable {
                    hatomPlayer.stop(); // 释放播放器
                    return null;
                }

                @Override
                public void onSuccess(Object result) {
                    dismiss();
                }
            });
            textView.setOnClickListener(null);
            hatomPlayer = null; // 置空防止内存泄漏
        } else {
            dismiss();
        }
    }
}
