//
//  MapDataCommunicationProtocol.swift
//  map_amap_ios
//
//  Created by 诺诺诺的言 on 2025/7/22.
//

import Foundation
import MapKit
import Flutter
import MAMapKit

// 地图功能接口定义
protocol MapCapability {
    var paramsDecodeError: FlutterError? { get set }

    var methodChannel: FlutterMethodChannel? { get set }
    
    var currentRegion: MACoordinateRegion? { get set }

    var markers: [ATMapMarker] { get set }

    var polylines: [ATMapPolyline] { get set }

    // 处理地图添加Marker
    func handleMapAddMarker(args: [[String: Any]]?, result: @escaping FlutterResult)
    
    // 处理地图添加Polyline
    func handleMapAddPolyline(args: [String: Any]?, result: @escaping FlutterResult)
    
    ///移除地图里面指定的Polyline
    func handleMapRemovePolyline(args: [String: Any]?, result: @escaping FlutterResult)
    
    // 处理地图移动
    func handleMapMoveCamera(args: [String: Any]?, result: @escaping FlutterResult)
    
    // 处理地图清除Marker
    func handleMapClearMarkers(result: @escaping FlutterResult)
    
    // 处理地图清除所有的Polyline
    func handleMapClearAllPolyline(result: @escaping FlutterResult)
    
    // 处理地图替换所有Marker
    func handleMapReplaceAllMarkers(args: [[String: Any]]?, result: @escaping FlutterResult)
    
    // 处理Marker点击事件
    func handleMarkerTap(marker: inout ATMapMarker)

    func handleMethodCall(_ call: FlutterMethodCall, result: @escaping FlutterResult)
    
    ///处理移动至多个点的位置，并提供设置padding距离
    func handleMapMoveToSuitableLocation(args: [String: Any]?, result: @escaping FlutterResult)
    
    ///清除指定标记物
    func handleMapRemovemarker(args: [String: Any]?, result: @escaping FlutterResult)
    
    ///禁用或开启地图所有交互
    func handleMapInteractionEnabled(args: [String: Any]?, result: @escaping FlutterResult)
    
    ///显示或隐藏地图logo
    func handleMapLogoVisible(args: [String: Any]?, result: @escaping FlutterResult)
}

extension MapCapability {
    func handleMethodCall(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        guard let callMethod = MapAmapDataChannelKeyword(rawValue: call.method) else {
            result(FlutterMethodNotImplemented)
            return
        }
        switch callMethod {
        case .methodMapMoveCamera, .methodMapAnimateCamera:
            handleMapMoveCamera(args: call.arguments as? [String: Any], result: result)
        case .methodUpdateOrAddMarkers:
            handleMapAddMarker(args: call.arguments as? [[String: Any]], result: result)
        case .methodMapClear:
            handleMapClearMarkers(result: result)
        case .methodReplaceAllMarkers:
            handleMapReplaceAllMarkers(args: call.arguments as? [[String: Any]], result: result)
        case .methodAddPolyline:
            handleMapAddPolyline(args: call.arguments as? [String: Any], result: result)
        case .methodRemovePolyline:
            handleMapRemovePolyline(args: call.arguments as? [String: Any], result: result)
        case .methodMapMoveToSuitableLocation:
            handleMapMoveToSuitableLocation(args: call.arguments as? [String: Any], result: result)
        case .methodMarkerRemoveMarker:
            handleMapRemovemarker(args: call.arguments as? [String: Any], result: result)
        case .methodMapInteractionEnabled:
            handleMapInteractionEnabled(args: call.arguments as? [String: Any], result: result)
        case .methodMapLogoVisible:
            handleMapLogoVisible(args: call.arguments as? [String: Any], result: result)
        case .methodMapClearAllPolylines:
            handleMapClearAllPolyline(result: result)
        default:
            result(FlutterMethodNotImplemented)
        }
    }
}
