import 'package:account/src/features/delete_account/presentation/state/delete_account_view_model.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:legacy_shared/legacy_shared.dart';
import 'package:navigation/navigation.dart';
import 'package:sf_localizations/sf_localizations.dart';
import 'package:utils/utils.dart';

class DeleteAccountScreen extends ConsumerWidget {
  final NavigationContract navigationContract;

  const DeleteAccountScreen({super.key, required this.navigationContract});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final theme = ref.watch(themePortProvider);

    final state = ref.watch(deleteAccountViewModelProvider);
    final viewModel = ref.read(deleteAccountViewModelProvider.notifier);

    return PageLayout(
      title: context.translate(I18n.legacyDeleteAccount),
      body: SingleChildScrollView(child: Container(
        margin: EdgeInsets.symmetric(horizontal: 10),
        child: Column(
          children: [
            Container(
              color: theme.getColorFor(ThemeCode.backgroundSecondary),
              width: double.infinity,
              padding: SizeUtils.getByScreen(
                small: EdgeInsets.symmetric(vertical: 20),
                big: EdgeInsets.symmetric(vertical: 19),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    decoration: BoxDecoration(
                      color: Color(0xFF00A1C6),
                      borderRadius: BorderRadius.circular(12)
                    ),
                    padding: SizeUtils.getByScreen(
                      small: EdgeInsets.all(7),
                      big: EdgeInsets.all(6),
                    ),
                    child: Icon(Icons.power_settings_new_outlined,
                      size: SizeUtils.getByScreen(small: 48, big: 46),
                      color: Colors.white,
                    ),
                  ),
                  Text(context.translate(I18n.legacyDeleteAccount),
                    style: TextStyle(fontSize: SizeUtils.getByScreen(small: 20, big: 19)),
                  )
                ],
              ),
            ),
            SizedBox(height: SizeUtils.getByScreen(small: 8, big: 6)),
            Text(context.translate(I18n.legacyDeleteAccountBody1),
              textAlign: TextAlign.start,
            ),
            SizedBox(height: SizeUtils.getByScreen(small: 38, big: 36)),
            Text(context.translate(I18n.legacyDeleteAccountBody2),
              textAlign: TextAlign.start,
              style: TextStyle(
                fontSize: 12,
                color: theme.getColorFor(ThemeCode.textPrimary)
              ),
            ),
          ],
        )
      )),
      footer: Container(
        padding: SizeUtils.getByScreen(
          small: EdgeInsets.symmetric(horizontal: 14, vertical: 10),
          big: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        ),
        child: PrimaryButton(
          onPressed: (){
            if (state.loggedUser != null) {
              showDialog(context: context, builder: (context) =>
                Dialog(
                  backgroundColor: Colors.transparent,
                  child: ConfirmDialog(navigationContract: navigationContract),
                )
              );
            }
          },
          text: context.translate(I18n.legacyRequestCancelButton),
          color: Color(0xFF588EA5)
        ),
      ),
    );
  }
}

class ConfirmDialog extends ConsumerWidget{

  final NavigationContract navigationContract;

  const ConfirmDialog({
    super.key,
    required this.navigationContract,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(deleteAccountViewModelProvider);
    final viewModel = ref.read(deleteAccountViewModelProvider.notifier);

    final steps = [
      Container(
        height: 210,
        width: 500,
        color: Colors.white,
        padding: SizeUtils.getByScreen(
          small: EdgeInsets.symmetric(horizontal: 22, vertical: 11),
          big: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(context.translate(I18n.legacyVerifyAccount),
              style: TextStyle(
                fontWeight: FontWeight.w500
              ),
            ),
            SizedBox(height: SizeUtils.getByScreen(small: 18, big: 16)),
            Text('${context.translate(I18n.email)}: ${state.loggedUser!.email}'),
            SizedBox(height: SizeUtils.getByScreen(small: 8, big: 6)),
            Row(
              children: [
                Text('${context.translate(I18n.password)}: '),
                SizedBox(width: SizeUtils.getByScreen(small: 12, big: 10)),
                Expanded(child: TextField(
                  controller: viewModel.passwordController,
                  style: TextStyle(fontSize: 12),
                  decoration: InputDecoration(hintText: context.translate(I18n.password)),
                  obscureText: true,
                  enableSuggestions: false,
                  autocorrect: true,
                ))
              ],
            ),
            if (state.errorMessage.isNotEmpty)
              Text(
                state.errorMessage,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Theme.of(context).colorScheme.error,
                  fontSize: 13,
                ),
              ),
            SizedBox(height: SizeUtils.getByScreen(small: 12, big: 10)),
            Row(
              children: [
                Expanded(child: SecondaryButton(
                  onPressed: (){Navigator.pop(context);},
                  text: context.translate(I18n.legacyCancel),
                  color: Color(0xFF588EA5),
                  height: 40,
                  radius: 20,
                )),
                SizedBox(width: SizeUtils.getByScreen(small: 12, big: 10)),
                Expanded(child: PrimaryButton(
                  onPressed: viewModel.nextStep,
                  text: context.translate(I18n.accept),
                  color: Color(0xFF588EA5),
                  height: 40,
                  radius: 20,
                )),
              ],
            )
          ],
        ),
      ),
      Container(
        height: 400,
        width: 500,
        color: Colors.white,
        padding: SizeUtils.getByScreen(
          small: EdgeInsets.symmetric(horizontal: 22, vertical: 11),
          big: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(height: SizeUtils.getByScreen(small: 14, big: 12)),
            Text(context.translate(I18n.legacyRequestCancelTitle),
              style: TextStyle(
                fontWeight: FontWeight.w500
              ),
            ),
            SizedBox(height: SizeUtils.getByScreen(small: 14, big: 12)),
            Expanded(child: SingleChildScrollView(child: Column(
              children: [
                Text(context.translate(I18n.legacyRequestCancelBody),
                  style: TextStyle(height: 1.5),
                ),
                SizedBox(height: SizeUtils.getByScreen(small: 12, big: 10)),
                ...List<Widget>.generate(state.devices.length, (int index) =>
                  CheckboxListTile(
                    contentPadding: EdgeInsets.zero,
                    title: Text(context.translate(I18n.legacyDeleteDeviceData,
                        args: {'name': state.devices[index].carrierName}
                      ),
                      style: TextStyle(height: 0),
                    ),
                    controlAffinity: ListTileControlAffinity.leading,
                    value: false,
                    onChanged: (_){
                      viewModel.updateDeleteDevice(index);
                    }
                  )
                ),
              ]
            ))),
            SizedBox(height: SizeUtils.getByScreen(small: 12, big: 10)),
            Row(
              children: [
                Expanded(child: SecondaryButton(
                  onPressed: (){
                    viewModel.resetConfirmStep();
                    Navigator.pop(context);
                  },
                  text: context.translate(I18n.legacyCancel),
                  color: Color(0xFF588EA5),
                  height: 50,
                  radius: 25,
                )),
                SizedBox(width: SizeUtils.getByScreen(small: 12, big: 10)),
                Expanded(child: PrimaryButton(
                  onPressed: () async {
                    viewModel.deleteAccount();
                    if (!context.mounted) return;

                    navigationContract.goTo(AppRoutes.login);
                  },
                  text: context.translate(I18n.legacyConfirm),
                  color: Color(0xFF588EA5),
                  height: 50,
                  radius: 25,
                )),
              ],
            )
          ],
        ),
      )
    ];

    return steps[state.confirmStep];
  }

}
