import 'package:flutter/material.dart';
import 'package:test_sa/common_widgets/stapper_widget/components/base_step.dart';
import 'package:test_sa/common_widgets/stapper_widget/components/custom_line.dart';
import 'package:test_sa/common_widgets/stapper_widget/custom_stepper.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/service_request_latest/views/forms/work_order/components/asset_conditon_view.dart';
import 'package:test_sa/service_request_latest/views/forms/work_order/components/attachments_view.dart';
import 'package:test_sa/service_request_latest/views/forms/work_order/components/time_duration_view.dart';

class WorkOrderFormView extends StatefulWidget {

  const WorkOrderFormView({Key? key}) : super(key: key);

  @override
  _WorkOrderFormViewState createState() => _WorkOrderFormViewState();
}

class _WorkOrderFormViewState extends State<WorkOrderFormView> {
  int currentStep = 0;
  int activeStep = 0;
  int reachedStep = 0;
  int upperBound = 2;
  final formKey = GlobalKey<FormState>();

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getInitialData();
    });
    super.initState();
  }

  Future<void> getInitialData() async {
    // ListingVm listingVm = Provider.of<ListingVm>(context, listen: false);
    // listingVm.listingPropertyType = widget.arguments['listingType'];
    // if (widget.arguments['formType'] == AppConstants.addListing) {
    //   listingVm.landLordModel = LandLordModel();
    //   listingVm.listing['propertyType'] = widget.arguments['listingType'];
    //   if (listingVm.listing['propertyType'] == AppConstants.sell) {
    //     listingVm.listingParameter = 'sellParameter';
    //     listingVm.listing[listingVm.listingParameter] =
    //         SellParameter().toJson();
    //     listingVm.listing[listingVm.listingParameter]?['ownerDocs'] = [];
    //   }
    //   if (listingVm.listing['propertyType'] == AppConstants.rent) {
    //     listingVm.listingParameter = 'rentParameter';
    //     listingVm.listing[listingVm.listingParameter] =
    //         RentParameter().toJson();
    //     listingVm.listing[listingVm.listingParameter]?['ownerDocs'] = [];
    //   }
    // } else if (widget.arguments['formType'] == AppConstants.editListing) {
    //   if (listingVm.listing['propertyType'] == AppConstants.sell) {
    //     listingVm.listingParameter = 'sellParameter';
    //   }
    //   if (listingVm.listing['propertyType'] == AppConstants.rent) {
    //     listingVm.listingParameter = 'rentParameter';
    //   }
    //   if (widget.arguments['isDuplicate'] == true) {
    //     FormUtils.setDuplicateValues(
    //         listingVm: listingVm, listType: widget.arguments['listingType']);
    //   }
    //   listingVm.formLocationController.clear();
    //   listingVm.formCommunityInitialValue =
    //   await FormUtils.setLocation(listingVm.listing, context);
    //   listingVm.formLocationController.text =
    //       listingVm.formCommunityInitialValue ?? '';
    //
    //   listingVm.formAvailabilityDate =
    //       listingVm.listing[listingVm.listingParameter]['availabilityDate'] ??
    //           '';
    //   listingVm.formBuildYearDate =
    //       listingVm.listing[listingVm.listingParameter]['buildYear'] ?? '';
    //
    //   listingVm.listing['photos'] ??= [];
    //   listingVm.listing[listingVm.listingParameter]['ownerDocs'] ??= [];
    // }
    // GeneralUtil.initilizePortalsSwitch(listingVm: listingVm);
    // listingVm.getTenantExtent();
    // await listingVm.getRegion();
    // if (listingVm.listing[listingVm.listingParameter]
    // ['propertyFinderLocation'] !=
    //     null) {
    //   await listingVm.getPropertyFinderLocationList(
    //       searchQuery: listingVm.listing[listingVm.listingParameter]
    //       ['propertyFinderLocation']);
    // }
    // await listingVm.getWaterMarkImage();
    // await listingVm.getDeveloper();
    // await GeneralUtil.loadPortalsLocationsData(listingVm);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.neutral100,
      appBar: DefaultAppBar(title: context.translation.createWorkOrder),
      body: Column(
        children: [
          16.height,
          CustomStepper(
            activeStep: activeStep,
            lineStyle: LineStyle(
              activeLineColor: AppColor.primary10,
              finishedLineColor: AppColor.primary10,
              defaultLineColor: AppColor.white40,
              lineThickness: 1,
              lineLength: 93.toScreenWidth,
              lineType: LineType.normal,
            ),
            internalPadding: 2.toScreenWidth,
            steps: [
              StepModel(customStep: customStepWidget(index: 0, stepIndex: 1), customTitle: customStepLabel(index: 1, label: context.translation.timeAndDuration)),
              StepModel(customStep: customStepWidget(index: 1, stepIndex: 2), customTitle: customStepLabel(index: 1, label: context.translation.assetsCondition)),
              StepModel(customStep: customStepWidget(index: 2, stepIndex: 3), customTitle: customStepLabel(index: 1, label: context.translation.attachmentsAcknowledge)),
            ],
            onStepReached: (index) => setState(() => activeStep = index),
          ),
          Expanded(
              child: Padding(
            padding: EdgeInsetsDirectional.symmetric(horizontal: 16.toScreenWidth, vertical: 25.toScreenHeight),
            child: getStepWidget(),
          )),
          bottomContainerWidget(),
        ],
      ),
    );
  }

  Widget bottomContainerWidget() {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth,vertical: 16.toScreenHeight),
        color: AppColor.white10,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            AppFilledButton(
              label: context.translation.cancel,
              loading: false,
              buttonColor: AppColor.white60,
              textColor: AppColor.black10,
              onPressed: () async {
                Navigator.pop(context);
                // await snapshot.updateRequest(user: userProvider.user, request: serviceRequestProvider.serviceRequest);
                // Navigator.pop(context, true);
              },
            ).expanded,
            12.width,
            AppFilledButton(
              label: context.translation.next,
              buttonColor: AppColor.primary10,
              loading: false,
              onPressed: () {
                _incrementActiveStep();
                //TODO move to next step..
              },
            ).expanded,
          ],
        ));
  }

  void _incrementActiveStep() {
    setState(() {
      ++activeStep;
      if (reachedStep < activeStep) {
        reachedStep = activeStep;
      }
    });
  }

  Future<void> _navigateNext(ServiceRequestsProvider serviceRequestsProvider) async {
    if (activeStep < upperBound) {
      switch (activeStep) {
        case 0:
          // await _navigateNextStep(listingVm);
          break;
        case 1:
          break;
        case 2:
          _incrementActiveStep();
          break;
      }
    }
  }

// Your onPressed code

  Widget customStepWidget({required int index, required int stepIndex}) {
    return CircleAvatar(
      backgroundColor: activeStep >= index ? AppColor.primary10 : AppColor.background(context),
      child: activeStep > index
          ? Icon(
              Icons.done,
              color: AppColor.white10,
              size: 26.toScreenHeight,
            )
          : activeStep == index
              ? Text(
                  '0$stepIndex',
                  style: const TextStyle(fontWeight: FontWeight.w500, color: AppColor.white10),
                  textAlign: TextAlign.center,
                )
              : Text(
                  '0$stepIndex',
                  style: const TextStyle(color: AppColor.black20),
                ),
    );
  }

  Widget customStepLabel({required String label, required int index}) {
    return Column(
      children: [
        Text(
          label,
          textAlign: TextAlign.center,
          style: AppTextStyles.bodyText2.copyWith(color: AppColor.black20),
        ),
      ],
    );
  }

  /// Returns the previous button.

  Widget getStepWidget() {
    switch (activeStep) {
      case 0:
        return  const TimeDurationView();
      case 1:
        return  AssetConditionView();
      case 2:
        return  AttachmentView();
      default:
        return const SizedBox();
    }
  }
}
