import 'dart:io';

import 'package:dartboard_resume/dartboard_parser.dart';
import 'package:dartboard_resume/dartboard_widgets.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:toml/toml.dart';

int? lastDartboardHash;

Future<void> renderPdf(String tomlFilePath, {bool force = false}) async {
  try {
    final start = DateTime.now().microsecondsSinceEpoch;
    final dartboardData = DartboardData.fromToml(tomlFilePath);
    if (lastDartboardHash == dartboardData.hashCode && !force) {
      return;
    }
    lastDartboardHash = dartboardData.hashCode;
    stdout.writeln("Detected change:\nRendering with new dartboard data: $lastDartboardHash");
    final pdfFuture = Document()..addPage(_generatePdfPage(dartboardData: dartboardData, renderNs: 0));
    await pdfFuture.save();
    final renderNs = DateTime.now().microsecondsSinceEpoch - start;
    final pdf = Document();
    pdf.addPage(_generatePdfPage(dartboardData: dartboardData, renderNs: renderNs));

    final file = File("example.pdf");
    final bytes = await pdf.save();

    stdout.writeln('New pdf file saved.');
    file.writeAsBytesSync(bytes);
    stdout.writeln('Reloading llpp...');
    Process.runSync('pkill', ['-HUP', 'llpp']);
  } catch (e) {
    stderr.writeln('Encountered error: $e');
    try {
      stderr.writeln('Current toml map:\n${TomlDocument.loadSync(tomlFilePath).toMap()}');
    } catch (_) {
      stderr.writeln('Cannot display current toml map');
    }
  }
}

Page _generatePdfPage({required DartboardData dartboardData, required int renderNs}) {
  final List<Widget> groupedExperienceList = dartboardData.groupedExperiences.entries.map<Widget>(
    (entry) {
      final String subsection = entry.key;
      final List<DartboardExperience> experiences = entry.value;
      return Column(
        children: [
          Row(
            children: [
              Text(
                subsection,
                style: dartboardData.subheaderTextStyle
                    .merge(const TextStyle(fontSize: 18))
                    .apply(color: const PdfColorGrey(0.2)),
              ),
            ],
          ),
          Container(height: 2, width: 200, color: const PdfColorGrey(0.7)),
          ...experiences.map(
            (DartboardExperience exp) => DartboardExperienceEntry(dartboardData: dartboardData, exp: exp),
          ),
          DartboardFooter(dartboardData: dartboardData, renderNs: renderNs),
        ],
      );
    },
  ).toList();
  return Page(
    pageTheme: const PageTheme(pageFormat: PdfPageFormat.standard),
    build: (Context context) {
      return
          // FullPage(
          // ignoreMargins: true,
          // child:
          Column(
        children: [
          SizedBox(
            height: 120,
            width: double.infinity,
            child: Stack(
              children: [
                Positioned(
                  left: 0,
                  child: Container(
                    height: 100,
                    width: 100,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      image: DecorationImage(
                        fit: BoxFit.contain,
                        image: MemoryImage(
                          File(dartboardData.imagePath).readAsBytesSync(),
                        ),
                      ),
                    ),
                  ),
                ),
                Center(
                  child: Column(
                    children: [
                      Text(dartboardData.fullName, style: dartboardData.headerTextStyle),
                      Text(dartboardData.phoneNumber, style: dartboardData.headerTextStyle),
                      Text(dartboardData.email, style: dartboardData.headerTextStyle),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Container(height: 20),
          ...groupedExperienceList,
        ],
        // ),
      );
    },
  );
}