import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:gap/gap.dart';
import 'package:get/get.dart';
import 'package:traderscircuit/Utils/text.dart';

import '../../../Utils/Common/CustomTextFormField.dart';
import '../../../Utils/Common/FilePicker.dart';
import '../../../Utils/Common/commonBotton.dart';
import '../../../Utils/Common/custom_drop_down.dart';
import '../../../controller/contact_us_controller.dart';
import 'ticket_confirmed_bottom_sheet.dart';

class CreateTicketBottomSheet {
  TextEditingController descriptionController = TextEditingController();
  ContactUsController contactUsController = Get.put(ContactUsController());
  bottomSheet(
    BuildContext context,
  ) {
    return showModalBottomSheet(
      useSafeArea: true,
      isScrollControlled: true,
      context: context,
      backgroundColor: const Color(0xFF101111),
      isDismissible: false,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(8.r),
          topRight: Radius.circular(8.r),
        ),
      ),
      builder: (BuildContext context) {
        return Obx(
          () => Container(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 20),
            child: Wrap(
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        GestureDetector(
                            onTap: () {
                              Get.back();
                            },
                            child: SvgPicture.asset(
                                "assets/images/svg/cross_cancel.svg")),
                      ],
                    ),
                    const Gap(16),
                    text18W600("Raise a Ticket"),
                    const Gap(20),
                    const CustomDropDownWidget(
                      header: "Choose your query",
                      listData: [
                        "Account Management",
                        "Technical Support",
                        "Billing and Payments",
                        "Feedback and Suggestions",
                        "Complaints and Disputes",
                        "Subscriptions",
                        "Portfolio",
                        "Content Buytes",
                        "Market Insights"
                      ],
                    ),
                    const Gap(14),
                    Stack(
                      children: [
                        CustomTextFormField1(
                          texttype: TextInputType.multiline,
                          hintText: "Description (min 30 characters)",
                          textEditingController: descriptionController,
                          maxlines: 8,
                          validator: (value) {
                            if (value.isEmpty) {
                              return 'Enter your description';
                            } else if (value.toString().length < 30) {
                              return 'Description should be minimum 30 characters';
                            }
                            return null;
                          },
                          inputFormatters: [
                            LengthLimitingTextInputFormatter(150),
                          ],
                        ),
                        contactUsController.attachmentPathNameList.isEmpty
                            ? const SizedBox()
                            : Positioned(
                                bottom: 8,
                                left: 9,
                                right: 9,
                                child: SizedBox(
                                  width: 1.sw,
                                  height: 37.h,
                                  child: ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                      itemCount: contactUsController
                                          .attachmentPathNameList.length,
                                      itemBuilder: (ctx, index) {
                                        return Container(
                                          width: 210.w,
                                          height: 37.h,
                                          margin:
                                              const EdgeInsets.only(right: 5),
                                          decoration: BoxDecoration(
                                              borderRadius:
                                                  BorderRadius.circular(8),
                                              border: Border.all(
                                                  width: 1,
                                                  color:
                                                      const Color(0xFF3A3A3A))),
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: [
                                              const Gap(6),
                                              SvgPicture.asset(
                                                  "assets/images/svg/attachment_pin.svg"),
                                              const Gap(6),
                                              SizedBox(
                                                  width: 120,
                                                  child: FittedBox(
                                                      child: text12W400(
                                                          contactUsController
                                                                  .attachmentPathNameList[
                                                              index]))),
                                              const Gap(15),
                                              GestureDetector(
                                                  onTap: () {
                                                    contactUsController
                                                        .attachmentPathNameList
                                                        .removeAt(index);
                                                    contactUsController
                                                        .attachmentFileList
                                                        .removeAt(index);
                                                  },
                                                  child: SvgPicture.asset(
                                                    "assets/images/svg/cross_cancel.svg",
                                                    color:
                                                        const Color(0xFF818181),
                                                    width: 8,
                                                    height: 8,
                                                  )),
                                            ],
                                          ),
                                        );
                                      }),
                                ))
                      ],
                    ),
                    contactUsController.attachmentPathNameList.length >= 3
                        ? const SizedBox()
                        : const Gap(10),
                    contactUsController.attachmentPathNameList.length >= 3
                        ? const SizedBox()
                        : InkWell(
                            onTap: () async {
                              var result = await FilePickerMethod().pickFile();
                              if (result != null) {
                                contactUsController.attachmentPathNameList
                                    .clear();

                                for (var a in result) {
                                  contactUsController.attachmentFileList.add(a);
                                }

                                for (var a
                                    in contactUsController.attachmentFileList) {
                                  contactUsController.attachmentPathNameList
                                      .add(FilePickerMethod()
                                          .extractFileName(a?.path ?? ''));
                                }
                              }
                            },
                            child: Row(
                              children: [
                                SvgPicture.asset(
                                    "assets/images/svg/attachment_pin.svg"),
                                const Gap(6),
                                text12W400(
                                    "Add Attachment (Max 3 files of 2MB each / Optional)"),
                              ],
                            ),
                          ),
                    const Gap(20),
                    SizedBox(
                      width: Get.width,
                      child: kycBtn(
                        text: "Create Ticket",
                        onTap: () {
                          Get.back();
                          TicketConfirmedBottomSheet().bottomSheet(context);
                        },
                        bgClr: const Color(0xFF6C0000),
                        borderClr: const Color(0xFF990000),
                      ),
                    ),
                    const Gap(30),
                  ],
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
