package com.csdn.harmonyfirstpro.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.global.resource.RawFileDescriptor;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainAbilitySlice extends AbilitySlice implements Player.IPlayerCallback {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    private Player mVideoContainer;
    private HiLogLabel TAG = new HiLogLabel(0,0,"MainAbilitySlice");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        mVideoContainer = new Player(this);
        mVideoContainer.setPlayerCallback(this);

        copyVideoFile();

        try {
            File distDir = getDistributedDir();
            String filePath = distDir + File.separator + "video.mp4";
            File videoFile = new File(filePath);
            if (videoFile.exists()) {
                FileInputStream inputStream = new FileInputStream(filePath);
                mVideoContainer.setSource(new Source(inputStream.getFD()));
                mVideoContainer.prepare();
            } else {
//                mHandler.sendEvent(MESSAGE_UPDATE_PLAY_VIDEO, DELAY_TIME);
            }
        } catch (IOException ioException) {
            HiLog.error(TAG, "IOException");
        }

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("Hello World");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (mVideoContainer!=null) {
            mVideoContainer.pause();
        }
    }

    public void copyVideoFile() {
        FileOutputStream outFile = null;
        FileInputStream inFile = null;
        RawFileDescriptor rfd = null;
        int FILE_BUFFER_SIZE = 100 * 1024;
        try {
            // 读取视频文件在hap包的起始位置和视频文件大小
            rfd = getResourceManager().getRawFileEntry("resources/rawfile/video.mp4").openRawFileDescriptor();
            long start = rfd.getStartPosition();
            inFile = new FileInputStream(rfd.getFileDescriptor());
            inFile.skip(start);

            // 设置分布式视频文件保存的路径
            File distDir = getDistributedDir();
            String filePath = distDir + File.separator + "video.mp4";
            outFile = new FileOutputStream(filePath);

            byte[] buffer = new byte[FILE_BUFFER_SIZE];
            int len;
            while ((len = inFile.read(buffer, 0, FILE_BUFFER_SIZE)) > 0) {
                outFile.write(buffer, 0, len);
            }
        } catch (IOException ioException) {
            HiLog.error(TAG, "IOException");
        } finally {
            if (inFile != null) {
                try {
                    inFile.close();
                } catch (IOException ioException) {
                    HiLog.error(TAG, "IOException");
                }
            }
            if (outFile != null) {
                try {
                    outFile.close();
                } catch (IOException ioException) {
                    HiLog.error(TAG, "IOException");
                }
            }
            if (rfd != null) {
                try {
                    rfd.close();
                } catch (IOException ioException) {
                    HiLog.error(TAG, "IOException");
                }
            }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        if (mVideoContainer!=null) {
            mVideoContainer.play();
        }
    }


    //视频播放回调方法
    @Override
    public void onPrepared() {
        if (mVideoContainer!=null) {
            mVideoContainer.play();
        }
    }

    @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) {

    }
}
