import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:guru_popup/guru_popup.dart';
import 'package:guru_utils/image/image_utils.dart';
import 'package:guru_utils/router/router.dart';
import 'package:guru_widgets/dialog/guru_dialog.dart';
import 'dart:ui';
import 'dart:ui' as ui;
import 'package:package_info_plus/package_info_plus.dart';

class AppOwnerInfo {
  final String logo;
  final String webUrl;
  final String gpUrl;
  final String appName;
  final String md5;
  final String lanuchYear;

  const AppOwnerInfo(this.logo, {
    this.webUrl = '',
    this.gpUrl = '',
    this.appName = '',
    this.md5 = '',
    this.lanuchYear = '2022'
  });
}

class AppOwnershipUtils {
  static Future<Uint8List> generateAppBitmap(AppOwnerInfo info, {bool waterMark = true}) async {
    const canvasRect = Rect.fromLTWH(0.0, 0.0, 1056.0, 640.0);
    Paint logoPaint = Paint()..filterQuality = FilterQuality.high;
    Paint bgPaint = Paint()..isAntiAlias = true;

    final PictureRecorder recorder = PictureRecorder();
    Canvas canvas = Canvas(recorder, canvasRect);

    bgPaint.color = const Color(0xFF5F3E30);
    canvas.drawRRect(
        RRect.fromRectAndRadius(canvasRect, const Radius.circular(10.0)),
        bgPaint);

    if (waterMark) {
      canvas.rotate(-3.14 / 4);
      for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 6; j++) {
          ParagraphBuilder guruParagraphBuilder =
              ParagraphBuilder(ParagraphStyle(
            fontStyle: FontStyle.normal,
            fontSize: 48,
            fontFamily: 'EncodeSansExpanded',
            strutStyle: ui.StrutStyle(height: 1.8),
          ));
          guruParagraphBuilder.pushStyle(
              ui.TextStyle(color: const Color.fromRGBO(173, 133, 81, 0.6)));
          guruParagraphBuilder.addText('Guru Game');
          const content = ParagraphConstraints(width: 400);
          canvas.drawParagraph(guruParagraphBuilder.build()..layout(content),
              Offset(i * 400 - 400, j * 240));
        }
      }
      canvas.restore();
      canvas.rotate(3.14 / 4);
    }

    final logoImage = await ImageUtils.loadImageFromAsset(info.logo);
    final logoSrcRect = Rect.fromLTWH(
        0.0, 0.0, logoImage.width.toDouble(), logoImage.height.toDouble());
    const logoDstRect = Rect.fromLTWH(40.0, 180.0, 240.0, 240.0);

    canvas.drawImageRect(logoImage, logoSrcRect, logoDstRect, logoPaint);

    ParagraphBuilder logoParagraphBuilder = ParagraphBuilder(ParagraphStyle(
      textAlign: TextAlign.center,
      fontStyle: FontStyle.normal,
      fontWeight: FontWeight.bold,
      fontSize: 40,
      fontFamily: 'EncodeSansExpanded',
      strutStyle: ui.StrutStyle(height: 1.8),
    ));
    logoParagraphBuilder
        .pushStyle(ui.TextStyle(color: const Color(0xFFF8C077)));
    logoParagraphBuilder.addText(info.appName);
    const title = ParagraphConstraints(width: 220);
    canvas.drawParagraph(
        logoParagraphBuilder.build()..layout(title), const Offset(40, 430));

    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    final Map<String, String> textMap = {
      'Official Website': info.webUrl,
      'Google Play': info.gpUrl,
      'Package Name': packageInfo.packageName,
      'MD5 certificate fingerprint': info.md5
    };

    final textKeys = textMap.keys.toList();
    for (var i = 0; i < textKeys.length; i++) {
      final String key = textKeys[i];
      final String value = textMap[key]!;

      ParagraphBuilder titleParagraphBuilder = ParagraphBuilder(ParagraphStyle(
        fontStyle: FontStyle.normal,
        fontSize: 46,
        fontFamily: 'EncodeSansExpanded',
        strutStyle: ui.StrutStyle(height: 1.8),
      ));
      titleParagraphBuilder
          .pushStyle(ui.TextStyle(color: const Color(0xFFF8C077)));
      titleParagraphBuilder.addText(key);
      const title = ParagraphConstraints(width: 660);
      canvas.drawParagraph(titleParagraphBuilder.build()..layout(title),
          Offset(320, i * 136 + 40));

      ParagraphBuilder valueParagraphBuilder = ParagraphBuilder(ParagraphStyle(
        fontStyle: FontStyle.normal,
        fontSize: value.length > 50 ? 28 : 40,
        fontFamily: 'EncodeSansExpanded',
        strutStyle: ui.StrutStyle(height: 1.8),
      ));
      valueParagraphBuilder.pushStyle(ui.TextStyle(color: Colors.white));
      valueParagraphBuilder.addText(value);
      const content = ParagraphConstraints(width: 660);
      canvas.drawParagraph(valueParagraphBuilder.build()..layout(content),
          Offset(320, i * 136 + 90));
    }

    final image = await recorder
        .endRecording()
        .toImage(canvasRect.width.toInt(), canvasRect.height.toInt());
    ByteData? imgBytes = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = imgBytes!.buffer.asUint8List();
    return pngBytes;
  }

  static Future<Widget> waterMarkWidget(
      {String title = 'Guru Game',
      String subTitle = '',
      int lineCount = 6}) async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        children: [
          for (var i = 0; i < lineCount; i++)
            Expanded(
                child: Center(
              child: Transform.rotate(
                  angle: -0.34,
                  child: Column(children: [
                    Text(
                      title,
                      style: const TextStyle(color: Colors.black, fontSize: 20),
                    ),
                    Text(
                      packageInfo.packageName,
                      style: const TextStyle(color: Colors.black, fontSize: 16),
                    ),
                    Text(
                      subTitle,
                      style: const TextStyle(color: Colors.black, fontSize: 16),
                    ),
                  ])),
            )),
        ],
      ),
    );
  }
}
