import 'package:flutter/material.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/views/advertisement/ad_creation_steps/ad_duration_container.dart';
import 'package:mc_common_app/views/advertisement/ad_creation_steps/ad_review_containers.dart';
import 'package:mc_common_app/views/advertisement/ad_creation_steps/damage_parts_container.dart';
import 'package:mc_common_app/views/advertisement/ad_creation_steps/vehicle_details_container.dart';
import 'package:mc_common_app/views/advertisement/components/create_ad_progress_steps_widget.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

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

  @override
  State<CreateAdView> createState() => _CreateAdViewState();
}

class _CreateAdViewState extends State<CreateAdView> {
  late AdVM adVM;

  @override
  void initState() {
    adVM = context.read<AdVM>();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: adVM.isAdEditEnabled ? LocaleKeys.updateAd.tr() : LocaleKeys.createAd.tr(),
        isRemoveBackButton: false,
        isDrawerEnabled: false,
        onBackButtonTapped: () => adVM.onBackButtonPressed(context),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            22.height,
            const CreateAdProgressSteps(),
            22.height,
            Consumer(builder: (BuildContext context, AdVM adVm, Widget? child) {
              return BuildAdStepContainer(
                onVehicleDetails: (context) => const VehicleDetails(),
                onDamageParts: (context) => const DamageParts(),
                onAdDuration: (context) => const AdDuration(),
                onReviewAd: (context) => const ReviewAd(),
                adCreationStepsEnum: adVm.currentProgressStep,
              );
            }),
            10.height,
            const BuildFooterButton().horPaddingMain(),
            10.height,
          ],
        ),
      ),
    );
  }
}

class BuildFooterButton extends StatelessWidget {
  const BuildFooterButton({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer(
      builder: (BuildContext context, AdVM adVm, Widget? child) {
        switch (adVm.currentProgressStep) {
          case AdCreationSteps.vehicleDetails:
            return SizedBox(
              width: double.infinity,
              child: ShowFillButton(
                title: LocaleKeys.next.tr(),
                onPressed: () {
                  adVm.updateCurrentStep(context);
                },
              ),
            );
          case AdCreationSteps.damageParts:
            return Row(
              children: [
                Expanded(
                  child: ShowFillButton(
                    txtColor: MyColors.black,
                    maxHeight: 55,
                    title: LocaleKeys.cancel.tr(),
                    onPressed: () {
                      adVm.resetValues();
                      pop(context);
                    },
                    backgroundColor: MyColors.greyButtonColor,
                  ),
                ),
                12.width,
                Expanded(
                  child: ShowFillButton(
                    maxHeight: 55,
                    title: LocaleKeys.next.tr(),
                    onPressed: () {
                      adVm.updateCurrentStep(context);
                    },
                    backgroundColor: MyColors.darkPrimaryColor,
                  ),
                ),
              ],
            );
          case AdCreationSteps.adDuration:
            return Row(
              children: [
                Expanded(
                  child: ShowFillButton(
                    txtColor: MyColors.black,
                    maxHeight: 55,
                    title: LocaleKeys.cancel.tr(),
                    onPressed: () {
                      adVm.resetValues();
                      pop(context);
                    },
                    backgroundColor: MyColors.greyButtonColor,
                  ),
                ),
                12.width,
                Expanded(
                  child: ShowFillButton(
                    maxHeight: 55,
                    title: LocaleKeys.next.tr(),
                    onPressed: () {
                      adVm.updateCurrentStep(context);
                    },
                    backgroundColor: MyColors.darkPrimaryColor,
                  ),
                ),
              ],
            );
          case AdCreationSteps.reviewAd:
            return Row(
              children: [
                Expanded(
                  child: ShowFillButton(
                    txtColor: MyColors.black,
                    maxHeight: 55,
                    title: LocaleKeys.cancel.tr(),
                    onPressed: () {
                      adVm.resetValues();
                      pop(context);
                    },
                    backgroundColor: MyColors.greyButtonColor,
                  ),
                ),
                12.width,
                Expanded(
                  child: ShowFillButton(
                    maxHeight: 55,
                    title: LocaleKeys.submitAd.tr(),
                    onPressed: () {
                      adVm.updateCurrentStep(context);
                    },
                    backgroundColor: MyColors.darkPrimaryColor,
                  ),
                ),
              ],
            );

          default:
            const SizedBox();
        }
        return const SizedBox();
      },
    );
  }
}

class BuildAdStepContainer extends StatelessWidget {
  final WidgetBuilder onVehicleDetails;
  final WidgetBuilder onDamageParts;
  final WidgetBuilder onAdDuration;
  final WidgetBuilder onReviewAd;
  final AdCreationSteps adCreationStepsEnum;

  const BuildAdStepContainer({
    super.key,
    required this.onVehicleDetails,
    required this.onDamageParts,
    required this.onAdDuration,
    required this.onReviewAd,
    required this.adCreationStepsEnum,
  });

  @override
  Widget build(BuildContext context) {
    switch (adCreationStepsEnum) {
      case AdCreationSteps.vehicleDetails:
        return onVehicleDetails(context);

      case AdCreationSteps.damageParts:
        return onDamageParts(context);

      case AdCreationSteps.adDuration:
        return onAdDuration(context);

      case AdCreationSteps.reviewAd:
        return onReviewAd(context);
    }
  }
}
