package com.eitchsyh.instrument.eitc_erm_dental_flutter;

import android.content.Intent;
import android.content.IntentSender;

import androidx.annotation.NonNull;

import io.flutter.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity implements
        FileController.OnDeleteFileRequestPermissionListener
{

    private FlutterEngine flutterEngine;
    private VideoController videoController;
    private FileController fileController;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine)
    {
        super.configureFlutterEngine(flutterEngine);
        this.videoController = new VideoController(this.getApplicationContext(), flutterEngine);
        this.fileController = new FileController(this.getApplicationContext(), flutterEngine, this);
        HookFlutterClipBordUtil.hookClipBoard(flutterEngine);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (videoController != null)
        {
            videoController.release();
        }
        videoController = null;
        if (fileController != null)
        {
            fileController.release();
        }
        fileController = null;
    }

    @Override
    public void onDeleteFileRequestPermission(IntentSender sender)
    {
        try
        {
            this.startIntentSenderForResult(sender, 1000, null, 0, 0, 0, null);

        }
        catch (Exception e)
        {
            Log.e("APP", "发送删除文件权限请求异常", e);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000)
        {
            this.fileController.onPermissionDeleteFile(resultCode == RESULT_OK);
        }
    }
}
