import 'dart:ui';

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/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';

import '../resource/assets.gen.dart';

void showTalkPopup(Offset offset, Alignment alignment,
    {VoidCallback? onRename, VoidCallback? onDelete}) {
  SmartDialog.showAttach(
    targetContext: null,
    targetBuilder: (_, __) => offset,
    animationType: SmartAnimationType.fade,
    clickMaskDismiss: true,
    alignment: alignment,
    maskColor: Colors.transparent,
    builder: (_) {
      return Container(
        width: 128.w,
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: '#D8D8D8'.toColor(), width: 1), // 边框
          borderRadius: BorderRadius.circular(8), // 圆角
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1), // 阴影颜色
              spreadRadius: 2, // 阴影扩散半径
              blurRadius: 6, // 阴影模糊半径
              offset: const Offset(0, 3), // 阴影偏移量
            ),
          ],
        ),
        child: Column(
          children: [
            _createNormalItem(StringName.talkRename.tr, onItemClick: () {
              SmartDialog.dismiss();
              onRename?.call();
            }),
            Divider(color: "#F6F6F6".toColor(), height: 1),
            _buildDeleteItem(onDelete),
          ],
        ),
      );
    },
  );
}

GestureDetector _buildDeleteItem(VoidCallback? onDelete) {
  return GestureDetector(
    onTap: () {
      SmartDialog.dismiss();
      onDelete?.call();
    },
    child: Container(
      color: Colors.transparent,
      padding: EdgeInsets.symmetric(horizontal: _itemPadding),
      height: _itemHeight,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            StringName.talkDelete.tr,
            style: TextStyle(color: '#F5574E'.toColor(), fontSize: 14.sp),
          ),
          const Spacer(),
          SizedBox(
              width: 20.w,
              height: 20.w,
              child: Assets.images.iconTalkDelete.image())
        ],
      ),
    ),
  );
}

Widget _createNormalItem(String title, {VoidCallback? onItemClick}) {
  return GestureDetector(
    onTap: onItemClick,
    child: Container(
      color: Colors.transparent,
      padding: EdgeInsets.symmetric(horizontal: _itemPadding),
      height: _itemHeight,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          StringName.talkRename.tr,
          style: TextStyle(
            fontSize: 14.sp,
            color: ColorName.primaryTextColor,
          ),
        ),
      ),
    ),
  );
}

final _itemHeight = 52.h;
final _itemPadding = 14.w;
