// Copyright © 2022 BytePlusRTC All rights reserved.
// SPDX-License-Identifier: MIT

package com.volcengine.reactnative.vertc;

import static com.volcengine.VolcApiEngine.runtime.NativeVariableManager.*;

import static android.content.Context.MEDIA_PROJECTION_SERVICE;
import android.os.Build;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.ss.bytertc.base.media.screen.RXScreenCaptureService;

import java.util.function.Function;

public class ScreenCaptureHelper {
  static final int SCREEN_RECORD_REQUEST_CODE = 1010;
  static ReactApplicationContext reactApplicationContext;
  
  private Function<Intent, Integer> mCallback;
  
  static public void registerReactApplicationContext(ReactApplicationContext context) {
    reactApplicationContext = context;
  }

  public void setup() {
    ScreenCaptureHelper.reactApplicationContext.addActivityEventListener(new ActivityEventListener() {
      @Override
      public void onActivityResult(Activity activity, int requestCode, int resultCode, @Nullable Intent intent) {
        if (requestCode != SCREEN_RECORD_REQUEST_CODE) {
          return;
        }

        // success
        if (resultCode == AppCompatActivity.RESULT_OK) {
          if (mCallback != null) {
            Context context = ScreenCaptureHelper.reactApplicationContext;
            Intent _intent = new Intent();
            _intent.putExtra(RXScreenCaptureService.KEY_LARGE_ICON, R.drawable.launcher_quick_start);
            _intent.putExtra(RXScreenCaptureService.KEY_SMALL_ICON, R.drawable.launcher_quick_start);
            _intent.putExtra(RXScreenCaptureService.KEY_LAUNCH_ACTIVITY, activity.getClass().getCanonicalName());
            _intent.putExtra(RXScreenCaptureService.KEY_CONTENT_TEXT, "Capturing Screen");
            _intent.putExtra(RXScreenCaptureService.KEY_RESULT_DATA, intent);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              context.startForegroundService(RXScreenCaptureService.getServiceIntent(context, RXScreenCaptureService.COMMAND_LAUNCH, _intent));
            } else {
              context.startService(RXScreenCaptureService.getServiceIntent(context, RXScreenCaptureService.COMMAND_LAUNCH, _intent));
            }
            mCallback.apply(_intent);
            mCallback = null;
          }
          return;
        } 
        
        // error        
        if (mCallback != null) {
          mCallback = null;
        }
      }

      @Override
      public void onNewIntent(Intent intent) {
      }
    });
  }

  public void registerIntentCallback(Function<Intent, Integer> cb) {
    this.mCallback = cb;
  }
  
  public void getScreenIntent() {
    MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) ScreenCaptureHelper.reactApplicationContext.getSystemService(MEDIA_PROJECTION_SERVICE);
    Intent permissionIntent =  mediaProjectionManager.createScreenCaptureIntent();
    ScreenCaptureHelper.reactApplicationContext.getCurrentActivity().startActivityForResult(permissionIntent, SCREEN_RECORD_REQUEST_CODE);
  }
}
