import 'package:PiliPlus/common/widgets/flutter/refresh_indicator.dart';
import 'package:PiliPlus/common/widgets/image/network_img_layer.dart';
import 'package:PiliPlus/common/widgets/view_safe_area.dart';
import 'package:PiliPlus/grpc/bilibili/main/community/reply/v1.pb.dart'
    show ReplyInfo;
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/common/image_type.dart';
import 'package:PiliPlus/models_new/match/match_info/contest.dart';
import 'package:PiliPlus/models_new/match/match_info/team.dart';
import 'package:PiliPlus/pages/common/dyn/common_dyn_page.dart';
import 'package:PiliPlus/pages/common/fab_mixin.dart'
    show NoBottomPaddingFabLocation;
import 'package:PiliPlus/pages/match_info/controller.dart';
import 'package:PiliPlus/pages/video/reply_reply/view.dart';
import 'package:PiliPlus/utils/date_utils.dart';
import 'package:PiliPlus/utils/extension/get_ext.dart';
import 'package:PiliPlus/utils/extension/widget_ext.dart';
import 'package:PiliPlus/utils/page_utils.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class MatchInfoPage extends StatefulWidget {
  const MatchInfoPage({super.key});

  @override
  State<MatchInfoPage> createState() => _MatchInfoPageState();
}

class _MatchInfoPageState extends CommonDynPageState<MatchInfoPage> {
  @override
  final MatchInfoController controller = Get.putOrFind(
    MatchInfoController.new,
    tag: Get.parameters['cid']!,
  );

  @override
  dynamic get arguments => null;

  @override
  Widget build(BuildContext context) {
    final child = Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(title: const Text('比赛详情')),
      body: ViewSafeArea(
        child: refreshIndicator(
          onRefresh: controller.onRefresh,
          child: CustomScrollView(
            physics: const AlwaysScrollableScrollPhysics(),
            slivers: [
              Obx(() => _buildInfo(controller.infoState.value)),
              buildReplyHeader(),
              Obx(() => replyList(controller.loadingState.value)),
            ],
          ),
        ),
      ).constraintWidth(),
      floatingActionButtonLocation: const NoBottomPaddingFabLocation(),
      floatingActionButton: SlideTransition(
        position: fabAnimation,
        child: fabButton,
      ),
    );
    return fabAnimWrapper(child);
  }

  Widget _buildInfo(LoadingState<MatchContest?> infoState) {
    if (infoState case Success(:final response?)) {
      try {
        Widget teamInfo(MatchTeam team) {
          return Column(
            spacing: 5,
            mainAxisSize: MainAxisSize.min,
            children: [
              NetworkImgLayer(
                width: 50,
                height: 50,
                src: 'https://i1.hdslb.com${team.logo}',
                type: ImageType.emote,
              ),
              Text(team.title!),
            ],
          );
        }

        return SliverToBoxAdapter(
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 10),
            child: Column(
              spacing: 12,
              mainAxisSize: MainAxisSize.min,
              children: [
                Center(
                  child: Text(
                    '${response.season?.title ?? ''}  ${response.gameStage ?? ''}',
                  ),
                ),
                Row(
                  spacing: 20,
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    if (response.homeId != 0)
                      Expanded(
                        child: Align(
                          alignment: const Alignment(0.8, 1),
                          child: teamInfo(response.homeTeam!),
                        ),
                      ),
                    Column(
                      spacing: 10,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        if (response.homeId != 0)
                          Text(
                            response.contestStatus == 1
                                ? 'VS'
                                : '${response.homeScore} : ${response.awayScore}',
                            style: const TextStyle(
                              fontSize: 25,
                              fontWeight: FontWeight.bold,
                            ),
                          )
                        else if (response.season?.logo != null)
                          NetworkImgLayer(
                            width: 50,
                            height: 50,
                            src: 'https://i1.hdslb.com${response.season!.logo}',
                            type: ImageType.emote,
                          ),
                        if (response.contestStatus == 2)
                          FilledButton.tonal(
                            style: FilledButton.styleFrom(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 12,
                              ),
                              shape: const RoundedRectangleBorder(
                                borderRadius: BorderRadius.all(
                                  Radius.circular(6),
                                ),
                              ),
                              visualDensity: VisualDensity.compact,
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            ),
                            onPressed: () =>
                                PageUtils.toLiveRoom(response.liveRoom),
                            child: const Text('看直播'),
                          )
                        else if (response.contestStatus == 3)
                          Text(
                            '${DateFormatUtils.dateFormat(response.stime)}${response.contestStatus == 3 ? ' 已结束' : ''}',
                            style: TextStyle(
                              color: theme.colorScheme.outline,
                            ),
                          )
                        else if (response.contestStatus == 1)
                          Text(
                            DateFormatUtils.format(
                              response.stime,
                              format: DateFormat('yy-MM-dd HH:mm'),
                            ),
                            style: TextStyle(
                              color: theme.colorScheme.outline,
                            ),
                          ),
                      ],
                    ),
                    if (response.awayId != 0)
                      Expanded(
                        child: Align(
                          alignment: const Alignment(-0.8, -1),
                          child: teamInfo(response.awayTeam!),
                        ),
                      ),
                  ],
                ),
              ],
            ),
          ),
        );
      } catch (_) {
        return const SliverToBoxAdapter();
      }
    }
    return const SliverToBoxAdapter();
  }

  @override
  void replyReply(BuildContext context, ReplyInfo replyItem, int? id) {
    EasyThrottle.throttle('replyReply', const Duration(milliseconds: 500), () {
      int oid = replyItem.oid.toInt();
      int rpid = replyItem.id.toInt();
      Get.to(
        Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: const Text('评论详情'),
            shape: Border(
              bottom: BorderSide(
                color: theme.colorScheme.outline.withValues(alpha: 0.1),
              ),
            ),
          ),
          body: ViewSafeArea(
            child: VideoReplyReplyPanel(
              enableSlide: false,
              id: id,
              oid: oid,
              rpid: rpid,
              isVideoDetail: false,
              replyType: controller.replyType,
              firstFloor: replyItem,
            ),
          ).constraintWidth(),
        ),
      );
    });
  }
}
