import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/view_models/user_view_model.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart';
import 'package:mc_common_app/widgets/txt_field.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class UpdateUserCityCountry extends StatefulWidget {
  const UpdateUserCityCountry({Key? key}) : super(key: key);

  @override
  State<UpdateUserCityCountry> createState() => _UpdateUserCityCountryState();
}

class _UpdateUserCityCountryState extends State<UpdateUserCityCountry> {
  DropValue? city;
  DropValue? country;

  @override
  void initState() {
    print(AppState().getUser!.data!.userInfo!.toJson());
    context.read<UserVM>().getAllCountriesForUser();
    super.initState();
  }

  @override
  void dispose() {
    city = null;
    country = null;
    print(" Dispose Called");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, UserVM uVM, Widget? child) {
      return Scaffold(
        appBar: CustomAppBar(
          title: LocaleKeys.updateCity.tr(),
        ),
        body: Column(
          children: [
            uVM.userCountries != null
                ? Container(
                padding: const EdgeInsets.only(right: 20, left: 20, top: 12),
                child: Builder(builder: (context) {
                  List<DropValue> userCountryDrop = [];
                  for (var element in uVM.userCountries!.data!) {
                    var countryid = country == null ? AppState().getUser.data!.userInfo!.countryId : country!.id;
                    print("Country ID" + countryid.toString());
                    if (countryid == element.id) {
                      print("Country Matched");
                      country = DropValue(element.id?.toInt() ?? 0, element.countryName ?? "", "");
                    }
                    userCountryDrop.add(DropValue(element.id?.toInt() ?? 0, element.countryName ?? "", ""));
                  }
                  return DropdownField(
                        (DropValue value) async {
                      country = value;
                      city = null;
                      await uVM.getAllCitiesForUser(country!.id);
                      setState(() {});
                    },
                    list: userCountryDrop,
                    dropdownValue: country != null && country != -1 ? DropValue(country!.id, country!.value, "") : null,
                    hint: country != null && country != -1 ? country!.value : "${LocaleKeys.country.tr()} *",
                    //  errorValue: adVM.vehicleCountryId.errorValue,
                  );
                }))
                : SizedBox(),
            uVM.userCities != null && uVM.userCities!.data!.isNotEmpty
                ? Container(
                padding: const EdgeInsets.only(right: 20, left: 20, top: 12),
                child: Builder(builder: (context) {
                  List<DropValue> userCityDrop = [];
                  for (var element in uVM.userCities!.data!) {
                    var cid = city == null ? AppState().getUser.data!.userInfo!.cityId : city!.id;
                    print("City ID" + cid.toString());
                    if (cid == element.id) {
                      city = DropValue(element.id?.toInt() ?? 0, element.cityName ?? "", "");
                    }
                    userCityDrop.add(DropValue(element.id?.toInt() ?? 0, element.cityName ?? "", ""));
                  }
                  return DropdownField(
                        (DropValue value) {
                      city = value;
                      setState(() {});
                    },
                    list: userCityDrop,
                    dropdownValue: city != null && city!.id != -1 ? DropValue(city!.id, city!.value, "") : null,
                    hint: city != null && city != -1 ? city!.value : "${LocaleKeys.city.tr()} *",
                    //  errorValue: uVM.userCities!.data!.isEmpty ? "No Cities Found" : "",
                  );
                }))
                : SizedBox(),
            20.height,
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: ShowFillButton(
                title: LocaleKeys.confirm.tr(),
                maxWidth: double.infinity,
                onPressed: () async {

                  await uVM.userDetailsUpdate(
                      context, AppState().getUser.data!.userInfo!.firstName!, AppState().getUser.data!.userInfo!.lastName!, city != null ? city?.id.toString() : null, city!.value, country!.value);
                },
              ),
            ),
          ],
        ),
      );
    });
  }
}
