import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:prime_chat/screens/ProfileScreen.dart';
import 'package:prime_chat/screens/components/Avatar.dart';
import 'package:prime_chat/screens/components/ItemBuilder.dart';

import '../basic/Method.dart';
import 'components/ProgressDialog.dart';

class SearchScreen extends StatefulWidget {
  const SearchScreen({Key? key}) : super(key: key);

  @override
  State<SearchScreen> createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen> {
  bool loading = false;
  List<Widget> children = [];

  @override
  Widget build(BuildContext context) {
    return ProgressDialog(
        loading: loading,
        label: "搜索中...",
        child: Scaffold(
          body: Column(
            children: [
              SearchBar(
                onSubmitted: (value) => _search(value),
              ),
              ...children
            ],
          ),
        ));
  }

  _search(String name) async {
    setState(() {
      loading = true;
    });
    final profile = await method.getProfile(name: name);
    debugPrint("get profile = ${profile?.name}");
    setState(() {
      loading = false;
      if (profile != null) {
        children.add(ListTile(
          leading: Avatar(profile.avatar),
          title: Text(profile.name),
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) =>
                        ProfileScreen(profile: profile, isFriend: false)));
          },
        ));
      }
    });
  }
}

class SearchBar extends StatelessWidget {
  const SearchBar({Key? key, this.onSubmitted, this.onChanged})
      : super(key: key);

  final void Function(String)? onSubmitted;
  final void Function(String)? onChanged;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      height: 80,
      color: theme.appBarTheme.backgroundColor,
      child: Column(
        children: [
          const SizedBox(
            height: 30,
          ),
          SizedBox(
            height: 40,
            child: Row(
              children: [
                Expanded(
                    child: Container(
                  // color: Colors.red,
                  // width: MediaQuery.of(context).size.width - 50,
                  margin: const EdgeInsets.symmetric(horizontal: 10),
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(6.0),
                  ),
                  child: TextField(
                    cursorColor: Colors.green,
                    autofocus: true,
                    style: const TextStyle(
                      fontSize: 18.0,
                      color: Colors.black,
                      fontWeight: FontWeight.w300,
                    ),
                    decoration: const InputDecoration(
                      contentPadding: EdgeInsets.only(bottom: 10),
                      icon: Icon(
                        Icons.search,
                        size: 20.0,
                        color: Colors.grey,
                      ),
                      border: InputBorder.none,
                      hintText: '搜索',
                      hintStyle: TextStyle(color: Colors.grey),
                    ),
                    onSubmitted: onSubmitted,
                    onChanged: onChanged,
                  ),
                )),
                SizedBox(
                    width: 40,
                    // height: 34,
                    child: GestureDetector(
                      child: const Text("取消"),
                      onTap: () => Navigator.pop(context),
                    ))
              ],
            ),
          ),
        ],
      ),
    );
  }
}
