import 'package:chat/controllers/moment_controller.dart';
import 'package:chat/models/moment/moment_model.dart';
import 'package:chat/views/moments/index/widgets/moment_header.dart';
import 'package:chat/views/moments/index/widgets/moment_list_item.dart';
import 'package:chat/views/moments/index/widgets/moment_list_reply.dart';
import 'package:chat/widgets/custom_easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

class MomentsPage extends GetView<MomentController> {
  const MomentsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewPadding.bottom,
        ),
        child: EasyRefresh.custom(
          scrollController: controller.scrollController,
          controller: controller.refreshController,
          header: LinkHeader(
            controller.headerNotifier,
            extent: 70.0,
            triggerDistance: 70.0,
            completeDuration: const Duration(milliseconds: 500),
          ),
          footer: CustomEasyRefresh.footer,
          onRefresh: () => controller.refreshList(),
          onLoad: () => controller.loadMoreList(),
          slivers: [
            MomentHeader(
              linkNotifier: controller.headerNotifier,
              onTitleDoubleTap: () {
                controller.scrollController.animateTo(
                  0,
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.fastOutSlowIn,
                );
              },
            ),
            Obx(() {
              final momentList = controller.momentData.value?.data ?? [];
              if (momentList.isEmpty) {
                return SliverFillRemaining(
                  child: CustomEasyRefresh.empty(text: '暂无动态内容'),
                );
              }
              return SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) {
                    if (index.isEven) {
                      final i = (index / 2).round();
                      return momentItemWidget(i, momentList[i]);
                    } else {
                      return const Divider();
                    }
                  },
                  childCount: (momentList.length * 2) - 1,
                ),
              );
            })
          ],
        ),
      ),
    );
  }

  Widget momentItemWidget(int index, MomentItemModel item) {
    return Column(children: [
      GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => MomentController.to.pushToDetail(index),
        child: MomentListItem(item: item),
      ),
      Padding(
        padding: const EdgeInsets.only(left: 52),
        child: MomentListItemReplay(
          index: index,
          item: item,
          maxDisplayCount: 3,
          maxLine: 2,
          reply: (value) =>
              MomentController.to.showReplyBar(item.dynamicId!, value),
        ),
      ),
    ]);
  }
}
