import 'package:electronic_assistant/data/bean/agenda_list_all_bean.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../data/bean/agenda.dart';
import '../../data/bean/template_bean.dart';
import '../../popup/talk_add_template_popup.dart';
import '../../resource/assets.gen.dart';
import '../../resource/colors.gen.dart';
import '../../resource/string.gen.dart';
import '../../utils/gradient_rounded_linear_progress_bar.dart';

Widget getAddAgendaView(String addTxt, {VoidCallback? onClick}) {
  return GestureDetector(
    onTap: onClick,
    child: Container(
      decoration: BoxDecoration(
          color: "#F6F5F8".toColor(),
          border: Border.all(color: '#EAE5EF'.toColor(), width: 1),
          borderRadius: BorderRadius.circular(8.w)),
      margin: EdgeInsets.only(bottom: 8.h, top: 6.h),
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 11.w),
      child: Row(
        children: [
          SizedBox(
              width: 22.w,
              height: 22.w,
              child: Assets.images.iconTalkAddAdenda.image()),
          SizedBox(width: 8.w),
          Text(
            addTxt,
            style: TextStyle(fontSize: 14.sp, color: ColorName.colorPrimary),
          )
        ],
      ),
    ),
  );
}

//
Widget getTalkAnalyseView(String analyseTxt, double progress) {
  return SizedBox(
    width: double.infinity,
    child: Column(
      children: [
        SizedBox(height: 138.h),
        SizedBox(
            width: 100.w,
            height: 100.w,
            child: Assets.anim.talkAnalyse.image()),
        SizedBox(height: 24.h),
        GradientRoundedLinearProgressBar(
          height: 12.w,
          width: 220.w,
          borderRadius: 10,
          backgroundColor: '#F6F5F8'.toColor(),
          progress: progress / 100,
          gradientColors: ["#9075FF".toColor(), "#4366FF".toColor()],
        ),
        SizedBox(height: 10.h),
        Text('$analyseTxt  $progress%',
            style:
                TextStyle(fontSize: 12.sp, color: ColorName.secondaryTextColor))
      ],
    ),
  );
}

Widget getTalkUploadingView(RxDouble progress) {
  return SizedBox(
    width: double.infinity,
    child: Column(
      children: [
        SizedBox(height: 138.h),
        SizedBox(
            width: 100.w,
            height: 100.w,
            child: Assets.anim.talkAnalyse.image()),
        SizedBox(height: 3.h),
        Text('谈话上传中',
            style:
                TextStyle(fontSize: 15.sp, color: ColorName.primaryTextColor)),
        SizedBox(height: 3.h),
        Obx(() {
          return Text(
              '谈话已上传${(progress.value * 100).toFormattedString(1)}%，请稍后查看...',
              style: TextStyle(
                  fontSize: 12.sp, color: ColorName.secondaryTextColor));
        })
      ],
    ),
  );
}

Widget getTalkLoadingView() {
  return SizedBox(
    width: double.infinity,
    child: Column(
      children: [
        SizedBox(height: 138.h),
        SizedBox(
            width: 100.w,
            height: 100.w,
            child: Assets.anim.talkAnalyse.image()),
        SizedBox(height: 12.h),
        Text(StringName.talkAnalyzing.tr,
            style:
                TextStyle(fontSize: 14.sp, color: ColorName.secondaryTextColor))
      ],
    ),
  );
}

Widget getTalkFailView() {
  return SizedBox(
    width: double.infinity,
    child: Column(
      children: [
        SizedBox(height: 111.h),
        SizedBox(
            width: 100.w,
            height: 100.w,
            child: Assets.images.iconTalkAnalyseFail.image()),
        SizedBox(height: 4.h),
        Text(
          StringName.talkAnalyseFail.tr,
          style: TextStyle(
            fontSize: 15.sp,
            color: ColorName.primaryTextColor,
            fontWeight: FontWeight.w500,
          ),
        ),
        SizedBox(height: 2.h),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(StringName.talkAnalyseSummaryFail.tr,
                style: TextStyle(
                    fontSize: 12.sp, color: ColorName.secondaryTextColor)),
            GestureDetector(
              onTap: () {
                Get.toNamed(RoutePath.complaintOpinion, arguments: "意见建议");
              },
              child: Text(
                StringName.talkAnalyseFailFeedback.tr,
                style: TextStyle(
                  color: "#102FDE".toColor(),
                  fontSize: 12.sp,
                  decoration: TextDecoration.underline,
                  decorationColor: "#102FDE".toColor(),
                ),
              ),
            )
          ],
        ),
      ],
    ),
  );
}

typedef TodoItemBuilder = List<Widget> Function(List<Agenda>? list);

Widget getTodoItemView(AgendaListAllBean agenda, TodoItemBuilder todoBuilder) {
  return Container(
    decoration: BoxDecoration(
      color: ColorName.colorPrimary,
      borderRadius: BorderRadius.circular(6),
    ),
    child: Builder(builder: (context) {
      return Theme(
        data: Theme.of(context).copyWith(
          splashFactory: NoSplash.splashFactory,
        ),
        child: Container(
          margin: EdgeInsets.only(left: 4.w),
          decoration: BoxDecoration(
            color: "#F6F5F8".toColor(),
            borderRadius: const BorderRadius.only(
              topRight: Radius.circular(6),
              bottomRight: Radius.circular(6),
            ),
          ),
          child: ExpansionTile(
            initiallyExpanded: true,
            onExpansionChanged: (value) {
              agenda.isExpanded.value = value;
            },
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(4.0),
              side: const BorderSide(color: Colors.transparent),
            ),
            collapsedShape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(4.0),
              side: const BorderSide(color: Colors.transparent),
            ),
            minTileHeight: 46.h,
            showTrailingIcon: false,
            childrenPadding: EdgeInsets.zero,
            tilePadding: EdgeInsets.zero,
            title: Row(
              children: [
                SizedBox(width: 12.w),
                Text(agenda.name.orEmpty,
                    style: TextStyle(
                        fontSize: 15.sp,
                        color: ColorName.primaryTextColor,
                        fontWeight: FontWeight.bold)),
                const Spacer(),
                Obx(() {
                  return Row(
                    children: [
                      Text(
                        '${agenda.list?.length}${StringName.talkTodoItem.tr}',
                        style: TextStyle(
                            fontSize: 15.sp,
                            color: ColorName.secondaryTextColor),
                      ),
                      SizedBox(width: 3.w),
                      SizedBox(
                          width: 16.w,
                          height: 16.w,
                          child: agenda.isExpanded.value
                              ? Assets.images.iconTalkExpand.image()
                              : Assets.images.iconTalkCollapse.image()),
                    ],
                  );
                }),
                SizedBox(width: 12.w),
              ],
            ),
            children: todoBuilder(agenda.list),
          ),
        ),
      );
    }),
  );
}

typedef TodoItemClick = void Function(Agenda? agenda);

List<Widget> getTalkAgendaSettingList(List<Agenda>? list,
    {TodoItemClick? itemClick}) {
  return list?.map((agenda) {
        return Padding(
          padding:
              EdgeInsets.only(left: 12.w, right: 12.w, top: 2.h, bottom: 10.h),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: 8.w,
                        height: 8.w,
                        margin: EdgeInsets.only(top: 7.h),
                        decoration: BoxDecoration(
                          color: ColorName.colorPrimary.withOpacity(0.5),
                          shape: BoxShape.circle,
                        ),
                      ),
                      SizedBox(width: 4.w),
                      Expanded(
                        child: Text(agenda.content.orEmpty,
                            style: TextStyle(
                                fontSize: 14.sp,
                                color: ColorName.primaryTextColor)),
                      ),
                    ]),
              ),
              SizedBox(width: 20.w),
              GestureDetector(
                onTap: () {
                  itemClick?.call(agenda);
                },
                child: Obx(() {
                  return Container(
                    decoration: BoxDecoration(
                      color: agenda.todo.value
                          ? "#E7E9F6".toColor()
                          : ColorName.colorPrimary,
                      borderRadius: BorderRadius.circular(6),
                    ),
                    padding:
                        EdgeInsets.symmetric(horizontal: 9.w, vertical: 5.w),
                    child: Text(
                      agenda.todo.value
                          ? StringName.talkTodoCancelMine.tr
                          : StringName.talkTodoSetMine.tr,
                      style: TextStyle(
                          fontSize: 13.sp,
                          color: agenda.todo.value
                              ? ColorName.colorPrimary
                              : Colors.white),
                    ),
                  );
                }),
              )
            ],
          ),
        );
      }).toList() ??
      [];
}

List<Widget> getTalkAgendaEditModelList(List<Agenda>? list,
    {void Function(Agenda agenda)? removeCallback}) {
  return list?.map((agenda) {
        return GestureDetector(
          onTap: () {
            removeCallback?.call(agenda);
          },
          child: Padding(
            padding: EdgeInsets.only(
                left: 12.w, right: 12.w, top: 2.h, bottom: 10.h),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                    width: 18.w,
                    height: 18.w,
                    child: Assets.images.iconTalkRemoveAgenda.image()),
                SizedBox(width: 4.w),
                Expanded(
                  child: Text(agenda.content.orEmpty,
                      style: TextStyle(
                          fontSize: 14.sp, color: ColorName.primaryTextColor)),
                ),
              ],
            ),
          ),
        );
      }).toList() ??
      [];
}

List<Widget> getTalkAgendaNormalList(List<Agenda>? list) {
  return list?.map((agenda) {
        return Padding(
          padding:
              EdgeInsets.only(left: 12.w, right: 12.w, top: 2.h, bottom: 10.h),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                width: 8.w,
                height: 8.w,
                margin: EdgeInsets.only(top: 7.h),
                decoration: BoxDecoration(
                  color: ColorName.colorPrimary.withOpacity(0.5),
                  shape: BoxShape.circle,
                ),
              ),
              SizedBox(width: 4.w),
              Expanded(
                child: Text(agenda.content.orEmpty,
                    style: TextStyle(
                        fontSize: 14.sp, color: ColorName.primaryTextColor)),
              ),
            ],
          ),
        );
      }).toList() ??
      [];
}

Widget buildTemplateView(List<TemplateBean>? templateList, int? id,
    {Widget? addTemplateView, Function(TemplateBean)? onTap}) {
  Widget targetWidget = SizedBox(
    height: 32.h,
    child: ListView(
      scrollDirection: Axis.horizontal,
      shrinkWrap: true,
      children: [
        SizedBox(width: 12.w),
        for (var item in templateList ?? <TemplateBean>[])
          GestureDetector(
            onTap: () {
              onTap?.call(item);
            },
            child: Container(
              height: double.infinity,
              decoration: item.id == id
                  ? BoxDecoration(
                      color: ColorName.colorPrimary,
                      borderRadius: BorderRadius.circular(6.w),
                    )
                  : BoxDecoration(
                      borderRadius: BorderRadius.circular(6.w),
                      color: "#FAF9FB".toColor(),
                      border:
                          Border.all(color: '#F2EFF5'.toColor(), width: 1.w),
                    ),
              margin: EdgeInsets.only(right: 8.w),
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              child: Center(
                child: Text(item.name ?? '',
                    style: TextStyle(
                        height: 1,
                        fontSize: 13.sp,
                        color: item.id == id
                            ? ColorName.white
                            : ColorName.secondaryTextColor)),
              ),
            ),
          ),
        addTemplateView ?? const SizedBox.shrink()
      ],
    ),
  );
  return targetWidget;
}

Widget buildAddTemplateView({
  required key,
  VoidCallback? addCallback,
}) {
  return GestureDetector(
    key: key,
    onTap: addCallback,
    child: Container(
      margin: EdgeInsets.only(right: 8.w),
      height: double.infinity,
      decoration: BoxDecoration(
        color: "#FAF9FB".toColor(),
        borderRadius: BorderRadius.circular(6.w),
        border: Border.all(color: '#F2EFF5'.toColor(), width: 1.w),
      ),
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Center(
        child: IntrinsicWidth(
          child: Row(
            children: [
              Assets.images.iconTalkAddTemplate
                  .image(width: 16.w, height: 16.w),
              Text(StringName.talkAddTemplate.tr,
                  style: TextStyle(
                      height: 1,
                      fontSize: 13.sp,
                      color: ColorName.tertiaryTextColor))
            ],
          ),
        ),
      ),
    ),
  );
}
