import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class AppIconGenerator {
  static Future<Uint8List> generateAppIcon() async {
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);
    const size = Size(1024, 1024);
    
    // Create a white background
    final paint = Paint()..color = Colors.white;
    canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
    
    // Create a blue circle background
    paint.color = const Color(0xFF2196F3); // Material Blue
    canvas.drawCircle(
      Offset(size.width / 2, size.height / 2),
      size.width * 0.4,
      paint,
    );
    
    // Draw the gift box icon
    _drawGiftBox(canvas, size);
    
    // Draw the text
    _drawText(canvas, size);
    
    final picture = recorder.endRecording();
    final image = await picture.toImage(1024, 1024);
    final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    return byteData!.buffer.asUint8List();
  }
  
  static void _drawGiftBox(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.stroke
      ..strokeWidth = size.width * 0.02;
    
    final centerX = size.width / 2;
    final centerY = size.height / 2 - size.height * 0.1;
    final boxSize = size.width * 0.15;
    
    // Draw gift box
    final boxRect = Rect.fromCenter(
      center: Offset(centerX, centerY),
      width: boxSize,
      height: boxSize,
    );
    canvas.drawRect(boxRect, paint);
    
    // Draw bow
    final bowWidth = boxSize * 0.8;
    final bowHeight = boxSize * 0.3;
    final bowRect = Rect.fromCenter(
      center: Offset(centerX, centerY - boxSize / 2),
      width: bowWidth,
      height: bowHeight,
    );
    canvas.drawRRect(
      RRect.fromRectAndRadius(bowRect, Radius.circular(bowHeight / 2)),
      paint,
    );
    
    // Draw 'e' inside the box
    final textPainter = TextPainter(
      text: TextSpan(
        text: 'e',
        style: TextStyle(
          color: Colors.white,
          fontSize: boxSize * 0.4,
          fontWeight: FontWeight.bold,
        ),
      ),
      textDirection: TextDirection.ltr,
    );
    textPainter.layout();
    textPainter.paint(
      canvas,
      Offset(
        centerX - textPainter.width / 2,
        centerY - textPainter.height / 2,
      ),
    );
  }
  
  static void _drawText(Canvas canvas, Size size) {
    // Draw "ENAKLO"
    final enakloPainter = TextPainter(
      text: TextSpan(
        text: 'ENAKLO',
        style: TextStyle(
          color: Colors.white,
          fontSize: size.width * 0.08,
          fontWeight: FontWeight.bold,
        ),
      ),
      textDirection: TextDirection.ltr,
    );
    enakloPainter.layout();
    enakloPainter.paint(
      canvas,
      Offset(
        size.width / 2 - enakloPainter.width / 2,
        size.height / 2 + size.height * 0.05,
      ),
    );
    
    // Draw "POS"
    final posPainter = TextPainter(
      text: TextSpan(
        text: 'POS',
        style: TextStyle(
          color: Colors.white,
          fontSize: size.width * 0.06,
          fontWeight: FontWeight.w500,
        ),
      ),
      textDirection: TextDirection.ltr,
    );
    posPainter.layout();
    posPainter.paint(
      canvas,
      Offset(
        size.width / 2 - posPainter.width / 2,
        size.height / 2 + size.height * 0.15,
      ),
    );
  }
} 