package com.csdn.harmonyfirstpro.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.player.Player;
import java.util.Optional;

public class VideoView extends SurfaceProvider implements Player.IPlayerCallback {

    public static final int STATE_INIT = 1000;
    public static final int STATE_PLAYING = 1001;
    public static final int STATE_PAUSE = 1002;
    public static final int STATE_STOP = 1003;

    private Player player;
    private SurfaceOps surfaceHolder;
    private int state = STATE_INIT;
    Context context;

    public VideoView(Context context) {
        super(context);
        initVideoView(context);
    }

    public VideoView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initVideoView(context);
    }

    public VideoView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initVideoView(context);
    }

    private void initVideoView(Context context) {
        this.context = context;
        player = new Player(getContext());
        player.setPlayerCallback(this);
        Optional<SurfaceOps> surfaceHolderOptional = getSurfaceOps();
        surfaceHolder = surfaceHolderOptional.get();
        state = STATE_INIT;
    }

    public void setCallback(SurfaceOps.Callback callback) {
        if (surfaceHolder != null) {
            surfaceHolder.addCallback(callback);
        }
    }

    public void playAssets(String fileName, boolean isLooping, SurfaceOps holder) {
        try {
//播放本地视频：
            player.setSource(getContext().getResourceManager().getRawFileEntry(fileName).openRawFileDescriptor());
//播放在线视频：
//            player.setSource(new Source(fileName));
            player.setVideoSurface(holder.getSurface());
            player.enableSingleLooping(isLooping);
            player.enableScreenOn(true);
            player.prepare();
            initPlayViewSize();
            player.play();
//            if (state != STATE_INIT) {
//                player.rewindTo(currentTime * 1000);
//            }
            state = STATE_PLAYING;
//            handler.sendEvent(InnerEvent.get(MESSAGE_UPDATE_PLAY_TIME));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initPlayViewSize() {
        HiLog.debug(new HiLogLabel(0,0,"XXXXXXXXXXX"),"context.getAbilityInfo().getDefaultFormHeight():");
        int videoWidth = player.getVideoWidth();
        int videoHeight = player.getVideoHeight();
//        Log.hiLog("videoWidth:" + videoWidth + ", videoHeight:" + videoHeight);
        if (videoWidth < videoHeight) {
            double scale = 1080 * 1.f / videoHeight;
            double currHeight = videoHeight * scale;
            double currWidth = videoWidth * scale;
            setHeight(((int) currHeight));
            setWidth(((int) currWidth));
        } else {
            double scale = 1920 * 1.f / videoWidth;
            double currHeight = videoHeight * scale;
            double currWidth = videoWidth * scale;
            setHeight(((int) currHeight));
            setWidth(((int) currWidth));
        }
    }

    public void pause() {
        if (state == STATE_PLAYING) {
            player.pause();
            state = STATE_PAUSE;
        }
    }

    public void start() {
        if (state == STATE_PAUSE) {
            player.play();
            state = STATE_PLAYING;
        }
    }

    public int getState(){
        return state;
    }

    public void stop() {
        if (player!=null) {
            player.stop();
            state = STATE_STOP;
        }
    }

    @Override
    public void onPrepared() {

    }

    @Override
    public void onMessage(int i, int i1) {

    }

    @Override
    public void onError(int i, int i1) {

    }

    @Override
    public void onResolutionChanged(int i, int i1) {

    }

    @Override
    public void onPlayBackComplete() {

    }

    @Override
    public void onRewindToComplete() {

    }

    @Override
    public void onBufferingChange(int i) {

    }

    @Override
    public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

    }

    @Override
    public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

    }
}
