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:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/utils/toast_util.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';

typedef RenameBuilder = void Function(String content);

void reNameDialog(String title, String? content,
    {String? hintTxt, int? maxLength, RenameBuilder? returnBuilder}) {
  final controller = TextEditingController();
  controller.text = content ?? "";
  final contentObs = controller.text.obs;
  controller.addListener(() {
    contentObs.value = controller.text;
  });
  SmartDialog.show(
      builder: (_) {
        return IntrinsicHeight(
          child: Container(
            padding: EdgeInsets.all(16.w),
            width: 336.w,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
            ),
            child: Column(
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 15.sp,
                          color: ColorName.primaryTextColor),
                    ),
                    const Spacer(),
                    GestureDetector(
                      onTap: () {
                        SmartDialog.dismiss();
                      },
                      child: SizedBox(
                        width: 28.w,
                        height: 28.w,
                        child: Assets.images.iconRenameClose.image(),
                      ),
                    )
                  ],
                ),
                SizedBox(height: 24.h),
                Container(
                  decoration: BoxDecoration(
                    color: "#F0F0F0".toColor(),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 14.w),
                  height: 51.h,
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          cursorColor: ColorName.primaryTextColor,
                          maxLength: maxLength,
                          style: TextStyle(
                              fontSize: 16.sp,
                              color: ColorName.primaryTextColor),
                          controller: controller,
                          maxLines: 1,
                          decoration: InputDecoration(
                            counterText: '',
                            hintText: hintTxt,
                            hintStyle: TextStyle(
                                fontSize: 16.sp,
                                color: ColorName.tertiaryTextColor),
                            border: InputBorder.none,
                            fillColor: Colors.transparent,
                          ),
                        ),
                      ),
                      Obx(() {
                        return Visibility(
                          visible: contentObs.value!.isNotEmpty,
                          child: GestureDetector(
                            onTap: () {
                              controller.clear();
                            },
                            child: SizedBox(
                              width: 20.w,
                              height: 20.w,
                              child: Assets.images.iconRenameClearTxt.image(),
                            ),
                          ),
                        );
                      })
                    ],
                  ),
                ),
                SizedBox(height: 28.h),
                Row(
                  children: [
                    Expanded(
                        child: GestureDetector(
                      onTap: () {
                        SmartDialog.dismiss();
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 12.h),
                        decoration: BoxDecoration(
                          color: '#F0F0F0'.toColor(),
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: Center(
                          child: Text(
                            StringName.cancel.tr,
                            style: TextStyle(
                                fontSize: 16.sp,
                                color: ColorName.secondaryTextColor),
                          ),
                        ),
                      ),
                    )),
                    SizedBox(width: 12.w),
                    Expanded(
                        child: GestureDetector(
                      onTap: () {
                        if (contentObs.value.isEmpty) {
                          ToastUtil.showToast(hintTxt);
                          return;
                        }
                        returnBuilder?.call(contentObs.value);
                        SmartDialog.dismiss();
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 12.h),
                        decoration: BoxDecoration(
                          color: ColorName.colorPrimary,
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: Center(
                          child: Text(
                            StringName.sure.tr,
                            style: TextStyle(
                                fontSize: 16.sp, color: ColorName.white),
                          ),
                        ),
                      ),
                    )),
                  ],
                ),
                SizedBox(height: 8.h),
              ],
            ),
          ),
        );
      },
      clickMaskDismiss: false);
}
