import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/passo/bulding/additional_item/additional_item_bloc.dart';
import 'package:unit2/bloc/passo/bulding/property_appraisal/property_appraisal_bloc.dart';
import 'package:unit2/bloc/passo/bulding/property_info/property_info_bloc.dart';
import 'package:unit2/model/passo/additional_items.dart';
import 'package:unit2/model/passo/property_appraisal.dart';
import 'package:unit2/screens/passo/Building/add_building.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/widgets/passo/custom_button.dart';

class PropertyAppraisalPage extends StatefulWidget {
  final VoidCallback NextBtn;
  final VoidCallback PrevBtn;

  PropertyAppraisalPage(this.NextBtn, this.PrevBtn);

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

class _PropertyAppraisalPage extends State<PropertyAppraisalPage> {
  double depRate = 0;

  double _depRate = 0;

  double assessment_level = 0;
  bool isTaxable = false;
  bool isExempt = false;
  String _memoranda = '';
  final focus = FocusNode();

  String assessmentLevel(marketValues, property_class) {
    final marketValue = double.parse(marketValues);
    switch (property_class) {
      case 'Residential':
        if (marketValue < 175000) {
          // setState(() {
          //   assessment_level = 0;
          // });
          return '0 ';
        } else if (marketValue < 300000 && marketValue > 175000) {
          // setState(() {
          //   assessment_level = 0.10;
          // });
          return '10 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.20;
          // });
          return '20 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.25;
          // });
          return '25 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        }
        break;
      case 'Agricultural':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return '45 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 750000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return '55 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return '65 ';
        } else if (marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        }
        break;
      case 'Commercial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return '75 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
        }
        break;
      case 'Industrial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return '75 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
          return '80 ';
        }
        break;
      case 'Mineral':
        break;
      case 'Timberland':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return '45 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return '55 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return '65 ';
        } else if (marketValue < 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        }
        break;
      default:
    }
    return '';
  }

  double assessmentValue(marketValues, property_class) {
    final marketValue = double.parse(marketValues);
    switch (property_class) {
      case 'Residential':
        if (marketValue < 175000) {
          // setState(() {
          //   assessment_level = 0;
          // });
          return marketValue * 0;
        } else if (marketValue < 300000 && marketValue > 175000) {
          // setState(() {
          //   assessment_level = 0.10;
          // });
          return marketValue * 0.10;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.20;
          // });
          return marketValue * 0.20;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.25;
          // });
          return marketValue * 0.25;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        }
        break;
      case 'Agricultural':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return marketValue * 0.45;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 750000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return marketValue * 0.55;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return marketValue * 0.65;
        } else if (marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        }
        break;
      case 'Commercial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return marketValue * 0.75;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
        }
        break;
      case 'Industrial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return marketValue * 0.75;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
          return marketValue * 0.80;
        }
        break;
      case 'Mineral':
        break;
      case 'Timberland':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return marketValue * 0.45;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return marketValue * 0.55;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return marketValue * 0.65;
        } else if (marketValue < 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        }
        break;
      default:
    }
    return 0;
  }

  calculateAdditionalItems(List<AdditionalItems> items) {
    double sum = 0;
    double product = 1;

    for (AdditionalItems value in items) {
      sum += double.parse(value.adjustedMarketVal);
    }

    return sum;
  }

  calculateTotalConstructionCost(buildingCost, additionalItems) {
    double sum = 0;
    double product = 1;

    sum = buildingCost + calculateAdditionalItems(additionalItems);

    return sum;
  }

  calculateMarketValue(buildingCost, additionalItems, dep) {
    double sum = 0;
    double depreciation = 0;
    double total = 0;

    sum = buildingCost + calculateAdditionalItems(additionalItems);

    depreciation = sum * dep;

    total = sum - depreciation;

    return total;
  }

  calculateDepCost(buildingCost, additionalItems, dep) {
    double sum = 0;
    double depreciation = 0;
    double total = 0;

    sum = buildingCost + calculateAdditionalItems(additionalItems);

    depreciation = sum * dep;

    total = sum - depreciation;

    return depreciation;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProgressHUD(
        padding: const EdgeInsets.all(24),
        backgroundColor: Colors.black87,
        indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
        child: BlocConsumer<AdditionalItemBloc, AdditionalItemState>(
          listener: (context, state) {
            if (state is AdditionalItemsLoading) {
              final progress = ProgressHUD.of(context);
              progress!.showWithText("Please wait...");
            }
            if (state is AdditionalItemsLoaded) {
              final progress = ProgressHUD.of(context);
              progress?.dismiss();
            }
            if (state is AdditionalItemsErrorState) {
              final progress = ProgressHUD.of(context);
              progress?.dismiss();
            }
          },
          builder: (context, state) {
            if (state is AdditionalItemsLoaded) {
              return SingleChildScrollView(
                child: Container(
                  margin: const EdgeInsets.only(left: 20.0, right: 20.0),
                  child: Column(
                    children: [
                      Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 20, right: 0, bottom: 20),
                        child: const Text('PROPERTY APPRAISAL',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18),
                            textAlign: TextAlign.left),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Unit Construction Cost",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              formKey.currentState!.value['bldg_type']
                                      .unitValue +
                                  ' sq.m',
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Building Core",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              '',
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 40),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Sub-total",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              (double.parse(formKey
                                          .currentState!.value['total_area']) *
                                      double.parse(formKey.currentState!
                                          .value['bldg_type'].unitValue))
                                  .toString(),
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 40),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Cost of Additional Items",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              '',
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Sub-total",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              calculateAdditionalItems(state.items).toString(),
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Total Construction Cost",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              calculateTotalConstructionCost(
                                      (double.parse(formKey.currentState!
                                              .value['total_area']) *
                                          double.parse(formKey.currentState!
                                              .value['bldg_type'].unitValue)),
                                      state.items)
                                  .toString(),
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 40),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Depreciation Rate",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          SizedBox(
                            width: 90,
                            height: 25,
                            child: FormBuilderTextField(
                              name: 'depRate',
                              decoration: normalTextFieldStyle("", ""),
                              validator: FormBuilderValidators.compose([]),
                              onChanged: (value) {
                                setState(() {
                                  depRate = double.parse(value!);
                                });
                              },
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Depreciation Cost",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              calculateDepCost(
                                      (double.parse(formKey.currentState!
                                              .value['total_area']) *
                                          double.parse(formKey.currentState!
                                              .value['bldg_type'].unitValue)),
                                      state.items,
                                      depRate)
                                  .toString(),
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Total % Depreciation",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              '${(depRate * 100).toStringAsFixed(2)}%',
                              textAlign: TextAlign.right,
                            ),
                          )
                        ],
                      ),
                      const SizedBox(height: 15),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              "Market Value",
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 13),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            child: Text(
                              calculateMarketValue(
                                      (double.parse(formKey.currentState!
                                              .value['total_area']) *
                                          double.parse(formKey.currentState!
                                              .value['bldg_type'].unitValue)),
                                      state.items,
                                      depRate)
                                  .toString(),
                              textAlign: TextAlign.right,
                            ),
                          ),
                        ],
                      ),
                      Row(
                        children: [
                          Expanded(
                            flex: 1,
                            child: SingleChildScrollView(
                                scrollDirection: Axis.horizontal,
                                child: Container(
                                    margin: const EdgeInsets.only(
                                        left: 20.0, right: 20.0),
                                    child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        children: [
                                          Container(
                                            margin: const EdgeInsets.only(
                                                left: 0,
                                                top: 20,
                                                right: 0,
                                                bottom: 20),
                                            child: const Text(
                                                'PROPERTY ASSESSMENT',
                                                style: TextStyle(
                                                    fontWeight: FontWeight.bold,
                                                    fontSize: 18),
                                                textAlign: TextAlign.left),
                                          ),
                                          Column(
                                            children: [
                                              Row(
                                                children: [
                                                  Container(
                                                    width: 100,
                                                    margin:
                                                        const EdgeInsets.only(
                                                            top: 15, left: 15),
                                                    padding:
                                                        const EdgeInsets.all(
                                                            5.0),
                                                    child: const Text(
                                                      'Actual Use',
                                                      style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: 13,
                                                      ),
                                                      textAlign:
                                                          TextAlign.center,
                                                    ),
                                                  ),
                                                  Container(
                                                    width: 100,
                                                    margin:
                                                        const EdgeInsets.only(
                                                            top: 15, left: 15),
                                                    padding:
                                                        const EdgeInsets.all(
                                                            5.0),
                                                    child: const Text(
                                                      'Market Value',
                                                      style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: 13,
                                                      ),
                                                      textAlign:
                                                          TextAlign.center,
                                                    ),
                                                  ),
                                                  Container(
                                                    width: 100,
                                                    margin:
                                                        const EdgeInsets.only(
                                                            top: 15, left: 15),
                                                    padding:
                                                        const EdgeInsets.all(
                                                            5.0),
                                                    child: const Text(
                                                      'Ass. Level',
                                                      style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: 13,
                                                      ),
                                                      textAlign:
                                                          TextAlign.center,
                                                    ),
                                                  ),
                                                  Container(
                                                    width: 100,
                                                    margin:
                                                        const EdgeInsets.only(
                                                            top: 15, left: 15),
                                                    padding:
                                                        const EdgeInsets.all(
                                                            5.0),
                                                    child: const Text(
                                                      'Ass. Value',
                                                      style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: 13,
                                                      ),
                                                      textAlign:
                                                          TextAlign.center,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              SizedBox(
                                                height: 59,
                                                child: Row(
                                                  children: [
                                                    Container(
                                                      width: 100,
                                                      margin:
                                                          const EdgeInsets.only(
                                                              top: 15,
                                                              left: 15),
                                                      padding:
                                                          const EdgeInsets.all(
                                                              5.0),
                                                      child: Text(
                                                        formKey.currentState
                                                                ?.value[
                                                            'actual_use']!,
                                                        style: TextStyle(
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: 13,
                                                        ),
                                                        textAlign:
                                                            TextAlign.center,
                                                      ),
                                                    ),
                                                    Container(
                                                      width: 100,
                                                      margin:
                                                          const EdgeInsets.only(
                                                              top: 15,
                                                              left: 15),
                                                      padding:
                                                          const EdgeInsets.all(
                                                              5.0),
                                                      child: Text(
                                                        calculateMarketValue(
                                                                (double.parse(formKey
                                                                            .currentState!
                                                                            .value[
                                                                        'total_area']) *
                                                                    double.parse(formKey
                                                                        .currentState!
                                                                        .value[
                                                                            'bldg_type']
                                                                        .unitValue)),
                                                                state.items,
                                                                depRate)
                                                            .toString(),
                                                        style: TextStyle(
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: 13,
                                                        ),
                                                        textAlign:
                                                            TextAlign.center,
                                                      ),
                                                    ),
                                                    Container(
                                                      width: 100,
                                                      margin:
                                                          const EdgeInsets.only(
                                                              top: 15,
                                                              left: 15),
                                                      padding:
                                                          const EdgeInsets.all(
                                                              5.0),
                                                      child: Text(
                                                        assessmentLevel(
                                                            calculateMarketValue(
                                                                    (double.parse(formKey.currentState!.value[
                                                                            'total_area']) *
                                                                        double.parse(formKey
                                                                            .currentState!
                                                                            .value[
                                                                                'bldg_type']
                                                                            .unitValue)),
                                                                    state.items,
                                                                    depRate)
                                                                .toString(),
                                                            formKey.currentState
                                                                    ?.value[
                                                                'actual_use']),
                                                        style: TextStyle(
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: 13,
                                                        ),
                                                        textAlign:
                                                            TextAlign.center,
                                                      ),
                                                    ),
                                                    Container(
                                                      width: 100,
                                                      margin:
                                                          const EdgeInsets.only(
                                                              top: 15,
                                                              left: 15),
                                                      padding:
                                                          const EdgeInsets.all(
                                                              5.0),
                                                      child: Text(
                                                        assessmentValue(
                                                                calculateMarketValue(
                                                                        (double.parse(formKey.currentState!.value['total_area']) *
                                                                            double.parse(formKey
                                                                                .currentState!
                                                                                .value[
                                                                                    'bldg_type']
                                                                                .unitValue)),
                                                                        state
                                                                            .items,
                                                                        depRate)
                                                                    .toString(),
                                                                formKey.currentState
                                                                        ?.value[
                                                                    'actual_use'])
                                                            .toString(),
                                                        style: TextStyle(
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: 13,
                                                        ),
                                                        textAlign:
                                                            TextAlign.center,
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              )
                                            ],
                                          ),
                                        ]))),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 50,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          CustomButton(
                            icon: const Icon(Icons.chevron_left_rounded,
                                color: Colors.white),
                            onPressed: () {
                              {
                                widget.PrevBtn();
                              }
                              ;
                            },
                          ),
                          CustomButton(
                            icon: const Icon(Icons.chevron_right_rounded,
                                color: Colors.white),
                            onPressed: () async {
                              {
                                final tempID =
                                    await SharedPreferences.getInstance();
                                print(tempID.getInt('tempid'));
                                var appraisals = PropertyAppraisal(
                                    id: 1,
                                    bldgapprDetailsId:
                                        tempID.getInt('tempid')! - 1,
                                    unitconstructCost: formKey.currentState!
                                        .value['bldg_type'].unitValue,
                                    buildingCore: 'test',
                                    unitconstructSubtotal:
                                        (double.parse(formKey.currentState!.value['total_area']) *
                                                double.parse(formKey
                                                    .currentState!
                                                    .value['bldg_type']
                                                    .unitValue))
                                            .toString(),
                                    depreciationRate: depRate.toString(),
                                    depreciationCost: calculateDepCost(
                                            (double.parse(formKey.currentState!.value['total_area']) *
                                                double.parse(formKey.currentState!.value['bldg_type'].unitValue)),
                                            state.items,
                                            depRate)
                                        .toString(),
                                    costAddItems: calculateAdditionalItems(state.items).toString(),
                                    addItemsSubtotal: calculateAdditionalItems(state.items).toString(),
                                    totalpercentDepreciation: (depRate * 100).toStringAsFixed(2),
                                    marketValue: calculateMarketValue((double.parse(formKey.currentState!.value['total_area']) * double.parse(formKey.currentState!.value['bldg_type'].unitValue)), state.items, depRate).toString(),
                                    totalArea: formKey.currentState!.value['total_area'],
                                    actualUse: "Residential");
                                context.read<PropertyAppraisalBloc>()
                                  ..add(AddPropertyAppraisal(
                                      appraisal: appraisals));

                                widget.NextBtn();
                              }
                              ;
                            },
                          )
                        ],
                      ),
                    ],
                  ),
                ),
              );
            }
            return Container();
          },
        ),
      ),
    );
  }

  Future<bool> _waitForAddPropertyInfoToComplete() async {
    // Wait for the state change indicating completion
    final propertyInfoState = context.read<PropertyInfoBloc>().state;

    if (propertyInfoState is PropertyInfoErrorState) {
      // Check if the add operation was unsuccessful
      return true; // You'll need to define this in your state class
    }

    // Return false if the state didn't change as expected
    return false;
  }
}
