import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sport/bean/comment.dart';
import 'package:sport/bean/post_user.dart';
import 'package:sport/pages/social/notification.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/dialog/bindphone_dialog.dart';

import 'button_primary.dart';
import 'decoration.dart';

class TextInput extends StatefulWidget {
  final FocusNode focusNode;
  final String subjectId;
  final String parentCommentId;
  final String toCommentId;
  final VoidCallback callback;
  final bool comment;
  final PostUser user;
  final bool autoFocus;
  final bool atUser;

  TextInput(this.subjectId, {this.focusNode, this.parentCommentId, this.toCommentId, this.callback, this.comment = false, this.user, this.autoFocus = false, this.atUser = true});

  @override
  State<StatefulWidget> createState() {
    return _TextInputState();
  }
}

class _TextInputState extends State<TextInput> with InjectApi {
  var _textFieldValue = "";
  TextEditingController _controller;
  FocusNode _focusNode;
  var _posting = false;

  @override
  void initState() {
    super.initState();
    _focusNode = widget.focusNode ?? FocusNode();
    _controller = TextEditingController();
    if (widget.comment) {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        FocusScope.of(context).requestFocus(_focusNode);
      });
    }
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: shadowTop(),
      child: SafeArea(
        child: ConstrainedBox(
          constraints: BoxConstraints(maxHeight: 150, minHeight: 50),
          child: Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  child: TextField(
                    controller: _controller,
                    focusNode: _focusNode,
                    autofocus: widget.autoFocus,
                    keyboardType: TextInputType.multiline,
                    strutStyle: StrutStyle(height: 1.1),
                    minLines: 1,
                    maxLines: 3,
                    maxLength: 200,
                    onChanged: (value) {
                      setState(() {
                        _textFieldValue = value;
                      });
                    },
                    style: TextStyle(fontSize: 15.0),
                    decoration: InputDecoration(
                        counterText: "",
                        hintText: "发表你的看法...",
                        hintStyle: TextStyle(color: Color(0xff999999),fontSize: 15.0),
                        prefixIconConstraints: BoxConstraints(minWidth: 36),
                        prefixIcon: Padding(
                          padding: const EdgeInsets.fromLTRB(12.0 ,0,0,0),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Image.asset("lib/assets/img/bbs_icon_reportx.png"),
                              SizedBox(
                                width: 4,
                              ),
                              if (widget.user != null && widget.atUser == true)
                                ConstrainedBox(
                                    constraints: BoxConstraints(maxWidth: 100),
                                    child: Text(
                                      "@${widget.user.name}",
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ))
                            ],
                          ),
                        ),
                        contentPadding: EdgeInsets.symmetric(vertical: 16.0),
                        border: InputBorder.none),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12.0),
                child: _posting
                    ? SizedBox(
                        height: 22,
                        width: 22,
                        child: CircularProgressIndicator(),
                      )
                    : PrimaryButton(
                        width: 80,
                        height: 35,
                        callback: _textFieldValue.isEmpty
                            ? null
                            : () async {
                                if (await showBindPhoneDialog(context) != true) {
                                  return;
                                }
                                setState(() {
                                  _posting = true;
                                });
                                var resp = await api
                                    .postForumComment(widget.subjectId, _textFieldValue,
                                        parentCommentId: widget.parentCommentId, toCommentId: widget.toCommentId)
                                    .catchError((e) {});
                                if (resp != null && resp.code == 0) {
                                  ToastUtil.show("发表成功");
                                  _controller?.clear();
                                  _focusNode?.unfocus();
                                  widget.callback?.call();
                                  _textFieldValue = '';
                                  CommentNotification(Comment(id: resp.data.commentId, subjectId: widget.subjectId), CommentNotification.TYPE_ADD)
                                      .dispatch(context);
                                }
                                setState(() {
                                  _posting = false;
                                });
//                      SystemChannels.textInput.invokeMethod('TextInput.hide');
                              },
                        content: "发表",
                      ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
