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 '../common_view.dart';
import 'controller.dart';

class SummaryView extends BasePage<SummaryController> {
  const SummaryView({super.key});

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

  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),
          );
        }));
  }

  Widget buildTemplateView() {
    return Container();
  }

  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.homeTalkTodoTitle.tr,
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: ColorName.primaryTextColor,
                  fontSize: 15.sp)),
          SizedBox(height: 12.h),
          buildTodoView()
        ],
      ),
    );
  }

  Widget buildTodoView() {
    return Obx(() {
      return Column(
          children: controller.agendaAllList.map((agenda) {
        return Padding(
            padding: EdgeInsets.only(bottom: 8.h),
            child: getTodoItemView(
                agenda, (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: [
          // buildTemplateView(),
          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) {
        return getTalkLoadingView();
      } else {
        return Container();
      }
    });
  }
}
