import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:intl/intl.dart';
import 'package:package_info/package_info.dart';
import '../styles/colors.dart';
import 'dart:io';
import 'dart:async';
import 'dart:ui';
import '../styles/totast.dart';
import 'ChangeUserInfo.dart';
import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../redux/AppState.dart';
import '../model/UserInfo.dart';
import '../net/HttpManager.dart';
import '../net/Result.dart';
import '../redux/UserRedux.dart';
import 'package:image_cropper/image_cropper.dart';

class UserChange extends StatefulWidget {
  @override
  UserChangeState createState() => UserChangeState();
}

class UserChangeState extends State<UserChange> {
  String version = '';
  bool receiveMsg = false;

  @override
  void initState() {
    super.initState();
    PackageInfo.fromPlatform().then((packageInfo) {
      setState(() {
        version = packageInfo.version;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return StoreConnector<AppState, UserInfo>(
        converter: (Store store) => store.state.userInfo,
        builder: (context, userInfo) {
          return WillPopScope(
              key: Key('UserChangesPage'),
              child: Scaffold(
                backgroundColor: PAGE_BACKGROUND_COLOR,
                appBar: AppBar(
                  // backgroundColor: PRIMARY_COLOR,
                  title: Text('系统设置'),
                  centerTitle: true,
                  elevation: 0,
                ),
                body: Stack(
                  children: <Widget>[
                    RefreshIndicator(
                      color: PRIMARY_COLOR,
                      backgroundColor: Colors.white,
                      displacement: 10,
                      onRefresh: () async {
                        await Future.delayed(const Duration(seconds: 1));
                        getUserInfo();
                      },
                      child: SizedBox.expand(
                        child: SingleChildScrollView(
                          physics: AlwaysScrollableScrollPhysics(),
                          child: Column(
                            children: <Widget>[
                              _sectionDivier(),
                              _section([
                                _cell(
                                  '头像',
                                  ClipOval(
                                    child: new SizedBox(
                                      width: 36.0,
                                      height: 36.0,
                                      child: Image.network(
                                        userInfo.icon,
                                        fit: BoxFit.fill,
                                      ),
                                    ),
                                  ),
                                  onTap: () {
                                    _chooseImage();
                                  },
                                ),
                                _cell(
                                  '昵称',
                                  userInfo.nickname,
                                  placeholder: '请填写昵称',
                                  onTap: () {
                                    Navigator.push(
                                        context,
                                        CupertinoPageRoute(
                                            builder: (context) =>
                                                ChangeUserInfo(
                                                    title: '昵称',
                                                    val: userInfo.nickname)));
                                  },
                                ),
                                _cell(
                                  '用户Code',
                                  userInfo.id.toString(),
                                ),
                                _cell(
                                  '性别',
                                  userInfo.sex,
                                  placeholder: '请添加性别信息',
                                  onTap: () {
                                    _chooseSex(context);
                                  },
                                ),
                                _cell(
                                  '生日',
                                  DateFormat('yyyy-MM-dd').format(
                                    DateTime.fromMillisecondsSinceEpoch(
                                        userInfo.birthday),
                                  ),
                                  placeholder: '选择生日',
                                  onTap: () {
                                    _chooseBirthday(context);
                                  },
                                ),
                                _cell('手机', userInfo.phone, showBorder: false,
                                    onTap: () {
                                  Navigator.push(
                                      context,
                                      CupertinoPageRoute(
                                          builder: (context) => ChangeUserInfo(
                                              title: '手机号',
                                              val: userInfo.phone)));
                                })
                              ]),
                            ],
                          ),
                        ),
                      ),
                    ),
                    // Positioned(
                    //   bottom: 10,
                    //   right: 15,
                    //   left: 15,
                    //   height: 48,
                    //   child: FlatButton(
                    //       textColor: Colors.white,
                    //       color: PRIMARY_COLOR,
                    //       highlightColor: Color(0xFF763434),
                    //       child: Text(
                    //         '退出登录',
                    //         style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
                    //       ),
                    //       onPressed: () async {
                    //         Toast.show(context, '退出成功', 1500, 'success');
                    //         Navigator.push(context, MaterialPageRoute(builder: (context) => LoginFirst()));
                    //         final prefs = await SharedPreferences.getInstance();
                    //         prefs.remove('token');
                    //         JPush jpush = JPush();
                    //         jpush.deleteAlias();
                    //         // Future.delayed(const Duration(seconds: 1), () {
                    //         //   StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(null));
                    //         // });
                    //       }),
                    // )
                  ],
                ),
              ),
              onWillPop: () {
                Toast.hide();
                Navigator.pop(context);
                return Future.value(false);
              });
        });
  }

  Future<void> updateUserInfo(userInfo, value, key) async {
    if (Toast.preToast == null) {
      Toast.show(context, '加载中', -1, 'loading');
    }
    final Result res = await HttpManager.post('userInfo/update',
        data: {'id': userInfo.id, key: value});
    Toast.hide();
    if (res.success) {
      Toast.show(context, '修改成功', 1500, 'success');
      getUserInfo();
    } else {
      Toast.show(context, res.error, 1500, 'info');
    }
  }

  Future<void> getUserInfo() async {
    Result res = await HttpManager.get('userInfo/getUserInfo');
    if (res.success) {
      StoreProvider.of<AppState>(context)
          .dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
    } else {}
  }

  Future<void> _chooseSex(BuildContext context) async {
    String sex = await showCupertinoModalPopup(
        context: context,
        builder: (BuildContext context) {
          return CupertinoActionSheet(
            title: Text('选择性别'),
            actions: <Widget>[
              CupertinoActionSheetAction(
                child: Text('男'),
                onPressed: () {
                  Navigator.pop(context, '男');
                },
              ),
              CupertinoActionSheetAction(
                child: Text('女'),
                onPressed: () {
                  Navigator.pop(context, '女');
                },
              )
            ],
            cancelButton: CupertinoDialogAction(
              child: const Text('取消'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          );
        });
    if (sex != null) {
      updateUserInfo(
          StoreProvider.of<AppState>(context).state.userInfo, sex, 'sex');
    }
  }

  Future<void> _chooseBirthday(BuildContext context) async {
    UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
    DateTime date = userInfo.birthday > 0
        ? DateTime.fromMillisecondsSinceEpoch(userInfo.birthday)
        : DateTime.now();
    DateTime res = await showCupertinoModalPopup<DateTime>(
      context: context,
      builder: (BuildContext context) {
        return Container(
          height: 216,
          color: CupertinoColors.white,
          child: Column(
            children: <Widget>[
              SizedBox(
                height: 44,
                child: Stack(
                  children: <Widget>[
                    Positioned(
                      left: 0,
                      top: 0,
                      bottom: 0,
                      child: CupertinoButton(
                        child: Text(
                          '取消',
                          style: TextStyle(color: PRIMARY_COLOR),
                        ),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ),
                    Positioned(
                      right: 0,
                      top: 0,
                      bottom: 0,
                      child: CupertinoButton(
                        child: Text(
                          '确定',
                          style: TextStyle(color: PRIMARY_COLOR),
                        ),
                        onPressed: () {
                          Navigator.of(context).pop(date);
                        },
                      ),
                    )
                  ],
                ),
              ),
              Expanded(
                flex: 1,
                child: DefaultTextStyle(
                  style: const TextStyle(
                    color: CupertinoColors.black,
                    fontSize: 22.0,
                  ),
                  child: GestureDetector(
                    // Blocks taps from propagating to the modal sheet and popping.
                    onTap: () {},
                    child: SafeArea(
                      top: false,
                      child: CupertinoDatePicker(
                        mode: CupertinoDatePickerMode.date,
                        initialDateTime: date,
                        onDateTimeChanged: (DateTime newDateTime) {
                          date = newDateTime;
                        },
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        );
      },
    );
    if (res != null) {
      updateUserInfo(userInfo, res.millisecondsSinceEpoch, 'birthday');
    }
  }

  Future<void> _chooseImage() async {
    String path = await FilePicker.getFilePath(type: FileType.IMAGE);
    if (path == null) {
      return;
    }
    File croppedFile = await ImageCropper.cropImage(
      sourcePath: path,
      ratioX: 1.0,
      ratioY: 1.0,
      maxWidth: 512,
      maxHeight: 512,
      toolbarColor: PRIMARY_COLOR,
    );
    if (croppedFile == null) {
      return;
    }
    Toast.show(context, '加载中', -1, 'loading');
    Result res = await HttpManager.post('assets/uploadFile', data: {
      'file': UploadFileInfo(croppedFile, croppedFile.path),
    });
    Toast.hide();
    if (res.success) {
      updateUserInfo(StoreProvider.of<AppState>(context).state.userInfo,
          res.data[0], 'icon');
    }
  }

  Widget _sectionDivier() {
    return SizedBox(height: 10);
  }

  Widget _section(List<Widget> children) {
    return Container(
      color: CELL_COLOR,
      child: Column(
        children: children,
      ),
    );
  }

  Widget _cell(String title, dynamic child,
      {String placeholder, void Function() onTap, bool showBorder = true}) {
    Widget secondChild;
    if (child == null || (child is String && child.isEmpty)) {
      secondChild = Text(
        placeholder ?? '',
        style: TextStyle(color: PLACEHOLDER_COLOR, fontSize: 13),
      );
    } else if (child is String) {
      secondChild = Text(
        child ?? '',
        style: TextStyle(color: Colors.white, fontSize: 15),
      );
    } else {
      secondChild = child;
    }
    return Container(
      height: 60,
      padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
      child: GestureDetector(
        onTap: onTap,
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Color(0x2E000000),
                width: showBorder ? 1 : 0,
              ),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: Text(
                  title ?? '',
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              ),
              secondChild,
              onTap != null
                  ? Padding(
                      padding: EdgeInsets.only(left: 2),
                      child: Image.asset(
                        'images/icon_inter.png',
                        width: 24,
                        height: 24,
                      ),
                    )
                  : Container(
                      width: 28,
                    )
            ],
          ),
        ),
      ),
    );
  }
}
