import 'dart:ui' as ui;

import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/app/constants/sc_room_msg_type.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/shared/business_logic/usecases/sc_case.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart';
import 'package:yumi/services/audio/rtc_manager.dart';
import 'package:yumi/services/audio/rtm_manager.dart';

///聊天输入框
class RoomMsgInput extends StatefulWidget {
  String? atTextContent;

  RoomMsgInput({this.atTextContent});

  @override
  _RoomMsgInputState createState() => _RoomMsgInputState();
}

class _RoomMsgInputState extends State<RoomMsgInput> {
  bool showSend = false;

  FocusNode msgNode = FocusNode();

  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    if (widget.atTextContent != null) {
      controller.value = TextEditingValue(text: widget.atTextContent ?? "");
    }
  }

  @override
  Widget build(BuildContext context) {
    // FocusScope.of(context).requestFocus(msgNode);
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        children: <Widget>[
          Expanded(
            child: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
            ),
          ),
          float(context),
        ],
      ),
    );
  }

  Widget float(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Colors.black),
      padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 12.w),
      height: 56.0,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white24,
          borderRadius: BorderRadius.circular(height(5)),
        ),
        child: Row(
          children: [
            Expanded(
              child: ExtendedTextField(
                controller: controller,
                textDirection:
                    widget.atTextContent != null ? TextDirection.ltr : null,
                specialTextSpanBuilder: AtTextSpanBuilder(),
                focusNode: msgNode,
                autofocus: true,
                onSubmitted: (s) {
                  Navigator.pop(context);
                  if (controller.text.isNotEmpty) {
                    var currenRoom =
                        Provider.of<RtcProvider>(
                          context,
                          listen: false,
                        ).currenRoom;
                    if (currenRoom != null) {
                      Provider.of<RtmProvider>(context, listen: false).dispatchMessage(
                        Msg(
                          groupId:
                              currenRoom
                                  .roomProfile
                                  ?.roomProfile
                                  ?.roomAccount ??
                              "",
                          role:
                              Provider.of<RtcProvider>(
                                context,
                                listen: false,
                              ).currenRoom?.entrants?.roles ??
                              "",
                          msg: controller.text,
                          type: SCRoomMsgType.text,
                          user: AccountStorage().getCurrentUser()?.userProfile,
                        ),
                      );
                    }
                  }
                },
                onChanged: (s) {
                  setState(() {
                    showSend = controller.text.isNotEmpty;
                  });
                },
                decoration: InputDecoration(
                  hintText: SCAppLocalizations.of(context)!.pleaseChatFfriendly,
                  fillColor: Colors.transparent,
                  hintStyle: TextStyle(color: Colors.white60, fontSize: sp(14)),
                  contentPadding: const EdgeInsets.symmetric(horizontal: 15),
                  counterText: '',
                  isDense: true,
                  filled: false,
                  focusColor: Colors.transparent,
                  hoverColor: Colors.transparent,
                  border: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  disabledBorder: InputBorder.none,
                  errorBorder: InputBorder.none,
                  focusedErrorBorder: InputBorder.none,
                ),
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(14),
                  color: Colors.white,
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
                if (controller.text.isNotEmpty) {
                  var currenRoom =
                      Provider.of<RtcProvider>(
                        context,
                        listen: false,
                      ).currenRoom;
                  if (currenRoom != null) {
                    Provider.of<RtmProvider>(context, listen: false).dispatchMessage(
                      Msg(
                        groupId:
                            currenRoom.roomProfile?.roomProfile?.roomAccount ??
                            "",
                        role:
                            Provider.of<RtcProvider>(
                              context,
                              listen: false,
                            ).currenRoom?.entrants?.roles ??
                            "",
                        msg: controller.text,
                        type: SCRoomMsgType.text,
                        user: AccountStorage().getCurrentUser()?.userProfile,
                      ),
                      addLocal: true,
                    );
                  }
                }
              },
              child: Container(
                padding: EdgeInsets.all(4.w),
                width: 30.w,
                height: 30.w,
                child: Image.asset("sc_images/room/sc_icon_room_message_send.png"),
              ),
            ),
            SizedBox(width: 3.w),
          ],
        ),
      ),
    );
  }
}

class PopRoute extends PopupRoute {
  final Duration _duration = Duration(milliseconds: 350);
  Widget child;

  PopRoute({required this.child});

  @override
  Color? get barrierColor => null;

  @override
  bool get barrierDismissible => true;

  @override
  String? get barrierLabel => null;

  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    return child;
  }

  @override
  Duration get transitionDuration => _duration;
}
