//
//  MapProtocol.swift
//  Runner
//
//  Created by Groot on 2025/5/8.
//

import Foundation
import Flutter
import MapKit

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

    var methodChannel: FlutterMethodChannel? { get set }
    
    var currentRegion: MKCoordinateRegion? { 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)
    
    // 处理地图移动
    func handleMapMoveCamera(args: [String: Any]?, result: @escaping FlutterResult)
    
    // 处理地图清除Marker
    func handleMapClearMarkers(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)
}

extension MapCapability {
    func handleMethodCall(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        guard let callMethod = MapViewChannelMethod(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)
        default:
            result(FlutterMethodNotImplemented)
        }
    }
}
