content: import 'package:flutter/material.dart';
  
  import 'package:provider/provider.dart';
  import 'package:uuid/uuid.dart';
  
  import 'package:kubenav/models/cluster_provider.dart';
  import 'package:kubenav/repositories/clusters_repository.dart';
  import 'package:kubenav/services/providers/aws_service.dart';
  import 'package:kubenav/utils/constants.dart';
  import 'package:kubenav/utils/showmodal.dart';
  import 'package:kubenav/utils/themes.dart';
  import 'package:kubenav/widgets/settings/clusters/settings_add_cluster_aws.dart';
  import 'package:kubenav/widgets/shared/app_bottom_sheet_widget.dart';
  
  class SettingsAWSProvider extends StatefulWidget {
    const SettingsAWSProvider({super.key, required this.provider});
  
    final ClusterProvider? provider;
  
    @override
    State<SettingsAWSProvider> createState() => _SettingsAWSProviderState();
  }
  
  class _SettingsAWSProviderState extends State<SettingsAWSProvider> {
    final _providerConfigFormKey = GlobalKey<FormState>();
    final _nameController = TextEditingController();
    final _accessKeyIDController = TextEditingController();
    final _secretKeyController = TextEditingController();
    String _region = 'us-east-1';
    final _sessionTokenController = TextEditingController();
    final _roleArnController = TextEditingController();
    bool _isLoading = false;
  
    /// [_validator] is used to validate all the required fields. If they are
    /// missing the validation of the form will fail.
    String? _validator(String? value) {
      if (value == null || value.isEmpty) {
        return 'This field is required';
      }
  
      return null;
    }
  
    Future<void> _saveProvider() async {
      ClustersRepository clustersRepository = Provider.of<ClustersRepository>(
        context,
        listen: false,
      );
  
      try {
        if (_providerConfigFormKey.currentState != null &&
            _providerConfigFormKey.currentState!.validate()) {
          setState(() {
            _isLoading = true;
          });
          if (widget.provider == null) {
            final provider = ClusterProvider(
              id: const Uuid().v4(),
              name: _nameController.text,
              type: ClusterProviderType.aws,
              aws: ClusterProviderAWS(
                accessKeyID: _accessKeyIDController.text,
                secretKey: _secretKeyController.text,
                region: _region,
                sessionToken: _sessionTokenController.text,
                roleArn: _roleArnController.text,
              ),
            );
            await clustersRepository.addProvider(provider);
  
            setState(() {
              _isLoading = false;
            });
            if (mounted) {
              Navigator.pop(context);
              showModal(context, SettingsAddClusterAWS(provider: provider));
            }
          } else {
            final provider = widget.provider;
            provider!.name = _nameController.text;
            provider.aws!.accessKeyID = _accessKeyIDController.text;
            provider.aws!.secretKey = _secretKeyController.text;
            provider.aws!.region = _region;
            provider.aws!.sessionToken = _sessionTokenController.text;
            provider.aws!.roleArn = _roleArnController.text;
            await clustersRepository.editProvider(provider);
  
            setState(() {
              _isLoading = false;
            });
            if (mounted) {
              Navigator.pop(context);
            }
          }
        }
      } catch (err) {
        setState(() {
          _isLoading = false;
        });
        if (mounted) {
          showSnackbar(
            context,
            'Failed to Save Provider Configuration',
            err.toString(),
          );
        }
      }
    }
  
    @override
    void initState() {
      super.initState();
      if (widget.provider != null && widget.provider!.aws != null) {
        _nameController.text = widget.provider!.name!;
        _accessKeyIDController.text = widget.provider!.aws!.accessKeyID ?? '';
        _secretKeyController.text = widget.provider!.aws!.secretKey ?? '';
        _region = widget.provider!.aws!.region ?? '';
        _sessionTokenController.text = widget.provider!.aws!.sessionToken ?? '';
        _roleArnController.text = widget.provider!.aws!.roleArn ?? '';
      }
    }
  
    @override
    void dispose() {
      _nameController.dispose();
      _accessKeyIDController.dispose();
      _secretKeyController.dispose();
      _sessionTokenController.dispose();
      _roleArnController.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return AppBottomSheetWidget(
        title: ClusterProviderType.aws.title(),
        subtitle: ClusterProviderType.aws.subtitle(),
        icon: ClusterProviderType.aws.icon(),
        closePressed: () {
          Navigator.pop(context);
        },
        actionText: widget.provider == null ? 'Save and Add Cluster(s)' : 'Save',
        actionPressed: () {
          _saveProvider();
        },
        actionIsLoading: _isLoading,
        child: Form(
          key: _providerConfigFormKey,
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.only(
                top: Constants.spacingMiddle,
                bottom: Constants.spacingMiddle,
                left: Constants.spacingMiddle,
                right: Constants.spacingMiddle,
              ),
              child: Column(
                children: [
                  TextFormField(
                    controller: _nameController,
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    enableSuggestions: false,
                    maxLines: 1,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Name',
                    ),
                    validator: _validator,
                  ),
                  const SizedBox(height: Constants.spacingMiddle),
                  TextFormField(
                    controller: _accessKeyIDController,
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    enableSuggestions: false,
                    maxLines: 1,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Access Key ID',
                    ),
                    validator: _validator,
                  ),
                  const SizedBox(height: Constants.spacingMiddle),
                  TextFormField(
                    controller: _secretKeyController,
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    enableSuggestions: false,
                    maxLines: 1,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Secret Key',
                    ),
                    validator: _validator,
                  ),
                  const SizedBox(height: Constants.spacingMiddle),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      const Text('Region'),
                      DropdownButton(
                        value: _region,
                        underline: Container(
                          height: 2,
                          color: Theme.of(context).colorScheme.primary,
                        ),
                        onChanged: (String? newValue) {
                          setState(() {
                            _region = newValue ?? '';
                          });
                        },
                        items: awsRegions.map((value) {
                          return DropdownMenuItem(
                            value: value,
                            child: Text(
                              value,
                              style: TextStyle(
                                color: Theme.of(
                                  context,
                                ).extension<CustomColors>()!.textPrimary,
                              ),
                            ),
                          );
                        }).toList(),
                      ),
                    ],
                  ),
                  const SizedBox(height: Constants.spacingMiddle),
                  TextFormField(
                    controller: _sessionTokenController,
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    enableSuggestions: false,
                    maxLines: 1,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Session Token (optional)',
                    ),
                  ),
                  const SizedBox(height: Constants.spacingMiddle),
                  TextFormField(
                    controller: _roleArnController,
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    enableSuggestions: false,
                    maxLines: 1,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Role ARN (optional)',
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }
  }
  