import 'dart:async';
import 'dart:ui';

import 'package:flutter/services.dart';

class QrScanner {
  static const MethodChannel _channel = const MethodChannel('scanner');

  static Future<String> scan({
    String title = "",
    Color laserColor,
    bool playBeep = false, //android support
    String promptMessage,
    String errorMsg, //android support
    String permissionDeniedText,
    String messageConfirmText,
    String messageCancelText,
  }) async {
    Map<String, dynamic> arguments = {
      Intents.TITLE: title,
      Intents.KEY_PLAY_BEEP: playBeep,
    };
    if (laserColor != null)
      arguments[Intents.LASER_COLOR] = '#${laserColor.value.toRadixString(16)}';
    if (promptMessage != null)
      arguments[Intents.PROMPT_MESSAGE] = promptMessage;
    if (errorMsg != null)
      arguments[Intents.ERROR_MESSAGE] = errorMsg;
    if (permissionDeniedText != null)
      arguments[Intents.PERMISSION_DENIED_MESSAGE] = permissionDeniedText;
    if (messageConfirmText != null)
      arguments[Intents.MESSAGE_CONFIRM_TEXT] = messageConfirmText;
    if (messageCancelText != null)
      arguments[Intents.MESSAGE_CANCEL_TEXT] = messageCancelText;
    final String code = await _channel.invokeMethod('scan', arguments);
    return code;
  }
}

class Intents {
  static const TITLE = "SCAN_TITLE";
  static const LASER_COLOR = "LASER_COLOR";
  static const KEY_PLAY_BEEP = "KEY_PLAY_BEEP";
  static const PROMPT_MESSAGE = "PROMPT_MESSAGE";
  static const ERROR_MESSAGE = "ERROR_MESSAGE";
  static const PERMISSION_DENIED_MESSAGE = "PERMISSION_DENIED_MESSAGE";
  static const MESSAGE_CONFIRM_TEXT = "MESSAGE_CONFIRM_TEXT";
  static const MESSAGE_CANCEL_TEXT = "MESSAGE_CANCEL_TEXT";
}
