import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:sport/bean/post_user.dart';
import 'package:sport/pages/social/user_friend_add_page.dart';
import 'package:sport/provider/bluetooth.dart';
import 'package:sport/provider/user_model.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/router/routes.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/list_tile.dart';
import 'package:sport/widgets/space.dart';
import 'package:sport/widgets/dialog/search_device.dart';

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

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

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Material(
        child: Scaffold(
          backgroundColor: Colors.white,
          body: SafeArea(
            child: Column(
              children: <Widget>[
                // 放外面一点的吧
//              InkWell(
//                onTap: () => NavigatorUtil.go(context, Routes.userInfo),
//                child:
//                AspectRatio(
//                  aspectRatio: 1125 / 435.0,
//                  child: Container(
//                    alignment: Alignment.bottomCenter,
//                    padding: EdgeInsets.fromLTRB(24.0, 0, 24.0, 34.0),
//                    decoration: new BoxDecoration(
////                      image: new DecorationImage(
////                        image: new AssetImage("lib/assets/img/bg_userprofile.jpg"),
////                      ),
//                    ),
//                  ),
//                ),
//              ),
                Space(height: 37.0,),

                Container(
                    padding: EdgeInsets.only(top: 12.0),
                    transform: Matrix4.translationValues(0, -10, 0),
                    decoration: BoxDecoration(borderRadius: BorderRadius.vertical(top: Radius.circular(10)), color: Colors.white),
                    child: ListView(
                      padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 0),
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      children: divideTiles(
                        context: context,
                        includeLast: true,
                        tiles: [
                          ListTile(
                            contentPadding: EdgeInsets.only(bottom: 20, top: 5),
                            title: Consumer<UserModel>(
                                builder: (_, model, __) => model.user == null
                                    ? Container()
                                    : Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Row(
                                      children: <Widget>[
                                        CircleAvatar(
                                          backgroundImage: userAvatarProvider(model.user.avatar),
                                          radius: 30,
                                        ),
                                        Space(
                                          width: 12,
                                        ),
                                        Column(
                                          mainAxisSize: MainAxisSize.min,
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(model.user.name ?? "", style: TextStyle(color: Color(0xff333333), fontSize: 18,fontWeight: FontWeight.w600)),
                                            Space(
                                              height: 7,
                                            ),
                                            Row(
                                              children: <Widget>[
                                                Row(
                                                  children: <Widget>[
                                                    Image.asset("lib/assets/img/mine_icon_${model.user.gender == 1 ? "man" : "girl"}_gray.png"),
                                                    Space(
                                                      width: 4,
                                                    ),
                                                    Text(
                                                      model.user.gender == 1 ? "男" : "女",
                                                      style: TextStyle(color: Color(0xff999999), fontSize: 12),
                                                    ),
                                                  ],
                                                ),
                                                if ((model.user.age ?? 0) != 0)
                                                  Padding(
                                                    padding: const EdgeInsets.only(left: 10.0),
                                                    child: Text(
                                                      "${model.user.age}岁",
                                                      style: TextStyle(color: Color(0xff999999), fontSize: 12),
                                                    ),
                                                  ),
                                                if (model.user.city != null)
                                                  Padding(
                                                    padding: const EdgeInsets.only(left: 10.0),
                                                    child: Text(
                                                      "${model.user.province}${model.user.province == model.user.city ? '' : model.user.city}",
                                                      style: TextStyle(color: Colors.white, fontSize: 12),
                                                    ),
                                                  ),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                    Row(
                                      children: <Widget>[
                                        Container(
                                          width: 22.0,
                                          height: 22.0,
                                          child: Image.asset("lib/assets/img/mine_icon_code.png"),
                                        ),
                                        Space(
                                          width: 17.0,
                                        ),
                                        arrowRight5(),
                                      ],
                                    )
                                    ,],
                                )
                            ),
                            onTap:() => NavigatorUtil.go(context, Routes.userInfo),
                          ),
                          Selector<Bluetooth, BluetoothDevice>(
                            selector: (_, bluetooth) => bluetooth.device,
                            builder: (_, device, ___) {
                              return device == null
                                  ? ListTile(
                                title: Row(
                                  mainAxisAlignment:MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Text("暂无连接设备",style: TextStyle(color: Color(0xff999999),fontSize: 16),),
                                    Text("前往连接",style: TextStyle(color: Color(0xff999999),fontSize: 14),)
                                  ],
                                ),
                                contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                                trailing: arrowRight5(),
                                onTap: () => showDialog(context: context, child: SearchDeviceDialog())
                              )
                                  : ListTile(
                                leading: Padding(padding: EdgeInsets.only(bottom: 10, top: 5), child: Image.asset("lib/assets/img/mine_image_shoe.png")),
                                title: Padding(
                                    padding: EdgeInsets.fromLTRB(0, 0, 12.0, 10),
                                    child:
                                    ValueListenableBuilder(
                                        valueListenable: Provider.of<Bluetooth>(context, listen: false).electricityNotifier,
                                        builder: (_, data, ___) => Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              "剩余电量 $data%",
                                              style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 12),
                                            ),
                                            Space(
                                              height: 8,
                                            ),
                                            ClipRRect(
                                              borderRadius: BorderRadius.circular(10),
                                              child: Container(
                                                child: LinearProgressIndicator(
                                                  value: data / 100,
                                                  valueColor: AlwaysStoppedAnimation(Color.fromRGBO(0, 220, 66, 1)
//                                            Color(0xffFF5B1D)
                                                  ),
                                                ),
                                                height: 7,
                                              ),
                                            )
                                          ],
                                        ))),
                                trailing: arrowRight5(),
                                contentPadding: EdgeInsets.symmetric(horizontal: 0.0,vertical: 10.0),
                                onTap: () {
                                  var device = Provider.of<Bluetooth>(context, listen: false).device;
                                  if (device == null) {
                                    ToastUtil.show("没连接设备！");
                                    return;
                                  }
                                  NavigatorUtil.go(context, Routes.deviceInfo);
                                },
                              );
                            },
                          ),
                          // ListTile(
                          //   title: Text("运动详情",style: TextStyle(fontSize: 16),),
                          //   contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                          //   trailing: arrowRight5(),
                          //   onTap: () => NavigatorUtil.go(context, Routes.sportDetail),
                          // ),
                          ListTile(
                            title: Text("我的等级",style: TextStyle(fontSize: 16),),
                            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                            trailing: arrowRight5(),
                            onTap: () => NavigatorUtil.go(context, Routes.level),
                          ),
                          ListTile(
                            title: Text("我的运动",style: TextStyle(fontSize: 16)),
                            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                            trailing: arrowRight5(),
                            onTap: () => NavigatorUtil.go(context, Routes.gamelistpage),
                          ),
                          ListTile(
                            title: Text("我的帖子",style: TextStyle(fontSize: 16)),
                            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                            trailing: arrowRight5(),
                            onTap: () {
                              var userModel = Provider.of<UserModel>(context, listen: false);
                              var user = userModel.user;
                              NavigatorUtil.goSocialUserDetail(context, PostUser(id: "${user.id}", name: user.name, avatar: user.avatar));
                            },
                          ),
                          ListTile(
                            title: Text("用户反馈",style: TextStyle(fontSize: 16)),
                            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                            trailing: arrowRight5(),
                            onTap: () => NavigatorUtil.go(context, Routes.feedback),
                          ),
                          ListTile(
                            title: Text("设置",style: TextStyle(fontSize: 16)),
                            contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
                            trailing: arrowRight5(),
                            onTap: () => NavigatorUtil.go(context, Routes.setting),
                          ),
                        ],
                      ).toList(),
                    )),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
