import 'package:feet/api/api.dart';
import 'package:feet/pages/image_view.dart';
import 'package:feet/util.dart';
import 'package:feet/widgets/homepage_post.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:share_plus/share_plus.dart';

class ArticlePage extends StatefulWidget {
  final Item article;
  const ArticlePage({super.key, required this.article});

  @override
  State<ArticlePage> createState() => _ArticlePageState();
}

class _ArticlePageState extends State<ArticlePage> {
  void showInfoDialog(bool debugInfo) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(widget.article.title),
        content: Text(
          // The dart formatter insists of writing this like this
          'By ${widget.article.author.isEmpty ? '(Unknown author)' : widget.article.author}\nPublished on ${formatDate(widget.article.createdOnTime)}${debugInfo ? '\nID: ${widget.article.id}\n'
              'Is read: ${widget.article.isRead ? 'Yes' : 'No'}\n'
              'Is saved: ${widget.article.isSaved ? 'Yes' : 'No'}' : ''}',
        ),
        actions: debugInfo
            ? []
            : [
                TextButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                      showInfoDialog(true);
                    },
                    child: const Text('Debug info')),
              ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: InkWell(
          enableFeedback: false,
          onTap: () => showInfoDialog(false),
          child: Text(widget.article.title),
        ),
        actions: [
          IconButton(
            onPressed: () {
              Share.share(widget.article.url, subject: widget.article.title);
            },
            icon: const Icon(Icons.share),
          ),
          PopupMenuButton(
            itemBuilder: (context) => [
              PopupMenuItem(
                onTap: () async {
                  await openUrl(context, widget.article.url);
                },
                value: 'open_in_browser',
                child: const Text('Open in browser'),
              ),
            ],
          ),
        ],
      ),
      body: ListView(
        children: [
          Container(
            margin: const EdgeInsets.all(12.0),
            child: SelectionArea(
              child: Html(
                data: widget.article.html,
                onImageTap: (url, ctx, attributes, element) =>
                    Navigator.of(context).push(
                  PageRouteBuilder(
                    opaque: false,
                    pageBuilder: (context, _, __) => ImageView(url: url!),
                    transitionsBuilder: (context, animation, _, child) {
                      return FadeTransition(
                        opacity: animation,
                        child: child,
                      );
                    },
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
