import 'package:flutter/material.dart' hide NestedScrollView;
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:sport/bean/post_user.dart';
import 'package:sport/bean/user_info.dart';
import 'package:sport/provider/lib/provider_widget.dart';
import 'package:sport/provider/lib/simple_model.dart';
import 'package:sport/provider/lib/view_state_lifecycle.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/DateFormat.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/error.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/loading.dart';
import 'package:sport/widgets/misc.dart';

class BlockUserListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageDetailState();
}

class _PageDetailState extends ViewStateLifecycle<BlockUserListPage, SimpleModel> with InjectApi {
  TextEditingController _controller;
  FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
    _controller = new TextEditingController(text: '');
  }

  @override
  void dispose() {
    super.dispose();
    _focusNode?.dispose();
    _controller?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: buildAppBar(context, title: "屏蔽列表"),
      body: ProviderWidget<SimpleModel>(
          model: model,
          onModelReady: (model) => model.initData(),
          builder: (_, model, __) {
            return EasyRefresh.custom(
              firstRefresh: true,
              onRefresh: () => model.refresh(),
              onLoad: () => model.loadMore(),
              enableControlFinishRefresh: true,
              controller: model.refreshController,
              header: buildClassicalHeader(),
              footer: buildClassicalFooter(),
              slivers: <Widget>[
                SliverToBoxAdapter(
                  child: _searchWidget(),
                ),
                if (model.isBusy)
                  SliverToBoxAdapter(
                    child: RequestLoadingWidget(),
                  ),
                if (model.isEmpty)
                  SliverToBoxAdapter(
                    child: RequestErrorWidget(
                      null,
                      msg: "暂无用户~",
                    ),
                  ),
                if (model.isIdle)
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return _buildItem(model.list[index]);
                      },
                      childCount: model.list.length,
                    ),
                  ),
                if (model.isError)
                  SliverToBoxAdapter(
                    child: RequestErrorWidget(() {
                      model.initData();
                    }),
                  ),
              ],
            );
          }),
    );
  }

  Widget _buildItem(UserInfo user) {
    return Column(
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(12.0),
          child: InkWell(
              onTap: () => NavigatorUtil.goSocialUserDetail(context, PostUser(id: "${user?.id}", name: user?.name, avatar: user?.avatar)),
              child: Row(
                children: <Widget>[
                  CircleAvatar(
                    backgroundImage: userAvatarProvider(user?.avatar),
                    radius: 22,
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  Expanded(
                    child: Text(
                      "${user?.name}",
                      style: Theme.of(context).textTheme.headline3,
                    ),
                  ),
                  GestureDetector(
                    child: Container(
                      width: 82,
                      height: 30,
                      alignment: Alignment.center,
                      child: Text(
                        "取消屏蔽",
                        strutStyle: fixedLine,
                        style: Theme.of(context).textTheme.bodyText2,
                      ),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        border: Border.all(
                          color: Color(0xff999999),
                          width: .5,
                        ),
                      ),
                    ),
                    onTap: () async {
                      await request(context, () async {
                        var resp = await api.postForumUnBlockUser("${user?.id}").catchError((onError) {});
                        if (resp?.code == 0) {
                          setState(() {
                            model.list.remove(user);
                          });
                        }
                      });
                    },
                  )
                ],
              )),
        ),
        Divider(
          height: 1,
        )
      ],
    );
  }

  Widget _searchWidget() {
    return Container(
      margin: EdgeInsets.fromLTRB(12.0, 6.0, 12.0, 6.0),
      height: 40,
      padding: EdgeInsets.fromLTRB(12.0, 0, 12.0, 0),
      decoration: BoxDecoration(
        color: Color(0xffF1F1F1),
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.all(Radius.circular(50)),
      ),
      child: Row(
        children: <Widget>[
          Image.asset("lib/assets/img/searchbar_icon_search.png"),
          SizedBox(
            width: 6,
          ),
          Expanded(
            child: TextField(
              controller: _controller,
              maxLines: 1,
              focusNode: _focusNode,
              decoration: InputDecoration(
                hintText: '输入账号/用户昵称',
                // contentPadding: EdgeInsets.all(10),
                border: InputBorder.none,
              ),
              onChanged: (value) {},
              onSubmitted: (value) {},
            ),
          ),
        ],
      ),
    );
  }

  @override
  SimpleModel createModel() => SimpleModel((page) async => (await api.getMyBlockUsers()).results);
}
