import 'package:flutter/material.dart';
import 'package:test_sa/controllers/validator/validator.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart';
import 'package:test_sa/new_views/common_widgets/app_text_form_field.dart';

class UpdateUserContactInfoBottomSheet extends StatefulWidget {
  final String userID;
  final String? uEmail;
  final String? uPhoneNo;
  final String? uExtensionNo;
  final Function(String, String, String) onUpdate;

  UpdateUserContactInfoBottomSheet(this.userID, {Key? key, this.uEmail = "", this.uPhoneNo = "", this.uExtensionNo = "", required this.onUpdate}) : super(key: key);

  @override
  _UpdateUserContactInfoBottomSheetState createState() {
    return _UpdateUserContactInfoBottomSheetState();
  }
}

class _UpdateUserContactInfoBottomSheetState extends State<UpdateUserContactInfoBottomSheet> {
  String email = "";
  String phoneNo = "";
  String extensionNo = "";

  @override
  void initState() {
    super.initState();
    email = widget.uEmail ?? "";
    phoneNo = widget.uPhoneNo ?? "";
    extensionNo = widget.uExtensionNo ?? "";
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        8.height,
        Align(
          alignment: AlignmentDirectional.centerStart,
          child: "Update Information".bottomSheetHeadingTextStyle(context),
        ),
        16.height,
        AppTextFormField(
          labelText: "Email",
          backgroundColor: AppColor.fieldBgColor(context),
          initialValue: widget.uEmail,
          textAlign: TextAlign.center,
          hintText: "email@example.com",
          labelStyle: AppTextStyles.textFieldLabelStyle,
          hintStyle: AppTextStyles.textFieldLabelStyle,
          textInputType: TextInputType.emailAddress,
          showShadow: false,
          onChange: (value) {
            email = value;
          },
          style: Theme.of(context).textTheme.titleMedium,
        ),
        12.height,
        AppTextFormField(
          labelText: "Phone Number",
          backgroundColor: AppColor.fieldBgColor(context),
          initialValue: widget.uPhoneNo,
          textAlign: TextAlign.center,
          hintText: "05xxxxxxxx",
          labelStyle: AppTextStyles.textFieldLabelStyle,
          hintStyle: AppTextStyles.textFieldLabelStyle,
          textInputType: TextInputType.phone,
          showShadow: false,
          onChange: (value) {
            phoneNo = value;
          },
          style: Theme.of(context).textTheme.titleMedium,
        ),
        12.height,
        AppTextFormField(
          labelText: "Extension No",
          backgroundColor: AppColor.fieldBgColor(context),
          initialValue: widget.uExtensionNo,
          textAlign: TextAlign.center,
          hintText: "1234",
          labelStyle: AppTextStyles.textFieldLabelStyle,
          hintStyle: AppTextStyles.textFieldLabelStyle,
          textInputType: const TextInputType.numberWithOptions(decimal: true),
          showShadow: false,
          onChange: (value) {
            extensionNo = value;
          },
          style: Theme.of(context).textTheme.titleMedium,
        ),
        12.height,
        AppFilledButton(
          label: "Update",
          buttonColor: context.isDark?  AppColor.primary10:AppColor.neutral50,
          onPressed: () async {
            FocusManager.instance.primaryFocus!.unfocus();
            if (email.isEmpty || !Validator.isEmail(email)) {
              "Please enter valid email".showToast;
              return;
            }
            if (phoneNo.isEmpty || phoneNo.length != 10) {
              "Please enter valid phone number".showToast;
              return;
            }
            if (extensionNo.isEmpty) {
              "Please enter extension".showToast;
              return;
            }
            showDialog(context: context, barrierDismissible: false, builder: (context) => const AppLazyLoading());
            bool status = await context.userProvider.updateContactInfo(widget.userID, email, phoneNo, extensionNo);
            Navigator.pop(context);
            if (status) {
              Navigator.pop(context);
              widget.onUpdate(email, phoneNo, extensionNo);
            }
          },
        ),
      ],
    );
  }
}
