import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';
import 'package:markdown/markdown.dart' as md;
import '../../../data/bean/talks.dart';
import '../../browser/view.dart';
import '../../home/view.dart';
import '../common_view.dart';
import 'controller.dart';

class SummaryView extends BasePage<SummaryController> {
  final String? talkId;

  SummaryView(this.talkId, {super.key}) {
    Get.lazyPut(() => SummaryController(talkId), tag: talkId);
  }

  @override
  get controller => Get.find<SummaryController>(tag: talkId);

  @override
  Widget buildBody(BuildContext context) {
    return buildSummaryStatusView();
  }

  @override
  bool immersive() {
    return true;
  }

  Widget buildSummaryView() {
    return Padding(
        padding:
            EdgeInsets.only(left: 12.w, right: 12.w, top: 16.h, bottom: 20.h),
        child: Obx(() {
          return HtmlWidget(
            onTapUrl: (url) {
              BrowserPage.start(url);
              return true;
            },
            md.markdownToHtml(controller.summaryBean.value?.summary.value ?? "",
                inlineSyntaxes: [
                  md.InlineHtmlSyntax(),
                  md.StrikethroughSyntax(),
                  md.EmojiSyntax(),
                  md.ColorSwatchSyntax(),
                  md.AutolinkExtensionSyntax(),
                  md.ImageSyntax()
                ],
                blockSyntaxes: [
                  const md.FencedCodeBlockSyntax(),
                  const md.HeaderWithIdSyntax(),
                  const md.SetextHeaderWithIdSyntax(),
                  const md.UnorderedListWithCheckboxSyntax(),
                  const md.OrderedListWithCheckboxSyntax(),
                  const md.FootnoteDefSyntax(),
                  const md.AlertBlockSyntax(),
                ]),
            textStyle:
                TextStyle(fontSize: 14.sp, color: ColorName.primaryTextColor),
          );
        }));
  }

  buildAllTaskView() {
    return Padding(
      padding:
          EdgeInsets.only(left: 12.w, right: 12.w, top: 20.h, bottom: 16.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(StringName.talkTodoAll.tr,
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: ColorName.primaryTextColor,
                  fontSize: 15.sp)),
          SizedBox(height: 12.h),
          _buildAddTaskView(),
          buildAllTaskList()
        ],
      ),
    );
  }

  Widget buildAllTaskList() {
    return Obx(() {
      if (controller.agendaAllList.isEmpty) {
        if (controller.talkController.isFirstRequestTask) {
          return Container();
        }
        return buildAgendaEmptyView(65.h,
            isVisible: controller.isEditModel == false);
      } else {
        return Column(
            children: controller.agendaAllList.map((item) {
          return Padding(
              padding: EdgeInsets.only(bottom: 8.h),
              child: controller.isEditModel
                  ? getTodoItemView(
                      item,
                      (list) => getTalkAgendaEditModelList(list,
                              removeCallback: (agenda) {
                            controller.removeTalkAgenda(list, agenda);
                          }))
                  : getTodoItemView(
                      item, (list) => getTalkAgendaNormalList(list)));
        }).toList());
      }
    });
  }

  Widget buildSummaryStatusView() {
    return Obx(() {
      if (controller.summaryBean.value?.status.value ==
          TalkStatus.analysisFail) {
        return getTalkFailView();
      } else if (controller.summaryBean.value?.status.value ==
          TalkStatus.analysisSuccess) {
        return ListView(padding: EdgeInsets.only(bottom: 150.h), children: [
          SizedBox(height: 14.h),
          Obx(() {
            return buildTemplateView(
                controller.templateList, controller.templateSelectId,
                onTap: (template) {
              controller.selectTemplate(template);
            },
                addTemplateView: buildAddTemplateView(
                    key: controller.addTemplateKey,
                    addCallback: () {
                      controller.addTemplateClick();
                    }));
          }),
          buildSummaryView(),
          Container(
            height: 6.h,
            color: "#F6F6F6".toColor(),
          ),
          buildAllTaskView(),
        ]);
      } else if (controller.summaryBean.value?.status.value ==
              TalkStatus.analysing ||
          controller.summaryBean.value?.status.value ==
              TalkStatus.waitAnalysis ||
          controller.summaryBean.value?.status.value ==
              TalkStatus.notAnalysis) {
        return getTalkAnalyseView(
            controller.talkController.isUploading.value == true &&
                    controller.talkController.talkBean.value?.status.value ==
                        TalkStatus.notAnalysis
                ? StringName.talkUploadingFileTip.tr
                : controller
                        .talkController.talkBean.value?.progressContent.value ??
                    '',
            controller.talkController.isUploading.value == true &&
                    controller.talkController.talkBean.value?.status.value ==
                        TalkStatus.notAnalysis
                ? controller.talkController.uploadProgress.value
                : controller.talkController.talkBean.value?.progress.value
                        ?.toDouble() ??
                    0.0);
      } else {
        return Container();
      }
    });
  }

  Widget _buildAddTaskView() {
    return Obx(() {
      return Visibility(
          visible: controller.isEditModel,
          child: Builder(builder: (context) {
            return getAddAgendaView(StringName.talkAddAgenda.tr, onClick: () {
              controller.showAddAgendaDialog(context);
            });
          }));
    });
  }
}
