import 'package:electronic_assistant/module/home/view.dart';
import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

void mainGuide(GlobalKey topGuide, {VoidCallback? knowCallback}) {
  final topGuideOffset = getWidgetPosition(topGuide);
  const tag = 'mainGuideDialog';
  double padding = 7.5.w;
  SmartDialog.show(
      tag: tag,
      backType: SmartBackType.block,
      clickMaskDismiss: false,
      maskColor: ColorName.black70,
      builder: (_) {
        return SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              Positioned(
                left: 0,
                right: 0,
                top: topGuideOffset.dy - padding,
                child: Column(
                  children: [
                    Container(
                        padding: EdgeInsets.all(padding),
                        margin: EdgeInsets.symmetric(horizontal: 10.w),
                        decoration: BoxDecoration(
                          color: ColorName.white,
                          borderRadius: BorderRadius.circular(14),
                        ),
                        child: getHomeHeadView()),
                    Padding(
                      padding: EdgeInsets.only(
                          left: 17.5.w, right: 17.5.w, top: 4.w),
                      child: Row(children: [
                        Expanded(
                          child: Column(children: [
                            Assets.images.iconMainGuideArrow
                                .image(width: 14.w, height: 27.w),
                            SizedBox(height: 8.w),
                            Text(StringName.guideRecord.tr,
                                style: TextStyle(
                                    fontSize: 14.sp, color: ColorName.white))
                          ]),
                        ),
                        SizedBox(width: 8.w),
                        Expanded(
                          child: Column(children: [
                            Assets.images.iconMainGuideArrow
                                .image(width: 14.w, height: 27.w),
                            SizedBox(height: 8.w),
                            Text(StringName.guideImport.tr,
                                style: TextStyle(
                                    fontSize: 14.sp, color: ColorName.white))
                          ]),
                        ),
                      ]),
                    )
                  ],
                ),
              ),
              Positioned(
                left: 0,
                right: 0,
                bottom: 8.h,
                child: Column(
                  children: [
                    GestureDetector(
                      onTap: () {
                        SmartDialog.dismiss(tag: tag);
                        knowCallback?.call();
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          border:
                              Border.all(color: ColorName.white, width: 2.w),
                          borderRadius: BorderRadius.circular(100.w),
                        ),
                        padding: EdgeInsets.symmetric(
                            horizontal: 28.w, vertical: 11.w),
                        child: Text(
                          StringName.guideAiKnow.tr,
                          style: TextStyle(
                              fontSize: 15.sp, color: ColorName.white),
                        ),
                      ),
                    ),
                    SizedBox(height: 32.h),
                    Container(
                        width: 274.w,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: Assets.images.bgGuideMainAiPopup.provider(),
                            fit: BoxFit.cover,
                          ),
                        ),
                        child: AspectRatio(
                          aspectRatio: 274 / 76,
                          child: Align(
                            alignment: const FractionalOffset(0.6016, 0.361),
                            child: IntrinsicHeight(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    StringName.guideAiSecretary.tr,
                                    style: TextStyle(
                                        fontSize: 16.sp,
                                        fontWeight: FontWeight.bold,
                                        color: ColorName.white),
                                  ),
                                  Text(
                                    StringName.guideAiSecretaryDesc.tr,
                                    style: TextStyle(
                                        fontSize: 12.sp,
                                        color: ColorName.white90),
                                  )
                                ],
                              ),
                            ),
                          ),
                        )),
                    SizedBox(height: 12.h),
                    Assets.images.iconGuideMainAi
                        .image(width: 66.w, height: 66.w),
                  ],
                ),
              )
            ],
          ),
        );
      });
}

Offset getWidgetPosition(GlobalKey key) {
  final context = key.currentContext;
  if (context == null) {
    throw Exception('Context is null');
  }

  final renderObject = context.findRenderObject();
  if (renderObject == null || renderObject is! RenderBox) {
    throw Exception('RenderObject is null or not a RenderBox');
  }

  final offset = renderObject.localToGlobal(Offset.zero);
  return offset;
}
