import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../features/call/call_page.dart';
import '../features/call/incoming_call_page.dart'; // Import the new page

class CallService {
  static const MethodChannel _channel = MethodChannel('call_service');
  static String? currentPhoneNumber;
  static bool _isCallPageVisible = false;

  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  CallService() {
    _channel.setMethodCallHandler((call) async {
      final context = navigatorKey.currentContext;
      print('CallService: Received method ${call.method} with args ${call.arguments}');
      if (context == null) {
        print('CallService: Navigator context is null, cannot navigate');
        return;
      }

      switch (call.method) {
        case "callAdded":
          final phoneNumber = call.arguments["callId"] as String;
          final state = call.arguments["state"] as String;
          currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
          print('CallService: Call added, number: $currentPhoneNumber, state: $state');
          if (state == "ringing") {
            _navigateToIncomingCallPage(context);
          } else {
            _navigateToCallPage(context);
          }
          break;
        case "callStateChanged":
          final state = call.arguments["state"] as String;
          print('CallService: State changed to $state');
          if (state == "disconnected" || state == "disconnecting") {
            _closeCallPage(context);
          } else if (state == "active" || state == "dialing") {
            _navigateToCallPage(context);
          } else if (state == "ringing") {
            _navigateToIncomingCallPage(context);
          }
          break;
        case "callEnded":
        case "callRemoved":
          print('CallService: Call ended/removed');
          _closeCallPage(context);
          currentPhoneNumber = null;
          break;
      }
    });
  }

  void _navigateToCallPage(BuildContext context) {
    if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/call') {
      print('CallService: CallPage already visible, skipping navigation');
      return;
    }
    print('CallService: Navigating to CallPage');
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        settings: const RouteSettings(name: '/call'),
        builder: (context) => CallPage(
          displayName: currentPhoneNumber!,
          phoneNumber: currentPhoneNumber!,
          thumbnail: null,
        ),
      ),
    ).then((_) {
      _isCallPageVisible = false;
    });
    _isCallPageVisible = true;
  }

  void _navigateToIncomingCallPage(BuildContext context) {
    if (_isCallPageVisible && ModalRoute.of(context)?.settings.name == '/incoming_call') {
      print('CallService: IncomingCallPage already visible, skipping navigation');
      return;
    }
    print('CallService: Navigating to IncomingCallPage');
    Navigator.push(
      context,
      MaterialPageRoute(
        settings: const RouteSettings(name: '/incoming_call'),
        builder: (context) => IncomingCallPage(
          displayName: currentPhoneNumber!,
          phoneNumber: currentPhoneNumber!,
          thumbnail: null,
        ),
      ),
    ).then((_) {
      _isCallPageVisible = false;
    });
    _isCallPageVisible = true;
  }

  void _closeCallPage(BuildContext context) {
    if (!_isCallPageVisible) {
      print('CallService: CallPage not visible, skipping pop');
      return;
    }
    if (Navigator.canPop(context)) {
      print('CallService: Popping CallPage');
      Navigator.pop(context);
      _isCallPageVisible = false;
    }
  }

  Future<void> makeGsmCall(
    BuildContext context, {
    required String phoneNumber,
    String? displayName,
    Uint8List? thumbnail,
  }) async {
    try {
      currentPhoneNumber = phoneNumber;
      print('CallService: Making GSM call to $phoneNumber');
      final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
      print('CallService: makeGsmCall result: $result');
      if (result["status"] != "calling") {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text("Failed to initiate call")),
        );
      }
    } catch (e) {
      print("CallService: Error making call: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("Error making call: $e")),
      );
      rethrow;
    }
  }

  Future<void> hangUpCall(BuildContext context) async {
    try {
      print('CallService: Hanging up call');
      final result = await _channel.invokeMethod('hangUpCall');
      print('CallService: hangUpCall result: $result');
      if (result["status"] != "ended") {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text("Failed to end call")),
        );
      }
    } catch (e) {
      print("CallService: Error hanging up call: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("Error hanging up call: $e")),
      );
      rethrow;
    }
  }
}