package com.czg.cashier_reserve.channel;

import com.czg.cashier_reserve.call.CallManager;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;

public class MessageChannel {
	public static void addMessageChannel(BinaryMessenger messenger) {
		callLogChannel(messenger);
		acceptCallChannel(messenger);
		rejectCallChannel(messenger);
		endCallChannel(messenger);
	}

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

		MethodChannel methodChannel = new MethodChannel(messenger, ChannelNames.getChannelName(ChannelNames.GET_CALL_LOG));

		methodChannel.setMethodCallHandler((call, result) -> {
			System.out.println("call.method: " + call.method);
			if (call.method.equals(ChannelNames.GET_CALL_LOG)) {
				System.out.println("加载通话记录");
				result.success(null);
				CallManager.loadCallLog();
			};
		});
	}

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

		MethodChannel methodChannel = new MethodChannel(messenger, ChannelNames.getChannelName(ChannelNames.ACCEPT_CALL));

		methodChannel.setMethodCallHandler((call, result) -> {
			System.out.println("call.method: " + call.method);
			if (call.method.equals(ChannelNames.ACCEPT_CALL)) {
				System.out.println("接听电话");
				result.success(null);
				CallManager.acceptCall();
			}
		});
	}

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

		MethodChannel methodChannel = new MethodChannel(messenger, ChannelNames.getChannelName(ChannelNames.REJECT_CALL));

		methodChannel.setMethodCallHandler((call, result) -> {
			System.out.println("call.method: " + call.method);
			if (call.method.equals(ChannelNames.REJECT_CALL)) {
				System.out.println("拒接电话");
				result.success(null);
				CallManager.rejectCall();
			}
		});
	}

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

		MethodChannel methodChannel = new MethodChannel(messenger, ChannelNames.getChannelName(ChannelNames.END_CALL));

		methodChannel.setMethodCallHandler((call, result) -> {
			System.out.println("call.method: " + call.method);
			if (call.method.equals(ChannelNames.END_CALL)) {
				System.out.println("结束通话");
				result.success(null);
				CallManager.endCall();
			}
		});
	}
}
