package com.app.duck.util;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.view.Surface;

import androidx.core.app.NotificationCompat;

import java.io.File;
import java.io.IOException;

/**
 * author: hx
 * created on: 2024/10/31 11:08
 * description:
 */
public class ScreenCaptureService extends Service {
    public static MediaProjection mediaProjection;
    private VirtualDisplay virtualDisplay;
    private MediaRecorder mediaRecorder;

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 启动前台服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            startForeground(1, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
        }
        initMediaRecorder();
        startScreenRecording();

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private Notification getNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "screen_record_channel")
                .setContentTitle("Screen Recording")
                .setContentText("Recording your screen")
                .setPriority(NotificationCompat.PRIORITY_HIGH);
        return builder.build();
    }

    private void initMediaRecorder() {
        File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "recorded_video.mp4");
        mediaRecorder.setOutputFile(outputFile.getAbsolutePath());
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setOutputFile(outputFile.getAbsolutePath());
        mediaRecorder.setVideoSize(1080, 1920); // 设置分辨率
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
        mediaRecorder.setVideoFrameRate(30);

        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void startScreenRecording() {
        Surface surface = mediaRecorder.getSurface();
        virtualDisplay = mediaProjection.createVirtualDisplay("ScreenRecord",
                1080, 1920, 320,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                surface, null, null);

        mediaRecorder.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaRecorder.stop();
        mediaRecorder.release();
        virtualDisplay.release();
        mediaProjection.stop();
    }
}
