// lib/services/obfuscate_service.dart
import 'package:dialer/presentation/common/widgets/color_darkener.dart';

import '../../core/config/app_config.dart';
import 'dart:ui';
import 'dart:typed_data';
import 'package:flutter/material.dart';

class ObfuscateService {
  // Private constructor
  ObfuscateService._privateConstructor();

  // Singleton instance
  static final ObfuscateService _instance = ObfuscateService._privateConstructor();

  // Factory constructor to return the same instance
  factory ObfuscateService() {
    return _instance;
  }

  // Public method to obfuscate data
  String obfuscateData(String data) {
    if (AppConfig.isStealthMode) {
      return _obfuscateData(data);
    } else {
      return data;
    }
  }

  // Private helper method for obfuscation logic
  String _obfuscateData(String data) {
    if (data.isNotEmpty) {
      // Ensure the string has at least two characters to obfuscate
      if (data.length == 1) {
        return '${data[0]}';
      } else {
        return '${data[0]}...${data[data.length - 1]}';
      }
    }
    return '';
  }
}


class ObfuscatedAvatar extends StatelessWidget {
  final Uint8List? imageBytes;
  final double radius;
  final Color backgroundColor;
  final String? fallbackInitial;

  const ObfuscatedAvatar({
    Key? key,
    required this.imageBytes,
    this.radius = 25,
    this.backgroundColor = Colors.grey,
    this.fallbackInitial,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (imageBytes != null && imageBytes!.isNotEmpty) {
      return ClipOval(
        child: ImageFiltered(
          imageFilter: AppConfig.isStealthMode
              ? ImageFilter.blur(sigmaX: 10, sigmaY: 10)
              : ImageFilter.blur(sigmaX: 0, sigmaY: 0),
          child: Image.memory(
            imageBytes!,
            fit: BoxFit.cover,
            width: radius * 2,
            height: radius * 2,
          ),
        ),
      );
    } else {
      return CircleAvatar(
        radius: radius,
        backgroundColor: backgroundColor,
        child: Text(
          fallbackInitial != null && fallbackInitial!.isNotEmpty
              ? fallbackInitial![0].toUpperCase()
              : '?',
          style: TextStyle(
            color: darken(backgroundColor),
            fontSize: radius,
          ),
        ),
      );
    }
  }
}
