import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sport/pages/login/login_widget.dart';
import 'package:sport/provider/user_model.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/misc.dart';

class BasicInfoPage extends StatefulWidget {
  String username;
  BasicInfoPage({this.username});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _BasicInfoPageState();
  }
}

class _BasicInfoPageState extends State<BasicInfoPage> with InjectLoginApi {
  int _genderController = 1; // 默认选中男
  String _name; // 用户名
  File _lastCropped;
  TextEditingController _textEditingController = new TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _name = widget.username;
    _textEditingController.text = widget.username;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          leading: IconButton(
            icon: arrowBack(),
            onPressed: () {
              NavigatorUtil.goHomePage(context);
            },
          ),
        ),
        body: Container(
          color: Colors.white,
          width: double.infinity,
          padding: EdgeInsets.only(top: 44.0),
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(bottom: 32.0),
                child: Text(
                  "基本信息填写",
                  style: TextStyle(
                      color: Color.fromRGBO(51, 51, 51, 1), fontSize: 25.0, fontWeight: FontWeight.w600),
                ),
              ),
              InkWell(
                child: Container(
                  width: 100.0,
                  height: 100.0,
                  margin: EdgeInsets.only(top: 40.0),
                  child: CircleAvatar(
                    radius: 100,
//                        borderRadius: BorderRadius.all(Radius.circular(200.0)),
                    backgroundImage: _lastCropped == null
                        ? AssetImage(
                            "lib/assets/img/login_image_uploadavatar.png")
                        : FileImage(
                            _lastCropped,
                          ),
                  ),
                ),
                onTap: () async {
                  dynamic data = await Navigator.push(context,
                      MaterialPageRoute(builder: (context) {
                    return BuildCroppingImagePage();
                  }));
                  setState(() {
                    _lastCropped = data;
                  });
                },
              ),
              Container(
                  padding: EdgeInsets.only(top: 32.0),
                  width: 300.0,
                  child: Column(
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            "用户名",
                            style: Theme.of(context).textTheme.headline1.copyWith(fontSize: 16.0),
                            textAlign: TextAlign.left,
                          ),
                          Expanded(
                            child: TextField(
                              textDirection: TextDirection.rtl,
                              controller: _textEditingController,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: '${widget.username}',
                                  hintStyle: TextStyle(color: Colors.grey),
                                  hintMaxLines: 1),
                              onChanged: (value) {
                                setState(() {
                                  _name = value;
                                });
                              },
                            ),
                          ),
                        ],
                      ),
                      Divider(),
                    ],
                  )),
              Container(
                width: 300.0,
                alignment: Alignment.center,
                padding: EdgeInsets.only(top: 12.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text("性别",
                        style:Theme.of(context).textTheme.headline1.copyWith(fontSize: 16.0),),
                    Row(
                      children: <Widget>[
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            // 选择男人时返回1并更新组件。
                            setState(() {
                              _genderController = 1;
                              // widget.selectCallback(_genderController);
                            });
                          },
                          child: Row(
                            children: <Widget>[
                              _genderController == 1
                                  ? Image.asset(
                                      'lib/assets/img/pop_icon_conneted.png',
                                      fit: BoxFit.contain,
                                      height: 16.0,
                                      width: 16.0,
                                    )
                                  : Container(height: 16, width: 16,),
                              Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: Text(
                                  '男',
                                  strutStyle: fixedLine,
                                  style: TextStyle(
                                    fontSize: 16.0,
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(
                          width: 32.0,
                        ),
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            // 选择女人时返回2并更新组件。
                            setState(() {
                              _genderController = 2;
                              // widget.selectCallback(_genderController);
                            });
                          },
                          child: Row(
                            children: <Widget>[
                              _genderController == 2
                                  ? Image.asset(
                                      'lib/assets/img/pop_icon_conneted.png',
                                      fit: BoxFit.contain,
                                      height: 16.0,
                                      width: 16.0,
                                    )
                                  :  Container(height: 16, width: 16,),
                              Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: Text(
                                  '女',
                                  strutStyle: fixedLine,
                                  style: TextStyle(
                                    fontSize: 16.0,
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              Container(
                width: 300,
                margin: EdgeInsets.symmetric(vertical: 25),
                child: PrimaryButton(content: "完成",callback: (){
                  loginApi
                      .updateInfo(_name, _genderController)
                      .then((value) async {
                    print("$value");
                    if (value['code'] == 0) {
                      SharedPreferences prefs =
                      await SharedPreferences.getInstance();
                      if (value['data']['avatar'] != null) {
                        prefs.setString("avatar", value['data']['avatar']);
                      }
                      prefs.setString("name", value['data']['name']);
                      Provider.of<UserModel>(context, listen: false).initUser();
                      NavigatorUtil.goHomePage(context);
                    }
                  });
                },),
              )
            ],
          ),
        ));
  }
}
