import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/common/text_formatters/expiration_date_formatter.dart';
import 'package:krow/core/application/common/validators/certificate_date_validator.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';

class CertificatesUploadDialog extends StatefulWidget {
  final String path;

  const CertificatesUploadDialog({
    super.key,
    required this.path,
  });

  @override
  State<CertificatesUploadDialog> createState() =>
      _CertificatesUploadDialogState();

  static Future<String?> show(BuildContext context, String path) {
    return showDialog<String?>(
      context: context,
      builder: (BuildContext context) {
        return CertificatesUploadDialog(
          path: path,
        );
      },
    );
  }
}

class _CertificatesUploadDialogState extends State<CertificatesUploadDialog> {
  final TextEditingController expiryDateController = TextEditingController();
  String? inputError;

  @override
  void initState() {
    super.initState();
    expiryDateController.addListener(() {
      if(expiryDateController.text.length == 10) {
        inputError =
            CertificateDateValidator.validate(expiryDateController.text);
      }else{
        inputError = null;
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Center(
        child: Container(
          constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 0.9),
          decoration: BoxDecoration(
            color: AppColors.grayWhite,
            borderRadius: BorderRadius.circular(16),
          ),
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ..._buildDialogTitle(context),
                  Material(
                    type: MaterialType.transparency,
                    child: KwTextInput(
                      title: 'expiry_date_1'.tr(),
                      hintText: 'enter_certificate_expiry_date'.tr(),
                      controller: expiryDateController,
                      maxLength: 10,
                      inputFormatters: [DateTextFormatter()],
                      keyboardType: TextInputType.datetime,
                      helperText: inputError,
                      showError: inputError != null,
                    ),
                  ),
                  const Gap(12),
                  _buildImage(context),
                  const Gap(24),
                  KwButton.primary(
                    label: 'save_certificate'.tr(),
                    disabled: expiryDateController.text.length != 10 || inputError != null,
                    onPressed: () {
                      Navigator.of(context).pop(expiryDateController.text);
                    },
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  ClipRRect _buildImage(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(16),
      child: Image.file(
        File(widget.path),
        width: MediaQuery.of(context).size.width - 80,
        fit: BoxFit.contain,
      ),
    );
  }

  List<Widget> _buildDialogTitle(BuildContext context) {
    return [
      Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
           Text(
            'add_certificate_expiry_date'.tr(),
            style: AppTextStyles.bodyLargeMed,
          ),
          GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Assets.images.icons.x.svg(),
          ),
        ],
      ),
      const Gap(8),
      Text(
        'please_enter_expiry_date'.tr(),
        style: AppTextStyles.bodySmallReg.copyWith(color: AppColors.blackGray),
      ),
      const SizedBox(height: 24),
    ];
  }
}
