import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/group_controller.dart';
import 'package:chat/controllers/private_controller.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/routes/conversation_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/views/conversation/widgets/message_field.dart';
import 'package:chat/views/conversation/widgets/message_list.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/enum/conversation_type.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';

class ConversationPage extends StatefulWidget {
  const ConversationPage({Key? key}) : super(key: key);

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

class _ConversationPageState extends State<ConversationPage> {
  late final V2TimConversation conversation;
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  final GlobalKey<dynamic> inputextField = GlobalKey();

  @override
  void initState() {
    super.initState();
    conversation = Get.arguments['conversation'];

    /// 标记会话内消息已读
    TimConversationService.to.markAsRead(conversation);
    if (conversation.type == ConversationType.V2TIM_GROUP) {
      GroupController.to.setCurrentGroup(conversation.groupID!);
    } else {
      PrivateController.to.setCurrentFriend(conversation.userID!);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        inputextField.currentState.hideAllPanel();
      },
      child: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: conversation.type == ConversationType.V2TIM_GROUP
              ? GetX<GroupController>(
                  builder: (_) {
                    return Text(
                      _.currentGroup.value.conversation?.showName ?? '',
                    );
                  },
                )
              : GetX<PrivateController>(
                  builder: (_) {
                    return Text(
                      _.currentFriend.value.conversation?.showName ?? '',
                    );
                  },
                ),
          actions: [
            _topRightAction(),
          ],
        ),
        body: Column(
          children: [
            if (conversation.type == ConversationType.V2TIM_C2C)
              _isFriendWidget(),
            Expanded(
              child: MessageList(conversation),
            ),
          ],
        ),
        bottomNavigationBar: MessageField(
          conversation,
          key: inputextField,
        ),
      ),
    );
  }

  Widget _isFriendWidget() {
    return GetX<PrivateController>(
      builder: (_) {
        if (!_.currentFriend.value.isFriend) {
          return Container(
            color: AppColors.warning.withOpacity(0.2),
            width: Get.width,
            padding: const EdgeInsets.all(8),
            child: Row(
              children: [
                const Text(
                  '你们还不是好友，请先',
                  style: TextStyle(
                    color: AppColors.unactive,
                    fontSize: 12,
                  ),
                ),
                const SizedBox(width: 4),
                InkWell(
                  onTap: () {
                    Get.toNamed(
                      ContactRoutes.friendRequestApply,
                      arguments: {
                        'userID': conversation.userID!,
                      },
                    );
                  },
                  child: const Text(
                    '添加好友',
                    style: TextStyle(
                      color: AppColors.primary,
                      fontSize: 12,
                    ),
                  ),
                ),
              ],
            ),
          );
        } else {
          return Container();
        }
      },
    );
  }

  Widget _topRightAction() {
    return IconButton(
      icon: const Icon(Icons.more_horiz),
      onPressed: () {
        conversation.type == ConversationType.V2TIM_GROUP
            ? Get.toNamed(
                ConversationRoutes.infoGroup,
              )
            : Get.toNamed(
                ConversationRoutes.infoPrivate,
              );
      },
    );
  }
}
