import 'dart:io';
import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sport/bean/forum.dart';
import 'package:sport/bean/image.dart' as photo;
import 'package:sport/bean/post.dart';
import 'package:sport/pages/social/gallery_photo_view.dart';
import 'package:sport/pages/social/social_detail_page.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/dialog/alert_dialog.dart';
import 'package:sport/widgets/dialog/bindphone_dialog.dart';
import 'package:sport/widgets/space.dart';

class PostPage extends StatefulWidget {
  final String id;
  final Forum forum;
  final Post post;

  const PostPage(this.id, {this.post, this.forum});

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<PostPage> {
  List<Asset> imageList = [];
  TextEditingController _controller;
  ValueNotifier<String> _valueNotifier = ValueNotifier("");
  FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
    _controller = TextEditingController()..addListener(() {});
  }

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
    _focusNode?.dispose();
    _valueNotifier?.dispose();
    PaintingBinding.instance.imageCache.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: buildBackButton(context),
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(""),
            PrimaryButton(
              width: 65,
              height: 35,
              content: "发布",
              callback: () async {
                if (await showBindPhoneDialog(context) != true) {
                  return ;
                }
                // if(widget.forum != null) {
                //   NavigatorUtil.pushAndRemoveUntil(context, (context) => SocialDetailPage(widget.forum, index: 2), RouteSettings(name: "forum"));
                // }else {
                //   Navigator.of(context).pop(true);
                // }
                String postValue = _valueNotifier.value.trim();
                if (postValue == "") {
                  ToastUtil.show("不能发布空白内容喔！");
                  return;
                }

                if (await showDialog(
                      context: context,
                      builder: (context) => CustomAlertDialog(title: '是否确认发布', ok: () => Navigator.of(context).pop(true)),
                    ) !=
                    true) {
                  return;
                }

                bool result = await showDialog(
                    context: context,
                    barrierDismissible: false,
                    builder: (context) => SimpleDialog(
                          children: <Widget>[
                            PostAction(widget.id, postValue, imageList, widget.post?.quoteSubjectId == '0' ? widget.post?.id : widget.post?.quoteSubjectId)
                          ],
                        ));
                if (result == true) {
                  ToastUtil.show("发布成功");
                  if(widget.forum != null) {
                    NavigatorUtil.pushAndRemoveUntil(context, (context) => SocialDetailPage(widget.forum, index: 2), RouteSettings(name: "forum"));
                  }else {
                    Navigator.of(context).pop(true);
                  }
                } else {
                  // ToastUtil.show("已取消发布");
                }
              },
            )
          ],
        ),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12.0),
          child: Form(
            onWillPop: () async {
              if (_valueNotifier.value.isNotEmpty || imageList.isNotEmpty) {
                bool result = await showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (context) {
                          return CustomAlertDialog(
                            title: '确认关闭吗？',
                            ok: () {
                              Navigator.of(context).pop(true);
                            },
                          );
                        }) ??
                    false;
                return result;
              }
              return true;
            },
            child: Column(
              children: <Widget>[
                TextFormField(
                  focusNode: _focusNode,
                  controller: _controller,
                  keyboardType: TextInputType.multiline,
                  maxLines: 8,
                  maxLength: 500,
                  style: TextStyle(fontSize: 16),
                  strutStyle: StrutStyle(forceStrutHeight: true, height: 1.4),
                  onChanged: (v) {
                    _valueNotifier.value = v;
                    if (_valueNotifier.value.length == 500) {
                      ToastUtil.show("文字数量已达上限");
                    }
                  },
                  buildCounter: (
                    BuildContext context, {
                    int currentLength,
                    int maxLength,
                    bool isFocused,
                  }) {
                    return Align(alignment: Alignment.centerLeft, child: Padding(
                      padding: const EdgeInsets.only(left:8.0),
                      child: Text("$currentLength/$maxLength"),
                    ));
                  },
                  decoration: InputDecoration(hintText: widget.post == null ? '发表你的看法...' : '分享文字...', border: InputBorder.none,
                  contentPadding: EdgeInsets.symmetric(horizontal: 6),
                  hintStyle: TextStyle(color: Color(0xff999999))),
                ),
                Space(
                  height: 16,
                ),
                widget.post == null
                    ? GridView.builder(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: imageList.length + (imageList.length < 9 ? 1 : 0),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, crossAxisSpacing: 12.0, mainAxisSpacing: 12.0),
                        itemBuilder: (context, index) {
                          return InkWell(
                            onTap: () => _select(),
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(6),
                              child: index >= imageList.length
                                  ? Image.asset(
                                      "lib/assets/img/bbs_icon_addimage.png",
                                      fit: BoxFit.cover,
                                    )
                                  : AssetThumb(
                                      asset: imageList[index],
                                      quality: 50,
                                      width: 200,
                                      height: 200,
                                    ),
                            ),
                          );
                        })
                    : Container(
                        padding: EdgeInsets.all(11.0),
                        width: double.infinity,
                        decoration: BoxDecoration(
                            shape: BoxShape.rectangle, borderRadius: BorderRadius.all(Radius.circular(10)), color: Theme.of(context).scaffoldBackgroundColor),
                        child: _postWidget(),
                      ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _postWidget() {
    Post post = widget.post.quoteSubject ?? widget.post;
    double width = MediaQuery.of(context).size.width - 24 - 22;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        RichText(
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
          text: TextSpan(style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 16), children: <InlineSpan>[
            TextSpan(text: '${post.nickname}：', style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor)),
            TextSpan(text: '${post.content}', style: Theme.of(context).textTheme.subtitle1),
          ]),
        ),
        if (post.images.length > 0)
          GridView.count(
              physics: new NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              padding: EdgeInsets.only(top: 15),
              childAspectRatio: post.images.length == 1 ? max(16 / 10, post.images[0].getImageAspectRatio()) : 1,
              crossAxisSpacing: 10.0,
              crossAxisCount: min(3, post.images.length),
              children: post.images
                  .asMap()
                  .keys
                  .take(min(3, post.images.length))
                  .map((i) => GestureDetector(
                      onTap: () => open(context, i, post.images),
                      child: i < 2
                          ? post.images.length == 1
                              ? Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: <Widget>[
                                    ClipRRect(
                                        borderRadius: BorderRadius.circular(6),
                                        child: Stack(
                                          children: <Widget>[
                                            CachedNetworkImage(
                                              alignment: Alignment.centerLeft,
                                              imageUrl: post.images[i].thumbnail,
                                              fit: BoxFit.cover,
                                              width: post.images[i].getWidth(width),
                                            ),
                                            if (post.images[i].isLongImage())
                                              Positioned(
                                                bottom: 4,
                                                right: 4,
                                                child: Container(
                                                  padding: EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                                                  decoration:
                                                      BoxDecoration(color: Colors.black.withOpacity(.8), borderRadius: BorderRadius.all(Radius.circular(20))),
                                                  child: Text(
                                                    "长图",
                                                    style: Theme.of(context).textTheme.bodyText1.copyWith(color: Colors.white),
                                                  ),
                                                ),
                                              )
                                          ],
                                        ))
                                  ],
                                )
                              : ClipRRect(
                                  borderRadius: BorderRadius.circular(6),
                                  child: CachedNetworkImage(alignment: Alignment.centerLeft, imageUrl: post.images[i].thumbnail, fit: BoxFit.cover))
                          : ClipRRect(
                              borderRadius: BorderRadius.circular(6),
                              child: Stack(
                                fit: StackFit.expand,
                                children: <Widget>[
                                  CachedNetworkImage(
                                    imageUrl: post.images[i].thumbnail,
                                    fit: BoxFit.cover,
                                  ),
                                  if (post.images.length - 3 > 0)
                                    Container(
                                      color: Color(0x80000000),
                                      child: Center(
                                        child: Text(
                                          "+${post.images.length - 3}",
                                          style: TextStyle(color: Colors.white, fontSize: 16),
                                        ),
                                      ),
                                    )
                                ],
                              ))))
                  .toList()),
      ],
    );
  }

  void _select() async {
    _focusNode?.unfocus();
    List<Asset> resultList;
    String error;

//    int max = 9 - imageList.length;
//    if (max <= 0) {
//      Fluttertoast.showToast(msg: "不能再添加了~", backgroundColor: Colors.black, textColor: Colors.white, fontSize: 16.0);
//      return;
//    }

    try {
      resultList = await MultiImagePicker.pickImages(
          maxImages: 9,
          selectedAssets: imageList,
          materialOptions: MaterialOptions(
            actionBarTitle: "选择图片",
            allViewTitle: "选择图片",
            useDetailsView: true,
            startInAllView: true,
            selectionLimitReachedText: "不能选择更多了~",
          ),
          cupertinoOptions: CupertinoOptions(
            selectionFillColor: "#ff11ab",
            selectionTextColor: "#ffffff",
            selectionCharacter: "✓",
          ));
    } on Exception catch (e) {
      error = e.toString();
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    if (resultList == null || resultList.isEmpty) return;

    setState(() {
      imageList = resultList;
    });
  }
}

class PostAction extends StatefulWidget {
  final String forumId;
  final String content;
  final List<Asset> imageList;
  final String quoteSubjectId;

  const PostAction(this.forumId, this.content, this.imageList, this.quoteSubjectId);

  @override
  State<StatefulWidget> createState() => PostActionState();
}

class PostActionState extends State<PostAction> with InjectApi {
  final Map<Asset, photo.Image> upload = {};
  ValueNotifier<String> _msg;
  bool _disposed = false;

  @override
  void initState() {
    super.initState();
    _disposed = false;
    _msg = ValueNotifier<String>("请稍候...");
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      post();
    });
  }

  @override
  void dispose() {
    _disposed = true;
    _msg?.dispose();
    super.dispose();
  }

  void post() async {
    List<Asset> imageList = widget.imageList;
    if (imageList != null && imageList.isNotEmpty) {
      Directory tempDir = await getTemporaryDirectory();
      Directory directory = new Directory('${tempDir.path}/upload');
      if (!directory.existsSync()) {
        directory.createSync();
        print('文档初始化成功，文件保存路径为 ${directory.path}');
      }

      for (var i = 0; i < imageList.length; i++) {
        if (_disposed) break;
        Asset asset = imageList[i];
        if (upload.containsKey(asset)) continue;
        ByteData byteData = await asset.getByteData(quality: 85);
        File file = File('${directory.path}/${DateTime.now().millisecondsSinceEpoch}_$i.jpg');
        List<int> bytes = byteData.buffer.asUint8List().toList();
        print('临时文件 ${file.path} ${bytes.length}');
        file.writeAsBytesSync(bytes);
        _msg.value = "上传图片(${i + 1}/${imageList.length})...";
        var resp = await api.mediaUp4Subject(file, srcType: "image");
        photo.Image image = resp.data;
        file.delete();
        upload[asset] = image;
        // await Future.delayed(Duration(seconds: 3));
      }
    }

    _msg.value = "发布中...";
    // await Future.delayed(Duration(seconds: 3));
    if (_disposed) return;
    var data =
        await api.postForum(widget.forumId, widget.content, images: upload.values.map((e) => e.id).toList().join(","), quoteSubjectId: widget.quoteSubjectId);
    if (data != null && data.code == 0) {
      Navigator.of(context).pop(true);
    } else {
      Navigator.of(context).pop(false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 16),
      child: Column(
        children: <Widget>[
          CircularProgressIndicator(),
          Padding(
            padding: const EdgeInsets.only(top: 15),
            child: ValueListenableBuilder(valueListenable: _msg, builder: (BuildContext context, String value, Widget child) => Text(value)),
          )
        ],
      ),
    );
  }
}
