import 'package:cims_apps/application/assets/path_assets.dart';
import 'package:cims_apps/application/component/button/button_view.dart';
import 'package:cims_apps/application/component/image/image_view.dart';
import 'package:cims_apps/application/component/text_caption/text_caption.dart';
import 'package:cims_apps/application/component/text_form/text_form_view.dart';
import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:cims_apps/core/utils/string_utils.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submission_parent.dart';
import 'package:cims_apps/features/auth/registration/viewmodel/submission_data_viewmodel.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SubmitEmail extends StatelessWidget {
  const SubmitEmail({Key? key}) : super(key: key);

  Widget _emailVerify(BuildContext context, SubmissionDataViewModel provider) {
    var textTheme = Theme.of(context).textTheme;
    return Padding(
      padding: const EdgeInsets.all(24.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const TextCaption(title: 'Check your e-mail'),
          const ImageView(image: PathAssets.imgEmail),
          Align(
            alignment: Alignment.center,
            child: RichText(
              textAlign: TextAlign.center,
              text: TextSpan(children: [
                TextSpan(
                  text:
                      'We have sent a verification link to your e-mail. \nPlease check your email for ',
                  style: textTheme.displayMedium,
                ),
                TextSpan(
                  recognizer: TapGestureRecognizer()
                    ..onTap = () async {
                      await provider.next(context).then((value) {
                        if (value) {
                          routePush(context, page: const SubmissionParent());
                        }
                      });
                    },
                  text: 'verification',
                  style: const TextStyle(
                    color: Colors.blue,
                  ),
                ),
                TextSpan(
                  text: ' to \ncontinue registration.',
                  style: textTheme.displayMedium,
                ),
              ]),
            ),
          ),
        ],
      ),
    );
  }

  showEmailVerify(BuildContext context, SubmissionDataViewModel provider) {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      enableDrag: false,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.zero,
        ),
      ),
      builder: (context) {
        var flutterView = View.of(context);
        return Padding(
          padding: EdgeInsets.only(
            top: MediaQueryData.fromView(flutterView).padding.top,
          ),
          child: _emailVerify(context, provider),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => SubmissionDataViewModel(),
        builder: (context, child) {
          return Consumer<SubmissionDataViewModel>(
              builder: (context, provider, child) {
            return SizedBox(
              height: SizeConfig.height * .78,
              child: Form(
                key: provider.formKeySubmitEmail,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    const TextCaption(title: 'Enter your e-mail'),
                    TextFormView(
                      name: 'E-mail Address',
                      hintText: 'Input e-mail address',
                      keyboardType: TextInputType.emailAddress,
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Filled cannot be empty';
                        } else if (!StringUtils.emailValidation(value)) {
                          return 'Format email wrong';
                        } else {
                          return null;
                        }
                      },
                    ),
                    SizedBox(height: SizeConfig.height * .43),
                    ButtonView(
                      name: 'Next',
                      onPressed: () async {
                        if (provider.formKeySubmitEmail.currentState!
                            .validate()) {
                          showEmailVerify(context, provider);
                        }
                      },
                    )
                  ],
                ),
              ),
            );
          });
        });
  }
}
