import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../data/bean/talks.dart';
import '../../../data/consts/Constants.dart';
import '../../../data/repositories/talk_repository.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../common_view.dart';
import 'controller.dart';

class HomeTalkView extends BasePage<HomeTalkController> {
  const HomeTalkView({super.key});

  @override
  bool immersive() {
    return true;
  }

  @override
  Color backgroundColor() {
    return '#F6F5F8'.color;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Obx(() {
      return SmartRefresher(
          controller: controller.refreshController,
          enablePullUp: false,
          onRefresh: controller.onRefresh,
          enablePullDown: true,
          child: ListView(
            padding: EdgeInsets.only(left: 12.w, top: 4.w, right: 12.w),
            children: [
              for (var value in controller.talkList.length >
                      TalkRepository.homeMaxShowCount
                  ? controller.talkList
                      .sublist(0, TalkRepository.homeMaxShowCount)
                  : controller.talkList)
                _buildTalkView(value),
              _buildSeeMoreView()
            ],
          ));
    });
  }

  Widget _buildSeeMoreView() {
    return Obx(() {
      if (controller.talkList.isNotEmpty) {
        return Container(
          alignment: Alignment.center,
          padding: EdgeInsets.only(
              top: 16.w, bottom: 100.w + Constants.bottomBarHeight),
          child: _buildMoreView(),
        );
      } else {
        return homeEmptyView(StringName.homeTalkNoData.tr, _buildMoreView());
      }
    });
  }

  Widget _buildMoreView() {
    return RichText(
      text: TextSpan(
        text: StringName.homeTalkSeeMoreTxt.tr,
        style: TextStyle(color: ColorName.secondaryTextColor, fontSize: 12.sp),
        children: <TextSpan>[
          TextSpan(
              text: StringName.homeTalkAll.tr,
              style: TextStyle(color: ColorName.colorPrimary, fontSize: 12.sp),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  controller.onGoTalkSeeAll();
                }),
        ],
      ),
    );
  }

  Widget _buildTalkView(TalkBean item) {
    return _buildTalkItem(item, onLongPressStart: (details) {
      controller.onLongPressTalkItem(item, details);
    }, onItemClick: () {
      controller.onTalkItemClick(item);
    });
  }

  Widget _buildTalkItem(TalkBean item,
      {VoidCallback? onItemClick,
      GestureLongPressStartCallback? onLongPressStart}) {
    return GestureDetector(
      onTap: onItemClick,
      onLongPressStart: onLongPressStart,
      child: Container(
          margin: EdgeInsets.only(bottom: 8.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(12.w)),
          ),
          child: Stack(
            children: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 16.w),
                    Text(
                      item.title.value.orEmpty,
                      style: TextStyle(
                          fontSize: 15.sp,
                          color: ColorName.primaryTextColor,
                          fontWeight: FontWeight.bold),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    SizedBox(height: 4.w),
                    RichText(
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      text: TextSpan(
                        children: [
                          WidgetSpan(
                            alignment: PlaceholderAlignment.middle,
                            child: Container(
                              decoration: BoxDecoration(
                                  gradient: RadialGradient(
                                      center: Alignment.centerRight,
                                      colors: [
                                        "#E7EEFF".toColor(),
                                        "#F4E8FF".toColor()
                                      ],
                                      radius: 2),
                                  borderRadius: BorderRadius.circular(4.w)),
                              padding: EdgeInsets.symmetric(
                                  horizontal: 5.w, vertical: 2.w),
                              margin: EdgeInsets.only(right: 4.w),
                              child: ShaderMask(
                                shaderCallback: (bounds) {
                                  return LinearGradient(
                                    colors: ['#9075FF'.color, '#4366FF'.color],
                                  ).createShader(bounds);
                                },
                                blendMode: BlendMode.srcIn,
                                child: Text(
                                  StringName.talkSummary.tr,
                                  style: TextStyle(fontSize: 11.sp, height: 1),
                                ),
                              ),
                            ),
                          ),
                          TextSpan(
                            text: item.summary.value.orEmpty,
                            style: TextStyle(
                                fontSize: 12.sp, color: '#969699'.color),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: 6.w),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Assets.images.iconTalkHomeSmallRecord
                            .image(width: 14.w, height: 14.w),
                        SizedBox(width: 2.w),
                        Text(item.duration.toFormattedDuration(),
                            style: TextStyle(
                                fontSize: 11.sp, color: '#969699'.color)),
                        SizedBox(width: 2.w),
                        Text('·',
                            style: TextStyle(
                                fontSize: 11.sp, color: '#969699'.color)),
                        SizedBox(width: 2.w),
                        Text(item.createTime.orEmpty,
                            style: TextStyle(
                                fontSize: 11.sp, color: '#969699'.color)),
                      ],
                    ),
                    SizedBox(height: 15.w)
                  ],
                ),
              ),
              Visibility(
                visible: item.isExample.isTrue,
                child: Align(
                  alignment: Alignment.topRight,
                  child: Container(
                    padding:
                        EdgeInsets.symmetric(vertical: 2.w, horizontal: 10.w),
                    decoration: BoxDecoration(
                        color: "#F0F0FA".toColor(),
                        borderRadius: const BorderRadius.only(
                            topRight: Radius.circular(12),
                            bottomLeft: Radius.circular(12))),
                    child: Text(StringName.homeTalkExample.tr,
                        style: TextStyle(
                            height: 1,
                            fontSize: 12.sp,
                            color: '#A7ACBF'.color)),
                  ),
                ),
              )
            ],
          )),
    );
  }
}
