import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/community/community_view/community_dynamic.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/retrofit/data/article.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/member_comment_list.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/tips_dialog.dart';
import 'package:huixiang/web/web_view/comment_list.dart';
import 'package:huixiang/web/web_view/input_comment.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../view_widget/classic_header.dart';
import '../view_widget/my_footer.dart';

class CommunityDetails extends StatefulWidget {
  final Map<String, dynamic> arguments;

  CommunityDetails({this.arguments});

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

class _CommunityDetails extends State<CommunityDetails>
    with WidgetsBindingObserver {
  double height = 0;
  double commentHeight = 60.h;

  // ComunityComment comunity;
  String parenId = "0";
  final GlobalKey commentKey = GlobalKey();
  final ScrollController scrollController = ScrollController();

  List<MemberCommentList> memberList = [];
  ApiService apiService;
  var commentFocus = FocusNode();

  String hintText = S.current.liuxianinjingcaidepinglunba;
  bool isKeyBoardShow = false;
  int commentTotal = 0;
  Article article;
  String businessId;

  final GlobalKey inputKey = GlobalKey();
  final TextEditingController commentTextController = TextEditingController();
  bool emojiShowing = false;
  double keyboard = -1;
  bool needShowSmiley = false;
  bool needHideSmiley = false;
  final RefreshController refreshController = RefreshController();

  void didChangeMetrics() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      isKeyBoardShow = MediaQuery.of(context).viewInsets.bottom > 0;
      if (!mounted) return;
      if (MediaQuery.of(context).viewInsets.bottom == 0) {
        if (isKeyBoardShow) {
          FocusScope.of(context).requestFocus(FocusNode());
          if (mounted)
            if(!emojiShowing)
            setState(() {
              hintText = S.current.liuxianinjingcaidepinglunba;
              isKeyBoardShow = false;
            });
        }
      } else {
        if (mounted)
          setState(() {
            isKeyBoardShow = true;
          });
      }
    });
    if (needShowSmiley && window.viewInsets.bottom <= 0.1) {
      needShowSmiley = false;
      setState(() {
        emojiShowing = true;
      });
    }
    if(needHideSmiley && window.viewInsets.bottom > 0.1) {
      needHideSmiley = false;
      setState(() {
        emojiShowing = false;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    // comunity = widget.arguments["comment"];
    EasyLoading.show(status: S.current.zhengzaijiazai,maskType: EasyLoadingMaskType.black);
    businessId = widget.arguments["businessId"];
    WidgetsBinding.instance.addObserver(this);
    commentFocus.addListener(_focusNodeListener);
    scrollController.addListener(() {
      //滚动监听,键盘如果弹起,滚动时收起
      if(MediaQuery.of(context).viewInsets.bottom > 0)
        FocusScope.of(context).requestFocus(FocusNode());
      if(needShowSmiley && window.viewInsets.bottom > 0.1)
        needHideSmiley = false;
      setState(() {
        emojiShowing = false;
      });
    });
    _queryMemberCommentList(true);
    queryDetails(businessId);
  }

  void _focusNodeListener() {
    /*if (_focusNode.hasFocus || _focusNode.consumeKeyboardToken()){
			setState(() {
				smileyPadGone = true;
			});
		}*/
  }

  _onTextFieldTap() {
    if(emojiShowing) {
      needHideSmiley = true;
    }
  }

  _onSmileyTap() {
    if(!emojiShowing && commentFocus.hasFocus && isKeyBoardShow) {
      needShowSmiley = true;
      commentFocus.unfocus();
    } else {
      setState(() {
        emojiShowing = !emojiShowing;
        isKeyBoardShow = emojiShowing;
      });
    }
  }

  @override
  void dispose(){
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
    commentFocus.removeListener(_focusNodeListener);
    refreshController.dispose();
  }

  ///详情接口
  queryDetails(id) async {
    SharedPreferences value = await SharedPreferences.getInstance();
    if (apiService == null)
      apiService = ApiService(
        Dio(),
        context: context,
        token: value.getString("token"),
      );
    BaseData<Article> baseData =
        await apiService.informationInfo(id).catchError((onError) {
      debugPrint(onError.toString());
      refreshController.refreshFailed();
    });
    if (baseData != null && baseData.isSuccess) {
      setState(() {
        article = baseData.data;
        EasyLoading.dismiss();
        refreshController.refreshCompleted();
      });
    }else {
      refreshController.refreshFailed();
    }
  }

  _onRefresh() {
    queryDetails(businessId);
    _queryMemberCommentList(true);
  }

  @override
  Widget build(BuildContext context) {
    double h = MediaQuery.of(context).viewInsets.bottom;
    if(h > 0 && keyboard < h) {
      keyboard = h;
      setState(() {
      });
    }
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          setState(() {
            emojiShowing = false;
            isKeyBoardShow = emojiShowing;
            FocusScope.of(context).requestFocus(FocusNode());
          });
        },
        child: Scaffold(
            // resizeToAvoidBottomInset: false,
          appBar: MyAppBar(
            title: S.of(context).dongtaixiangqing,
            titleColor: Colors.black,
            titleSize: 18.sp,
            background: Colors.white,
            leading: true,
            leadingColor: Colors.black,
          ),
          body: Container(
            child: Column(
              children: [
                Expanded(
                  child: SmartRefresher(
                    controller: refreshController,
                    enablePullDown: true,
                    enablePullUp: false,
                    header: MyHeader(),
                    footer: CustomFooter(
                      builder: (context, mode) {
                        return MyFooter(mode);
                      },
                    ),
                    onRefresh: () {
                      setState(() {
                        emojiShowing = false;
                        isKeyBoardShow = emojiShowing;
                        FocusScope.of(context).requestFocus(FocusNode());
                        _onRefresh();
                      });
                    },
                    scrollController:scrollController,
                    child: SingleChildScrollView(
                      physics: BouncingScrollPhysics(),
                      child: Column(
                        children: [
                          if (article != null)
                            CommunityDynamic(
                              article,
                              0,
                              exitFull: () {
                                setState(() {});
                              },
                              userId: widget.arguments != null
                                  ? widget.arguments["userId"]
                                  : widget.arguments["mid"],
                              itemCount: 3,
                              isDetails: true,
                              heightFun: (height) {
                                this.height = height +
                                    MediaQuery.of(context).padding.top +
                                    kToolbarHeight +
                                    24;
                                if (mounted) setState(() {});
                              },
                            ),
                          CommentList(
                            commentKey,
                            article?.likes ?? 0,
                            businessId,
                            4,
                            isKeyBoardShow,
                            _reply,
                            _delCommentTips,
                            12.sp,
                            requestApiFinish: (total) {
                              setState(() {
                                commentTotal = total;
                              });
                            },
                          ),
                          if (memberList == null || memberList.length == 0)
                            Container(
                              width: double.infinity,
                              alignment: Alignment.topCenter,
                              margin: EdgeInsets.only(top: 40),
                              padding: EdgeInsets.all(22.h),
                              child: Text(
                                S.of(context).zanwupinglun,
                                style: TextStyle(
                                  fontSize: 12,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xFFA0A0A0),
                                ),
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                  flex: 1,
                ),

                /// 富文本评论的输入框
                InputComment(
                  inputKey,
                  hintText,
                  isKeyBoardShow,
                  keyboard,
                  emojiShowing,
                  commentFocus,
                  commentTextController,
                  _toComment,
                  _onSmileyTap,
                  _onTextFieldTap,
                  _queryMemberComment,
                  _queryInformationLikes,
                  isLike: article?.liked ?? false,
                ),
              ],
            ),
          ),
        ));
  }

  ///给文章/活动点赞
  _queryInformationLikes() async {
    BaseData baseData =
        await apiService.informationLikes(businessId).catchError((onError) {});
    if (baseData != null && baseData.isSuccess) {
      commentKey.currentState.setState(() {});
      setState(() {
        if (article?.liked ?? false)
          article?.likes -= 1;
        else
          article?.likes += 1;
        article?.liked = !(article.liked ?? false);
      });
    } else {
      // SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
    }
  }

  ///动态发布评论
  _queryMemberComment(String content) async {
    BaseData baseData = await apiService.memberComment({
      "content": content,
      "parentId": parenId,
      "relationalId": businessId,
      "relationalType": 4
    }).catchError((error) {});
    if (baseData != null && baseData.isSuccess) {
      CommentListState state = commentKey.currentState;
      state.queryMemberCommentList();
      commentTextController.text = "";
      FocusScope.of(context).unfocus();
      _queryMemberCommentList(false);
    }
  }

  ///滑动到评论列表
  _toComment() {
    if (commentKey.currentContext == null) return;
    RenderBox firstRenderBox = commentKey.currentContext.findRenderObject();
    Offset first = firstRenderBox.localToGlobal(Offset.zero);
    scrollController.animateTo(
      first.dy +
          scrollController.offset -
          (kToolbarHeight + MediaQuery.of(context).padding.top),
      duration: Duration(milliseconds: 300),
      curve: Curves.easeIn,
    );
  }

  contentHeight() {
    double contentHeight = MediaQuery.of(context).size.height -
        kToolbarHeight -
        MediaQuery.of(context).padding.top -
        160.h;
    if ((contentHeight - 60.h) > (128.h * memberList.length)) {
      commentHeight = contentHeight - (128.h * memberList.length);
    }
  }

  ///删除评论的提示
  _delCommentTips(memberComment) {
    SmartDialog.show(widget: Tips(() {
      delComment(memberComment);
    }));
  }

  ///删除评论
  delComment(memberComment) async {
    BaseData baseData = await apiService.delComment(memberComment.id);
    if (baseData != null && baseData.isSuccess) {
      CommentListState state = commentKey.currentState;
      state.queryMemberCommentList();
    }
  }

  ///评论 回复
  _reply(memberComment) {
    FocusScope.of(context).requestFocus(commentFocus);
    parenId = memberComment.id;
    hintText = S.of(context).huifu_("${memberComment.username}");
  }

  ///评论列表
  _queryMemberCommentList(bool isOnRefresh) async {
    if (!isOnRefresh) EasyLoading.show(status: S.current.zhengzaijiazai,maskType: EasyLoadingMaskType.black);
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    if (apiService == null)
      apiService = ApiService(
        Dio(),
        context: context,
        token: sharedPreferences.getString("token"),
        // showLoading: true,
      );
    BaseData<PageInfo<MemberCommentList>> baseData =
        await apiService.memberCommentList({
      "pageNum": 1,
      "pageSize": 100,
      "relationalId": businessId,
      "relationalType": 4,
    }).catchError((error) {});
    if (baseData != null && baseData.isSuccess) {
      commentTotal = baseData.data.size;
      memberList = baseData.data.list;
      contentHeight();
      if (mounted) setState(() {});
      EasyLoading.dismiss();
    }else {
      refreshController.refreshFailed();
    }
  }
}