import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/forum.dart';
import 'package:sport/bean/post.dart';
import 'package:sport/bean/user_friend.dart';
import 'package:sport/pages/social/chat_page.dart';
import 'package:sport/pages/social/post_page.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/dialog/alert_dialog.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/loading.dart';
import 'package:sport/widgets/space.dart';

class PostSharePage extends StatefulWidget {
  final Post post;

  const PostSharePage(this.post);

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<PostSharePage> with InjectApi {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: buildBackButton(context),
        title: Text(
          "选择分享到的吧",
          style: titleStyle,
        ),
        centerTitle: false,
        titleSpacing: 0,
      ),
      body: FutureBuilder<RespList<Forum>>(
        future: api.getForumIndex(),
        builder: (_, snapshot) {
          if (snapshot.connectionState != ConnectionState.done)
            return RequestLoadingWidget();
          var list = snapshot.data?.results ?? [];
          return ListView.separated(
            itemBuilder: (BuildContext context, int index) => ListTile(
              leading: CircleAvatar(
                  backgroundImage:
                      CachedNetworkImageProvider(list[index].cover),
                  radius: 22.0),
              title: Text(
                "${list[index].name}",
                style: Theme.of(context)
                    .textTheme
                    .subtitle1
                    .copyWith(fontSize: 16),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              onTap: () async {
                bool result = await NavigatorUtil.goPage(
                    context,
                    (context) => PostPage(
                          list[index].forumId,
                          post: widget.post,
                          forum: list[index],
                        ));
                if (result == true) {
                  Navigator.of(context).pop(true);
                }
              },
              trailing: arrowRight(),
            ),
            itemCount: list.length,
            separatorBuilder: (BuildContext context, int index) => Divider(),
          );
        },
      ),
    );
  }
}

class PostShareFriendsPage extends StatefulWidget {
  final Post post;

  const PostShareFriendsPage(this.post);

  @override
  State<StatefulWidget> createState() => _PostShareFriendsPageState();
}

class _PostShareFriendsPageState extends State<PostShareFriendsPage>
    with InjectApi {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: buildBackButton(context),
        title: Text(
          "选择分享到的朋友",
          style: titleStyle,
        ),
        centerTitle: false,
        titleSpacing: 0,
      ),
      body: FutureBuilder<RespList<UserFriend>>(
        future: api.userFriends(),
        builder: (_, snapshot) {
          if (snapshot.connectionState != ConnectionState.done)
            return RequestLoadingWidget();
          var list = snapshot.data?.results ?? [];
          return ListView.separated(
            itemBuilder: (BuildContext context, int index) => ListTile(
              leading: CircleAvatar(
                  backgroundImage:
                      CachedNetworkImageProvider(list[index].socialInfo.avatar),
                  radius: 22.0),
              title: Text(
                "${list[index].socialInfo.name}",
                style: Theme.of(context)
                    .textTheme
                    .subtitle1
                    .copyWith(fontSize: 16),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              onTap: () async {
                await showDialog(
                    context: context,
                    builder: (context) => CustomAlertDialog(
                        child: Column(
                          children: <Widget>[
                            Space(
                              height: 37.0,
                            ),
                            CircleAvatar(
                                backgroundImage: CachedNetworkImageProvider(
                                  list[index].socialInfo?.avatar,
                                ),
                                radius: 35),
                            Space(
                              height: 8.0,
                            ),
                            Center(
                              child: Text(
                                "${list[index].socialInfo?.name}",
                                style: Theme.of(context).textTheme.headline3,
                              ),
                            ),
                            Space(
                              height: 8.0,
                            ),
                            _buildPostWidget(context, widget.post)
                          ],
                        ),
                        textOk: "分享",
                        ok: () async {
                          bool result = await NavigatorUtil.goPage(
                              context,
                              (context) => ChatPage(
                                    list[index].socialInfo.name,
                                    list[index].socialInfo.id,
                                    post: widget.post,
                                  ));
                          if (result == true) {
                            Navigator.of(context).pop(true);
                          }
                        }));
              },
              trailing: arrowRight(),
            ),
            itemCount: list.length,
            separatorBuilder: (BuildContext context, int index) => Divider(),
          );
        },
      ),
    );
  }
}

Widget _buildPostWidget(BuildContext context, Post post) {
  return Container(
      margin: const EdgeInsets.symmetric(horizontal: 8.0),
      padding: const EdgeInsets.symmetric(vertical: 7.0, horizontal: 8.0),
      decoration: new BoxDecoration(
        color: Color(0xfff1f1f1), // 底色
        borderRadius: new BorderRadius.all(Radius.circular(10.0)),
      ),
      child: Row(
        children: <Widget>[
          if (post.images != null && post.images.isNotEmpty)
            ClipRRect(
              child: CachedNetworkImage(
                imageUrl: post.images[0].src,
                fit: BoxFit.cover,
                width: 50,
                height: 50,
              ),
              // 也可控件一边圆角大小
              borderRadius: new BorderRadius.all(Radius.circular(6.0)),
            ),
          Expanded(
            child: Container(
              margin: const EdgeInsets.all(5.0),
              child: Text(post.content,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: Theme.of(context).textTheme.subtitle1),
            ),
          )
        ],
      ));
}
