import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/screens/home/state.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/utils/tools/tools.dart';
import 'package:qadirneyriz/widgets/custom_appbar.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:qadirneyriz/widgets/custom_button.dart';

class DeleteAccountScreen extends StatefulWidget {
  const DeleteAccountScreen({super.key});

  @override
  State<DeleteAccountScreen> createState() => _DeleteAccountScreenState();
}

class _DeleteAccountScreenState extends State<DeleteAccountScreen> {
  @override
  Widget build(BuildContext context) {
    return Consumer<HomeState>(
      builder: (context, value, child) {
        return CustomScrollView(
          slivers: [
            CustomAppbar(),
            SliverFillRemaining(
              hasScrollBody: false,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Icon(
                      Icons.warning_amber_rounded,
                      size: 100,
                      color: Colors.red,
                    ),
                    const SizedBox(height: 20),
                    Text(
                      AppLocalizations.of(context)!.suretodelelteaccount,
                      textAlign: TextAlign.center,
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 20),
                    Text(
                      AppLocalizations.of(context)!.actioncantundo,
                      textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 16, color: Colors.grey[600]),
                    ),
                    const SizedBox(height: 40),
                    deleteButton(value),
                  ],
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  CustomButton deleteButton(HomeState state) {
    switch (state.statusDeleteAccount) {
      case Status.loading:
        return CustomButton(
          onPressed: null,
          text: AppLocalizations.of(context)!.loading,
          hieght: 50,
        );

      default:
        return CustomButton(
          onPressed: () async {
            final status = await state.deleteAccount();
            if (status == Status.ready) {
              final logOut = await setting.userLocalDb.logOut();
              if (logOut) {
                context.goNamed('route');
                Tools.showCustomSnackBar(context,
                    text: state.messageDeleteAccount ?? 'Done successfully',
                    isError: false);
              } else {
                Tools.showCustomSnackBar(context,
                    text: state.messageDeleteAccount ??
                        AppLocalizations.of(context)!.error,
                    isError: true);
              }
            } else {
              Tools.showCustomSnackBar(context,
                  text: state.messageDeleteAccount ??
                      AppLocalizations.of(context)!.error,
                  isError: true);
            }
          },
          text: AppLocalizations.of(context)!.deleteaccount,
          hieght: 50,
        );
    }
  }
}
