import 'package:comunic/models/comment.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/like_widget.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart';
import 'package:comunic/ui/widgets/text_widget.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';

/// Single comment tile
///
/// @author Pierre HUBERT

enum _CommentAction { DELETE, UPDATE }

class CommentTile extends StatelessWidget {
  final Comment comment;
  final User user;
  final void Function(Comment) onUpdateComment;
  final void Function(Comment) onDeleteComment;

  const CommentTile({
    Key key,
    @required this.comment,
    @required this.user,
    @required this.onUpdateComment,
    @required this.onDeleteComment,
  })  : assert(comment != null),
        assert(user != null),
        assert(onUpdateComment != null),
        assert(onDeleteComment != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: _buildAccountImageWidget(),
      title: Text(
        user.displayName,
      ),
      subtitle: _buildCommentContent(),
      trailing: Text(
        diffTimeFromNowToStr(comment.timeSent),
        style: TextStyle(fontSize: 10.0),
      ),
    );
  }

  Widget _buildAccountImageWidget() {
    return PopupMenuButton<_CommentAction>(
      child: AccountImageWidget(
        user: user,
      ),
      onSelected: _selectedMenuOption,
      itemBuilder: (c) => [
        // Update comment content
        PopupMenuItem(
          enabled: comment.isOwner,
          child: Text(tr("Update")),
          value: _CommentAction.UPDATE,
        ),

        // Delete comment
        PopupMenuItem(
          enabled: comment.isOwner,
          child: Text(tr("Delete")),
          value: _CommentAction.DELETE,
        ),
      ],
    );
  }

  Widget _buildCommentContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        // Comment image
        Container(
          child: comment.hasImage
              ? NetworkImageWidget(
                  url: comment.imageURL,
                  allowFullScreen: true,
                  height: 100.0,
                  width: null,
                )
              : null,
        ),

        // Comment text
        Container(
          child: comment.hasContent
              ? TextWidget(
                  content: comment.content,
                  style: TextStyle(
                      color: darkTheme() ? darkAccentColor : Colors.black),
                )
              : null,
        ),

        // Comment likes
        LikeWidget(likeElement: comment),
      ],
    );
  }

  /// A menu option has been selected
  void _selectedMenuOption(_CommentAction value) {
    switch (value) {
      // Update comment content
      case _CommentAction.UPDATE:
        onUpdateComment(comment);
        break;

      // Delete comment
      case _CommentAction.DELETE:
        onDeleteComment(comment);
        break;
    }
  }
}