package com.gnhz.clinic.eitc_erm_app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("czb", "MainActivity onCreate");
    }

    private static final String CHANNEL = "com.gnhz.clinic.eitc_erm_app/channel";

    /**
     * Flutter 引擎配置器，用于配置 Flutter 引擎并设置通信通道
     */
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        // 注册 Flutter 插件
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        // 创建一个新的方法通道，与 Dart 代码进行通信
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            // 处理方法调用
                            if (call.method.equals("launchApp")) { // 只调方法
                                launchApp();
                                result.success("App Launched");
                            } else if (call.method.equals("launchAppOne")) { // 传一个参数
                                String param1 = call.argument("param1");
                                launchApp1(param1);
                                result.success("App Launched 1");
                            } else if (call.method.equals("launchAppTwo")) { // 传多个

                                String param1 = call.argument("param1");
                                String param2 = call.argument("param2");
                                launchApp2(param1, param2);

                                result.success("App Launched 2");
                            } else if (call.method.equals("launchAppObject")) { // 传对象

                                launchApp3(call.arguments());
                                Map<String, Object> person = new HashMap<>(); // 返回一个对象
                                person.put("name", "John Doe");
                                person.put("age", 30);
                                result.success(person);

                            } else {
                                // 如果收到其他方法调用，则返回未实现的错误给 Dart 代码
                                result.notImplemented();
                            }
                        });
    }

    @SuppressLint("LongLogTag") // 用于告诉编译器忽略指定代码段的特定警告或lint检查
    private void launchApp() {
       /* Intent intent = new Intent();
        intent.setClass(this, VideoSDK.class);
        startActivity(intent);*/
        System.out.println("--------------1访问成功--------------------");

        Log.d(CHANNEL, "The value of number is:是否 ");

        System.out.println("--------------访问成功--------------------");
    }

    private void launchApp1(String a) {
        System.out.println("--------访问成功----------一个参数----------" + a);

    }

    private void launchApp2(String a, String b) {
        System.out.println(b + "--------访问成功----------2个参数----------" + a);

    }

    private void launchApp3(Map<String, Object> arguments) {
        String name = (String) arguments.get("name");
        int age = (int) arguments.get("age");
        System.out.println(age + "---age--launchApp3---------1访问成功-------------------name-" + name);

    }

}