import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import '../resource/colors.gen.dart';
import '../resource/string.gen.dart';

void showAddAgendaDialog(
    BuildContext context,
    TextEditingController agendaContentController,
    TextEditingController agendaNameController,
    {List<String>? list,
    void Function()? callback}) {
  final etContentTxt = agendaContentController.text.obs;
  final etNameTxt = agendaNameController.text.obs;
  agendaContentController.addListener(() {
    etContentTxt.value = agendaContentController.text;
  });
  agendaNameController.addListener(() {
    etNameTxt.value = agendaNameController.text;
  });

  showModalBottomSheet(
    context: context,
    enableDrag: false,
    isScrollControlled: true,
    backgroundColor: ColorName.transparent,
    builder: (BuildContext context) {
      return Padding(
        padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12.w),
                topRight: Radius.circular(12.w)),
            color: "#FFFFFF".toColor(),
          ),
          child: IntrinsicHeight(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 18.h),
                Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        Get.back();
                      },
                      child: Container(
                        color: Colors.transparent,
                        margin: EdgeInsets.only(left: 16.w),
                        padding: EdgeInsets.symmetric(horizontal: 8.w),
                        child: Text(StringName.cancel.tr,
                            style: TextStyle(
                                color: ColorName.secondaryTextColor,
                                fontSize: 14.sp)),
                      ),
                    ),
                    const Spacer(),
                    Text(StringName.talkAddAgendaProject.tr,
                        style: TextStyle(
                            color: ColorName.primaryTextColor,
                            fontSize: 15.sp)),
                    const Spacer(),
                    GestureDetector(
                      onTap: () {
                        callback?.call();
                      },
                      child: Container(
                        color: Colors.transparent,
                        margin: EdgeInsets.only(right: 16.w),
                        padding: EdgeInsets.symmetric(horizontal: 8.w),
                        child: Obx(() {
                          return Text(StringName.done.tr,
                              style: TextStyle(
                                  color: etContentTxt.isNotEmpty &&
                                          etNameTxt.isNotEmpty
                                      ? ColorName.colorPrimary
                                      : ColorName.tertiaryTextColor,
                                  fontSize: 15.sp));
                        }),
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 24.h),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 16.w),
                  decoration: BoxDecoration(
                    color: "#F6F5F8".toColor(),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  height: 120.h,
                  child: TextField(
                    maxLines: null,
                    maxLength: 200,
                    cursorColor: ColorName.colorPrimary,
                    style: TextStyle(
                        fontSize: 15.sp, color: ColorName.primaryTextColor),
                    decoration: InputDecoration(
                      counterText: '',
                      border: InputBorder.none,
                      fillColor: Colors.transparent,
                      hintText: StringName.talkAddAgendaContentHint.tr,
                      hintStyle: TextStyle(
                          fontSize: 15.sp, color: ColorName.tertiaryTextColor),
                      contentPadding: const EdgeInsets.all(12).w,
                    ),
                    controller: agendaContentController,
                  ),
                ),
                SizedBox(height: 16.h),
                Padding(
                  padding: EdgeInsets.only(left: 16.w),
                  child: Text(StringName.talkAddAgendaNameTitle.tr,
                      style: TextStyle(
                          fontSize: 14.sp,
                          color: ColorName.secondaryTextColor)),
                ),
                SizedBox(height: 8.h),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 16.w),
                  decoration: BoxDecoration(
                    color: "#F6F5F8".toColor(),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  height: 44.h,
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          maxLines: 1,
                          maxLength: 15,
                          cursorColor: ColorName.colorPrimary,
                          style: TextStyle(
                              fontSize: 15.sp,
                              color: ColorName.primaryTextColor),
                          decoration: InputDecoration(
                            counterText: '',
                            border: InputBorder.none,
                            fillColor: Colors.transparent,
                            hintText: StringName.talkAddAgendaNameHint.tr,
                            hintStyle: TextStyle(
                                fontSize: 15.sp,
                                color: ColorName.tertiaryTextColor),
                            contentPadding: const EdgeInsets.all(12).w,
                          ),
                          controller: agendaNameController,
                        ),
                      ),
                      Obx(() {
                        return Visibility(
                          visible: etNameTxt.isNotEmpty,
                          child: GestureDetector(
                            onTap: () {
                              agendaNameController.clear();
                            },
                            child: Container(
                                margin: EdgeInsets.only(right: 12.w),
                                width: 20.w,
                                height: 20.w,
                                child:
                                    Assets.images.iconRenameClearTxt.image()),
                          ),
                        );
                      })
                    ],
                  ),
                ),
                SizedBox(height: 12.h),
                _buildAgendaList(list, itemClick: (item) {
                  agendaNameController.text = item;
                }),
                SizedBox(height: 16.h),
              ],
            ),
          ),
        ),
      );
    },
  );
}

Widget _buildAgendaList(List<String>? list,
    {void Function(String item)? itemClick}) {
  if (list == null || list.isEmpty) {
    return Container();
  }
  return SizedBox(
    height: 32.h,
    child: ListView(
        padding: EdgeInsets.only(left: 16.w),
        scrollDirection: Axis.horizontal,
        children: [
          for (var item in list)
            GestureDetector(
              onTap: () {
                itemClick?.call(item);
              },
              child: Container(
                margin: EdgeInsets.only(right: 12.w),
                decoration: BoxDecoration(
                  color: "#F6F5F8".toColor(),
                  borderRadius: BorderRadius.circular(16),
                ),
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                height: double.infinity,
                child: Row(
                  children: [
                    SizedBox(
                        width: 16.w,
                        height: 16.w,
                        child: Assets.images.iconTalkUser.image()),
                    SizedBox(width: 2.w),
                    Text(
                      item,
                      style: TextStyle(
                          fontSize: 14.sp, color: ColorName.primaryTextColor),
                    )
                  ],
                ),
              ),
            ),
        ]),
  );
}
