import 'package:flutter/services.dart';

class PenguinMethodChannel {
  static const MethodChannel _channel = MethodChannel('launch_penguin_ui');

  Future<Uint8List> loadGif() async {
    return await rootBundle.load("assets/images/progress-loading-red-crop-1.gif").then((data) => data.buffer.asUint8List());
  }

  Future<void> launch(String storyboardName, String languageCode, String username, {NavigationClinicDetails? details}) async {
    Uint8List image = await loadGif();
    try {
      await _channel.invokeMethod('launchPenguin', {
        "storyboardName": storyboardName,
        "baseURL": "https://prod.hmg.nav.penguinin.com",
        // "dataURL": "https://hmg.nav.penguinin.com",
        // "positionURL": "https://hmg.nav.penguinin.com",
        // "dataURL": "https://hmg-v33.local.penguinin.com",
        // "positionURL": "https://hmg-v33.local.penguinin.com",
        "dataURL": "https://prod.hmg.nav.penguinin.com",
        "positionURL": "https://prod.hmg.nav.penguinin.com",
        "dataServiceName": "api",
        "positionServiceName": "pe",
        "clientID": "HMG",
        // "username": "Haroon",
        "username": username,
        "isSimulationModeEnabled": false,
        "isShowUserName": false,
        "isUpdateUserLocationSmoothly": true,
        "isEnableReportIssue": true,
        "languageCode": languageCode,
        "clientKey": "UGVuZ3VpbklOX1Blbk5hdl9QSUY=",
        "mapBoxKey": "sk.eyJ1IjoicndhaWQiLCJhIjoiY2x6NWo0bTMzMWZodzJrcGZpemYzc3Z4dSJ9.uSSZuwNSGCcCdPAiORECmg",
        "clinicID": details?.clinicId ?? "",
        // "clinicID": "108", // 46 ,49, 133
        "patientID": details?.patientId ?? "",
        "projectID": details?.projectId ?? "",
        "loaderImage": image,
      });
    } on PlatformException catch (e) {
      print("Failed to launch PenguinIn: '${e.message}'.");
    }
  }

  void setMethodCallHandler(){
    _channel.setMethodCallHandler((MethodCall call) async {
      try {

        print(call.method);

        switch (call.method) {

          case PenguinMethodNames.onPenNavInitializationError:
            _handleInitializationError(call.arguments); // Handle onPenNavInitializationError errors.
            break;
          case PenguinMethodNames.onPenNavUIDismiss:
            //todo handle pen dismissable
            // _handlePenNavUIDismiss(); // Handle UI dismissal event.
            break;
          case PenguinMethodNames.onReportIssue:
             // Handle the report issue event.
            _handleInitializationError(call.arguments);
            break;
          default:
            _handleUnknownMethod(call.method); // Handle unknown method calls.
        }
      } catch (e) {
        print("Error handling method call '${call.method}': $e");
        // Optionally, log this error to an external service
      }
    });
  }
  static void _handleUnknownMethod(String method) {
    print("Unknown method: $method");
    // Optionally, handle this unknown method case, such as reporting or ignoring it
  }


  static void _handleInitializationError(Map<dynamic, dynamic> error) {
    final type = error['type'] as String?;
    final description = error['description'] as String?;
    print("Initialization Error: ${type ?? 'Unknown Type'}, ${description ?? 'No Description'}");

  }

}
// Define constants for method names
class PenguinMethodNames {
  static const String showPenguinUI = 'showPenguinUI';
  static const String openSharedLocation = 'openSharedLocation';

  // ---- Handler Method
  static const String onPenNavSuccess = 'onPenNavSuccess'; // Tested Android,iOS
  static const String onPenNavInitializationError = 'onPenNavInitializationError'; // Tested Android,iOS
  static const String onPenNavUIDismiss = 'onPenNavUIDismiss'; //Tested Android,iOS
  static const String onReportIssue = 'onReportIssue'; // Tested Android,iOS
  static const String onLocationOffCampus = 'onLocationOffCampus';  // Tested iOS,Android
  static const String navigateToPOI = 'navigateToPOI'; // Tested Android,iOS
}

class NavigationClinicDetails {
  String? clinicId;
  String? patientId;
  String? projectId;
}
