import 'package:flutter/material.dart';

import '/models/friends.dart';
import '/views/main/conversation/create_add_users_list.dart';

class ConversationAddFriendsList extends StatefulWidget {
  final List<Friend> friends;
  final Function(List<Friend> friendsSelected) saveCallback;
  const ConversationAddFriendsList({
    Key? key,
    required this.friends,
    required this.saveCallback,
  }) : super(key: key);

  @override
  State<ConversationAddFriendsList> createState() => _ConversationAddFriendsListState();
}

class _ConversationAddFriendsListState extends State<ConversationAddFriendsList> {
  List<Friend> friends = [];
  List<Friend> friendsSelected = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        automaticallyImplyLeading: false,
        flexibleSpace: SafeArea(
          child: Container(
            padding: const EdgeInsets.only(right: 16),
            child: Row(
              children: <Widget>[
                IconButton(
                  onPressed: (){
                    Navigator.pop(context);
                  },
                  icon: const Icon(Icons.arrow_back),
                ),
                const SizedBox(width: 2,),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        friendsSelected.isEmpty ?
                        'Select Friends' :
                        '${friendsSelected.length} Friends Selected',
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 20,left: 16,right: 16),
            child: TextField(
              decoration: const InputDecoration(
                hintText: "Search...",
                prefixIcon: Icon(
                  Icons.search,
                  size: 20
                ),
              ),
              onChanged: (value) => filterSearchResults(value.toLowerCase())
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 0,left: 16,right: 16),
            child: list(),
          ),
        ],
      ),
      floatingActionButton: Padding(
        padding: const EdgeInsets.only(right: 10, bottom: 10),
        child: FloatingActionButton(
          onPressed: () {
            widget.saveCallback(friendsSelected);

            setState(() {
              friendsSelected = [];
            });
          },
          backgroundColor: Theme.of(context).colorScheme.primary,
          child: friendsSelected.isEmpty ?
          const Text('Skip') :
          const Icon(Icons.add, size: 30),
        ),
      ),
    );
  }

  void filterSearchResults(String query) {
    List<Friend> dummySearchList = [];
    dummySearchList.addAll(widget.friends);

    if(query.isNotEmpty) {
      List<Friend> dummyListData = [];
      for (Friend friend in dummySearchList) {
      if (friend.username.toLowerCase().contains(query)) {
        dummyListData.add(friend);
      }
    }
      setState(() {
        friends.clear();
        friends.addAll(dummyListData);
      });
      return;
    }

    setState(() {
      friends.clear();
      friends.addAll(widget.friends);
    });
  }

  @override
  void initState() {
    super.initState();
    friends.addAll(widget.friends);
    setState(() {});
  }

  Widget list() {
    if (friends.isEmpty) {
      return const Center(
        child: Text('No Friends'),
      );
    }

    return ListView.builder(
      itemCount: friends.length,
      shrinkWrap: true,
      padding: const EdgeInsets.only(top: 16),
      physics: const BouncingScrollPhysics(),
      itemBuilder: (context, i) {
        return ConversationAddFriendItem(
          friend: friends[i],
          isSelected: (bool value) {
            setState(() {
              widget.friends[i].selected = value;
              if (value) {
                friendsSelected.add(friends[i]);
                return;
              }
              friendsSelected.remove(friends[i]);
            });
          }
        );
      },
    );
  }
}
