import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class FormStepLayout extends ConsumerWidget {
  final int currentStep;
  final int numSteps;
  final String? supertitle;
  final String title;
  final String? subtitle;
  final List<Widget> body;
  final List<Widget>? footer;
  final VoidCallback nextStep;
  final VoidCallback previousStep;

  const FormStepLayout({
    super.key,
    required this.title,
    this.subtitle,
    this.supertitle,
    required this.currentStep,
    required this.numSteps,
    required this.body,
    this.footer,
    required this.nextStep,
    required this.previousStep,
  });

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

    return Scaffold(
      backgroundColor: theme.getColorFor(ThemeCode.backgroundPrimary),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            color: theme.getColorFor(ThemeCode.backgroundPrimary),
            padding: EdgeInsets.only(left: 24, right: 24),
            child: Column(
              children: [
                Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      child: StepIndicator(
                        total: numSteps,
                        current: currentStep,
                        color: theme.getColorFor(ThemeCode.buttonPrimary),
                      ),
                    ),
                    SizedBox(height: 16),
                    if (supertitle != null)
                      Text(
                        supertitle!,
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 18),
                      ),
                    SizedBox(height: 10),
                    Text(
                      title,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 30,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 16),
                    if (subtitle != null)
                      Text(
                        subtitle!,
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 18),
                      ),
                  ],
                ),
                ...body,
                SizedBox(height: 16),
                if (footer == null)
                  navigationButtons(
                    currentStep,
                    numSteps,
                    nextStep,
                    previousStep,
                    theme,
                  ),
                ...(footer ?? []),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget navigationButtons(
    int currentStep,
    int numSteps,
    VoidCallback nextStep,
    VoidCallback previousStep,
    ThemePort theme,
  ) {
    if (currentStep == numSteps) {
      return PrimaryButton(
        onPressed: nextStep,
        text: "Continuar",
        color: theme.getColorFor(ThemeCode.buttonPrimary),
      );
    } else {
      return Row(
        spacing: 16,
        children: [
          Expanded(
            child: SecondaryButton(
              onPressed: previousStep,
              text: "Atrás",
              size: 16,
            ),
          ),
          Expanded(
            child: PrimaryButton(
              onPressed: nextStep,
              text: "Siguiente",
              size: 16,
              color: theme.getColorFor(ThemeCode.buttonSecondary),
            ),
          ),
        ],
      );
    }
  }
}
