import 'package:dsbridge_flutter/dsbridge_flutter.dart';
import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/bean/talks.dart';
import 'package:electronic_assistant/module/talk/mindmap/controller.dart';
import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import '../../../resource/string.gen.dart';
import '../common_view.dart';

class MindMapView extends BasePage<MindMapController> {
  final String? talkId;

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

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

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Obx(() {
      if (controller.talkController.talkBean.value?.status.value ==
          TalkStatus.analysisSuccess) {
        return _buildMindView();
      } else if (controller.talkController.talkBean.value?.status.value ==
          TalkStatus.analysisFail) {
        return getTalkFailView();
      } else {
        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);
      }
    });
  }

  Widget _buildMindView() {
    return Stack(
      children: [
        DWebViewWidget(controller: controller.talkController.webViewController),
        Obx(() {
          return Visibility(
            visible: !controller.isShowMindFullScreen.value,
            child: Container(
              margin: EdgeInsets.only(top: 14.h),
              child: buildTemplateView(
                  controller.templateList, controller.templateSelectId,
                  onTap: (bean) {
                controller.selectTemplate(bean);
              }),
            ),
          );
        }),
        GestureDetector(
          onTap: () {
            controller.fullScreenClick();
          },
          child: Obx(() {
            return Visibility(
              visible: !controller.isShowMindFullScreen.value,
              child: Align(
                alignment: const Alignment(0.0, 0.7),
                child: Container(
                  decoration: BoxDecoration(
                      color: ColorName.black60,
                      border: Border.all(color: ColorName.white20, width: 1.w),
                      borderRadius: BorderRadius.circular(24.w)),
                  padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 6.w),
                  child: IntrinsicWidth(
                    child: Row(
                      children: [
                        Assets.images.iconMindFullScreen
                            .image(width: 16.w, height: 16.w),
                        SizedBox(width: 2.w),
                        Text(StringName.talkMindFullScreen.tr,
                            style: TextStyle(
                                fontSize: 14.sp,
                                color: ColorName.white70,
                                height: 1))
                      ],
                    ),
                  ),
                ),
              ),
            );
          }),
        ),
        _buildFullScreenBackView()
      ],
    );
  }

  Widget _buildFullScreenBackView() {
    return Obx(() {
      return Visibility(
        visible: controller.isShowMindFullScreen.value,
        child: GestureDetector(
          onTap: () {
            controller.exitFullScreenClick();
          },
          child: SafeArea(
            child: Container(
              margin: EdgeInsets.only(left: 4.w, top: 8.w),
              child: Assets.images.iconMindFullScreenBack
                  .image(width: 60.w, height: 60.w),
            ),
          ),
        ),
      );
    });
  }
}
