package com.czg.cashier_reserve.channel;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.czg.cashier_reserve.ContextHolder;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class EventChannelManager {
	public static Map<String, BroadcastReceiver> receiverMap = new HashMap<>();
	public static List<BroadcastReceiver> broadcastReceiverList = new ArrayList<>();

	public static void addEventChannel(BinaryMessenger messenger) {
		getCallLogCallback(messenger);
		callStatusChange(messenger);
	}

	public static void callStatusChange(BinaryMessenger messenger) {
		if (messenger == null) {
			return;
		}

		EventChannel callStatusChannel = new EventChannel(messenger, ChannelNames.getChannelName(ChannelNames.CALL_STATUS_CHANGE));

		callStatusChannel.setStreamHandler(new EventChannel.StreamHandler() {
			private BroadcastReceiver broadCast;

			@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
			@Override
			public void onListen(Object o, EventChannel.EventSink eventSink) {
				broadCast = EventChannelManager.getBroadCast(eventSink);
				Context context = ContextHolder.getInstance().getContext();
				IntentFilter intentFilter = new IntentFilter(EventChannelManager.Action.CALL_STATUS_CHANGE);
				context.registerReceiver(broadCast, intentFilter, Context.RECEIVER_NOT_EXPORTED);
				receiverMap.put(EventChannelManager.Action.CALL_STATUS_CHANGE, broadCast);
			}

			@Override
			public void onCancel(Object o) {
				Context context = ContextHolder.getInstance().getContext();
				context.unregisterReceiver(broadCast);
				broadcastReceiverList.remove(broadCast);
				broadCast = null;
			}
		});
	}

	public static void getCallLogCallback(BinaryMessenger messenger) {
		if (messenger == null) {
			return;
		}

		EventChannel callLogChannel = new EventChannel(messenger, ChannelNames.getChannelName(ChannelNames.CALL_LOG_CALLBACK));

		callLogChannel.setStreamHandler(new EventChannel.StreamHandler() {
			private BroadcastReceiver broadCast;

			@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
			@Override
			public void onListen(Object o, EventChannel.EventSink eventSink) {
				broadCast = EventChannelManager.getBroadCast(eventSink);
				Context context = ContextHolder.getInstance().getContext();
				IntentFilter intentFilter = new IntentFilter(EventChannelManager.Action.GET_CALL_LOG_FINISH);
				context.registerReceiver(broadCast, intentFilter, Context.RECEIVER_NOT_EXPORTED);
				receiverMap.put(EventChannelManager.Action.GET_CALL_LOG_FINISH, broadCast);
			}

			@Override
			public void onCancel(Object o) {
				Context context = ContextHolder.getInstance().getContext();
				context.unregisterReceiver(broadCast);
				broadcastReceiverList.remove(broadCast);
				broadCast = null;
			}
		});
	}

	public static BroadcastReceiver getBroadCast(EventChannel.EventSink eventSink) {
		BroadcastReceiver b = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (Action.GET_CALL_LOG_FINISH.equals(action)) {
					String extra = intent.getStringExtra("callLog");
					eventSink.success(extra);
				} if (Action.CALL_STATUS_CHANGE.equals(action)) {
					String extra = intent.getStringExtra("callStatus");
					eventSink.success(extra);
				} else {
					eventSink.error("没有该事件", null, null);
				}
			}
		};

		broadcastReceiverList.add(b);
		return b;
	}

	// 这里假设Action是一个定义了相关常量的类，示例如下，你可以根据实际情况调整
	public static class Action {
		public static final String GET_CALL_LOG_FINISH = ChannelNames.BASE_NAME + "get_call_log_finish";
		public static final String CALL_STATUS_CHANGE = ChannelNames.BASE_NAME + "call_status_change";
	}
}
