
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
import 'package:twong/config/style.dart';
import 'package:twong/providers/user.dart';
import 'package:twong/router/index.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/widgets/app_bar.dart';

class AccountPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AccountState();
  }
}

class _AccountState extends State<AccountPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: DColors.back,
      appBar: DAppBar("账号信息"),
      body: SafeArea(
        child: Consumer<UserModel>(
          builder: (context, model, child) =>
              ListView(
                physics: ClampingScrollPhysics(),
                children: [
                  _buildItem("头像", CircleAvatar(backgroundImage:
                  CachedNetworkImageProvider(model.info.avatar),
                      radius: 14.px), onClick: _openAction),
                  _buildItem("昵称", Text(model.info.nickname), edit: 0),
                  _buildItem("密码",
                      Text("点击修改密码", style: TextStyle(color: Colors.grey)),
                      edit: 1),
                  _buildItem("手机号", Text(Utils.encodeString(model.info.phone)),
                      right: false),
                  _buildItem(
                      "ID", Text(model.info.uid.toString()), right: false),
                ],
              ),
        ),
      ),
    );
  }

  Widget _buildItem(String key, Widget value, { bool right = true, int edit, Function onClick }) {
    return Column(
      children: [
        InkWell(
          highlightColor: Colors.transparent,
          onTap: () {
            if(onClick != null) {
              onClick();
            }
            if(edit != null) {
              Navigator.pushNamed(context,
                  RouteNames.accountEdit, arguments: edit);
            }
          },
          child: Container(
            padding: EdgeInsets.all(6.px),
            margin: EdgeInsets.only(left: 12.px, right: 12.px),
            child: Row(
              children: [
                Expanded(child: Text(key)),
                value == null ? Container() : value,
                right ? Container(
                  margin: EdgeInsets.only(left: 6.px),
                  child: Icon(Icons.chevron_right, color: Colors.grey),
                ) : Container()
              ],
            ),
          ),
        ),
        Divider()
      ],
    );
  }

  Future _openAction() {
    var imgPicker = ImagePicker();
    return showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SafeArea(child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new ListTile(
                leading: Icon(Icons.photo_camera),
                title: Text("相机拍摄"),
                onTap: () async {
                  var image = await imgPicker.getImage(
                      source: ImageSource.camera);
                  Navigator.pop(context);
                },
              ),
              new ListTile(
                leading: Icon(Icons.photo_library),
                title: Text("照片库"),
                onTap: () async {
                  var image = await imgPicker.getImage(
                      source: ImageSource.gallery);
                  Navigator.pop(context);
                },
              ),
            ],
          ));
        });
  }
}