import 'package:flutter/material.dart';

import '/components/custom_circle_avatar.dart';
import '/models/conversation_users.dart';
import '/models/my_profile.dart';

class ConversationSettingsUserListItem extends StatefulWidget{
  final ConversationUser user;
  final bool isAdmin;
  final MyProfile profile;
  const ConversationSettingsUserListItem({
    Key? key,
    required this.user,
    required this.isAdmin,
    required this.profile,
  }) : super(key: key);

  @override
  _ConversationSettingsUserListItemState createState() => _ConversationSettingsUserListItemState();
}

class _ConversationSettingsUserListItemState extends State<ConversationSettingsUserListItem> {

  Widget admin() {
    if (!widget.user.admin) {
      return const SizedBox.shrink();
    }

    return Row(
      children: const <Widget> [
        SizedBox(width: 5),
        Icon(Icons.circle, size: 6),
        SizedBox(width: 5),
        Text(
          'Admin',
          style: TextStyle(
              fontSize: 14,
          ),
        ),
      ]
    );
  }

  Widget adminUserActions() {
    if (!widget.isAdmin || widget.user.admin || widget.user.username == widget.profile.username) {
      return const SizedBox(height: 50);
    }

    return PopupMenuButton<String>(
      itemBuilder: (context) => [
        PopupMenuItem(
                value: 'admin',
                // row with 2 children
                child: Row(
                  children: const [
                    Icon(Icons.admin_panel_settings),
                    SizedBox(
                      width: 10,
                    ),
                    Text('Promote to Admin')
                  ],
                ),
        ),
        PopupMenuItem(
                value: 'remove',
                // row with 2 children
                child: Row(
                  children: const [
                    Icon(Icons.cancel),
                    SizedBox(
                      width: 10,
                    ),
                    Text('Remove from chat')
                  ],
                ),
        ),
      ],
      offset: const Offset(0, 0),
      elevation: 2,
      // on selected we show the dialog box
      onSelected: (String value) {
        // if value 1 show dialog
        if (value == 'admin') {
          print('admin');
          return;
          // if value 2 show dialog
        }
        if (value == 'remove') {
          print('remove');
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
         padding: const EdgeInsets.only(left: 12,right: 5,top: 0,bottom: 5),
         child: Row(
             children: <Widget>[
               Expanded(
                   child: Row(
                       mainAxisAlignment: MainAxisAlignment.spaceBetween,
                       children: <Widget>[
                         CustomCircleAvatar(
                             initials: widget.user.username[0].toUpperCase(),
                             radius: 15,
                         ),
                         const SizedBox(width: 16),
                         Expanded(
                             child: Align(
                                 alignment: Alignment.centerLeft,
                                 child: Container(
                                     color: Colors.transparent,
                                     child: Row(
                                         children: <Widget>[
                                           Text(
                                               widget.user.username,
                                               style: const TextStyle(fontSize: 16)
                                            ),
                                           admin(),
                                         ],
                                     ),
                                 ),
                             ),
                         ),
                      ],
                    ),
                ),
                adminUserActions(),
            ],
        ),
    );
  }
}
