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

class DartboardFooter extends StatelessWidget {
  DartboardFooter({required this.dartboardData, required this.renderNs});
  final int renderNs;
  final DartboardData dartboardData;

  @override
  Widget build(Context context) {
    final double renderTimeMs = renderNs.toDouble() / 1000.0;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Text(
          'This resume was generated with DartBoard Resume',
          style: dartboardData.defaultTextStyle.copyWith(fontSize: 10),
        ),
        Text(
          'Rendered in ${renderTimeMs.toStringAsFixed(2)}ms',
          style: dartboardData.defaultTextStyle.copyWith(fontSize: 10),
        ),
      ],
    );
  }
}

class DartboardExperienceEntry extends StatelessWidget {
  DartboardExperienceEntry({required this.dartboardData, required this.exp});
  final DartboardData dartboardData;
  final DartboardExperience exp;

  @override
  Widget build(Context context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(exp.title, style: dartboardData.subheaderTextStyle.apply(color: const PdfColorGrey(0.3))),
                Container(height: 1, width: 60, color: const PdfColorGrey(0.75)),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  exp.range.toString(),
                  style: dartboardData.defaultTextStyle.apply(color: const PdfColorGrey(0.42)),
                ),
                if (exp.location != null)
                  Text(
                    exp.location!,
                    style: dartboardData.defaultTextStyle.apply(color: const PdfColorGrey(0.42)),
                  ),
              ],
            ),
          ],
        ),
        SizedBox(height: 6),
        ...exp.attributes.map(
          (a) => Padding(
            child: DartboardTextWithLink(
              stringSections: a.toTextLinkList(),
              bulletString: dartboardData.dartboardTheme.bulletPoint,
              style: dartboardData.defaultTextStyle.apply(
                color: const PdfColorGrey(0.55),
              ),
            ),
            padding: const EdgeInsets.only(bottom: 4.0),
          ),
        ),
        SizedBox(height: 20),
      ],
    );
  }
}

// TODO this lays out long text lines on a newline after a uilink rather than soft wrapping it.
class DartboardTextWithLink extends StatelessWidget {
  DartboardTextWithLink({this.bulletString, required this.stringSections, this.style});

  final String? bulletString;
  final List<DartboardTextLinkData> stringSections;
  final TextStyle? style;

  @override
  Widget build(Context context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(bulletString == null ? '' : '$bulletString ', style: style),
        SizedBox(
          width: DartboardTheme.width - DartboardTheme.margin * 2 - 20.0,
          child: Wrap(
            children: [
              ...stringSections.map((s) {
                switch (s.type) {
                  case DartboardTextType.normal:
                    return Text(s.text, style: style);
                  case DartboardTextType.linkText:
                    return UrlLink(
                      destination: s.url!,
                      child: Text(
                        s.text,
                        style: style?.copyWith(color: PdfColors.blue),
                      ),
                    );
                }
              }),
            ],
          ),
        ),
      ],
    );
  }
}