package com.csdn.harmonyfirstpro;

import com.csdn.harmonyfirstpro.slice.VideoAbilitySlice;
import com.csdn.harmonyfirstpro.view.VideoView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import com.csdn.harmonyfirstpro.ResourceTable;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class VideoAbility extends Ability  implements  SurfaceOps.Callback{

    private VideoView videoView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_video_layout);
        HiLog.debug(new HiLogLabel(0,0,"XXXXXXXXX"),"跳转了");
        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.getTopWindow().get();
        window.setTransparent(true);

        DirectionalLayout videoContent = (DirectionalLayout) findComponentById(com.csdn.harmonyfirstpro.ResourceTable.Id_video_content);

        DependentLayout.LayoutConfig lpVideo = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
        videoView = new VideoView(this);
        videoView.setCallback(this);
        videoContent.addComponent(videoView, lpVideo);

        Button videoBt = (Button) findComponentById(ResourceTable.Id_video_bt);
        videoBt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.debug(new HiLogLabel(0,0,"XXXXXXXXX"),"点击了");
                if(videoView.getState() == VideoView.STATE_PLAYING){
                    videoView.pause();
                    videoBt.setText("开始");
                }else{
                    videoView.start();
                    videoBt.setText("暂停");
                }
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
        videoView.playAssets("resources/rawfile/trailer.mp4", true, surfaceOps);
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {
        videoView.stop();
    }
}
