import 'package:cims_apps/application/component/button/back_button_view.dart';
import 'package:cims_apps/application/component/custom_app_bar/custom_app_bar.dart';
import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/data_bank/submit_bank_account.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/risk_profile/risk_profile_view.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_data_id_card.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_photo_ktp.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_email.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_personal_data.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_photo_selfie.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/submit_signature/initial_signature.dart';
import 'package:cims_apps/features/auth/registration/viewmodel/submission_data_viewmodel.dart';
import 'package:cims_apps/features/bottom_navigation_view.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SubmissionParent extends StatefulWidget {
  static const routeName = '/SubmissionParent';
  const SubmissionParent({Key? key}) : super(key: key);

  @override
  State<SubmissionParent> createState() => _SubmissionParentState();
}

class _SubmissionParentState extends State<SubmissionParent> {
  Widget _stepItem({bool isCurrentStep = false}) {
    return Container(
      margin: const EdgeInsets.only(right: 0.0, left: 4.0),
      height: 6,
      width: SizeConfig.width * .08,
      decoration: BoxDecoration(
        color: isCurrentStep
            ? ColorPalette.primary
            : ColorPalette.greyBorderNeutrals,
        borderRadius: BorderRadius.circular(50),
      ),
    );
  }

  _content(int index) {
    switch (index) {
      case 1:
        return const SubmitPersonalData();
      case 2:
        return const SubmitEmail();
      case 3:
        return const SubmitPhotoKtp();
      case 4:
        return const SubmitPhotoSelfie();
      case 5:
        return const SubmitDataIdCard();
      case 6:
        return const SubmitBankAccount();
      case 7:
        return const InitialSignature();
      case 8:
        return const RiskProfileView();
      case 9:
        return const Text("Step 9");
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => SubmissionDataViewModel(),
        builder: (context, child) {
          return PopScope(
            canPop: false,
            onPopInvoked: (didPop) async {
              if (didPop) {
                return;
              }
              await routePush(context,
                  page: const BottomNavigationView(),
                  routeType: RouteType.pushReplace);
            },
            child: Consumer<SubmissionDataViewModel>(
                builder: (context, provider, child) {
              return Scaffold(
                appBar: CustomAppBar(
                  height: SizeConfig.height * .1,
                  title: 'Registration',
                  leading: BackButtonView(
                    onPress: () {
                      routePush(
                        context,
                        page: const BottomNavigationView(),
                        routeType: RouteType.pushReplace,
                      );
                    },
                  ),
                ),
                body: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16.0, vertical: 16.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: List.generate(provider.stepAmount, (index) {
                          return _stepItem(
                            isCurrentStep:
                                provider.getCurrentStep == index + 1 ||
                                    provider.getCurrentStep - 1 > index,
                          );
                        }),
                      ),
                    ),
                    Expanded(
                      child: SingleChildScrollView(
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        child: _content(provider.getCurrentStep),
                      ),
                    ),
                  ],
                ),
              );
            }),
          );
        });
  }
}
