import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/user_search/user_list_row.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

typedef CallbackUserSelect(User suer);

Future<User> searchUser(BuildContext context,
        {CallbackUserSelect callbackUserSelect}) async =>
    await showSearch<User>(
      context: context,
      delegate: UserSearchDelegate(callbackUserSelect: callbackUserSelect),
    );

class UserSearchDelegate extends SearchDelegate<User> {
  final CallbackUserSelect callbackUserSelect;

  UserSearchDelegate({this.callbackUserSelect});

  @override
  String get searchFieldLabel => 'Search by FCS ID or Name';

  @override
  ThemeData appBarTheme(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return theme.copyWith(
      inputDecorationTheme: InputDecorationTheme(
          hintStyle: TextStyle(
              color: theme.primaryTextTheme.caption.color, fontSize: 14)),
      textTheme: theme.textTheme.copyWith(
          title: theme.textTheme.title.copyWith(
              color: theme.primaryTextTheme.title.color, fontSize: 16)),
      primaryColor: primaryColor,
    );
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () => query = '',
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () => close(context, null),
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    final packageModel = Provider.of<PackageModel>(context);
    return FutureBuilder(
        future: packageModel.searchUser(query),
        builder: (context, AsyncSnapshot<List<User>> snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data.length == 0) {
              return Container(
                child: Center(
                  child: Text(
                    "No result found",
                    textAlign: TextAlign.center,
                  ),
                ),
              );
            }
            return Container(
              padding: EdgeInsets.only(top: 15),
              child: ListView(
                children: snapshot.data
                    .map((u) => UserListRow(
                          user: u,
                          callbackUserSelect: callbackUserSelect,
                        ))
                    .toList(),
              ),
            );
          } else if (snapshot.hasError) {
            return Container(
              child: Center(
                child: Text(
                  '${snapshot.error}',
                  textAlign: TextAlign.center,
                ),
              ),
            );
          } else {
            return Container(
              child: Center(
                child: CircularProgressIndicator(
                    valueColor:
                        new AlwaysStoppedAnimation<Color>(primaryColor)),
              ),
            );
          }
        });
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container(
      child: Center(
        child: Opacity(
            opacity: 0.2,
            child: Icon(Icons.perm_identity, size: 200, color: primaryColor)),
      ),
    );
  }
}
