import 'package:flutter/material.dart';
import 'package:sport/bean/bind_info.dart';
import 'package:sport/pages/setting/bind_phone_page.dart';
import 'package:sport/pages/setting/reset_password.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/sharesdk/tencent.dart';
import 'package:sport/sharesdk/wechat.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/list_tile.dart';
import 'package:sport/widgets/loading.dart';

class AccountPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<AccountPage> with InjectLoginApi, TencentMixin, WechatMixin {
  BindInfoData _data;
  bool _loading = true;

  @override
  void initState() {
    super.initState();
    initData();
  }

  initData() async {
    BindInfo bindInfo = await loginApi.getBindInfo();
    setState(() {
      _loading = false;
      _data = bindInfo.data;
    });
  }

  @override
  Widget build(BuildContext context) {
    const contentPadding = EdgeInsets.symmetric(horizontal: 0.0);
    return Scaffold(
        backgroundColor: Colors.white,
        body: LoadingWidget(
          loading: _loading,
          child: CustomScrollView(
            slivers: <Widget>[
              buildSliverAppBar(context, "帐号管理"),
              _data == null
                  ? SliverToBoxAdapter(
                      child: Container(),
                    )
                  : SliverPadding(
                      padding: const EdgeInsets.symmetric(horizontal: 12),
                      sliver: SliverToBoxAdapter(
                        child: Column(
                          children: <Widget>[
                            ListView(
                              padding: EdgeInsets.zero,
                              physics: NeverScrollableScrollPhysics(),
                              shrinkWrap: true,
                              children: divideTiles(
                                includeLast: true,
                                context: context,
                                tiles: [
                                  if (_data?.phone?.isNotEmpty == true)
                                    ListTile(
                                      title: Text('重置密码'),
                                      trailing: arrowRight5(),
                                      contentPadding: contentPadding,
                                      onTap: () {
                                        Navigator.push(context, MaterialPageRoute(builder: (context) {
                                          return RestPasswordWidget(_data.password);
                                        }));
                                      },
                                    ),
                                  ListTile(
                                    title: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Text("绑定手机号"),
                                        Text(
                                          _data.phone != null ? "${_data.phone}" : " ",
                                          style: Theme.of(context).textTheme.bodyText2,
                                        )
                                      ],
                                    ),
                                    contentPadding: contentPadding,
                                    trailing: arrowRight5(),
                                    onTap: () async {
                                      var result = await NavigatorUtil.goPage(context, (context) => BindPhonePage());
                                      if (result == true) {
                                        await initData();
                                        ToastUtil.show("绑定成功");
                                      }
                                    },
                                  ),
                                  ListTile(
                                    title: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Text("绑定微信"),
                                        _data.weixin
                                            ? Text(
                                                "已绑定",
                                                style: Theme.of(context).textTheme.bodyText2,
                                              )
                                            : Text(
                                                "未绑定",
                                                style: Theme.of(context).textTheme.bodyText2,
                                              ),
                                      ],
                                    ),
                                    contentPadding: contentPadding,
                                    trailing: arrowRight5(),
                                    onTap: () {
                                      wechatLogin((code) async {
                                        if (code == null) {
                                          ToastUtil.show("请先安装最新版的微信客户端");
                                          return;
                                        }
                                        await request(context, () async {
                                          var data = await loginApi.bindWx(code);
                                          if (data["code"] == 0) {
                                            ToastUtil.show("绑定成功");
                                            setState(() {
                                              _data.weixin = true;
                                            });
                                          }
                                        });
                                      });
                                    },
                                  ),
                                  ListTile(
                                    title: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Text("绑定QQ"),
                                        _data.qq
                                            ? Text(
                                                "已绑定",
                                                style: Theme.of(context).textTheme.bodyText2,
                                              )
                                            : Text(
                                                "未绑定",
                                                style: Theme.of(context).textTheme.bodyText2,
                                              ),
                                      ],
                                    ),
                                    contentPadding: contentPadding,
                                    trailing: arrowRight5(),
                                    onTap: () {
                                      qqlogin((login, userInfo) async {
                                        if (userInfo == null) {
                                          return;
                                        }

                                        await request(context, () async {
                                          String openid = login.openid;
                                          var data = await loginApi.bindQQ(openid);
                                          if (data["code"] == 0) {
                                            ToastUtil.show("绑定成功");
                                            setState(() {
                                              _data.qq = true;
                                            });
                                          }
                                        });
                                      });
                                    },
                                  ),
                                ],
                              ).toList(),
                            ),
                          ],
                        ),
                      ),
                    ),
            ],
          ),
        ));
  }
}
