import 'dart:developer';
import 'dart:io';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/main.dart';
import 'package:mc_common_app/models/user_models/image_response.dart';
import 'package:mc_common_app/repositories/user_repo.dart';
import 'package:mc_common_app/services/common_services.dart';
import 'package:mc_common_app/utils/enums.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/appointments_view_model.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:mc_common_app/view_models/chat_view_model.dart';
import 'package:mc_common_app/view_models/requests_view_model.dart';
import 'package:provider/provider.dart';

class DashboardVmCustomer extends BaseVM {
  final CommonAppServices commonServices;
  final UserRepo userRepo;

  DashboardVmCustomer({required this.commonServices, required this.userRepo});

  String pickedImage = "";

  int selectedNavbarBarIndex = 2;

  void onNavbarTapped(int index) async {
    logger.i(AppState().getDeviceToken);
    selectedNavbarBarIndex = index;
    notifyListeners();
  }

  void pickImageFromPhone(BuildContext context, int sourceFlag) async {
    final File? pickedImageFile = await commonServices.pickImageFromPhone(sourceFlag);
    if (pickedImageFile == null) {
      return;
    }
    int sizeInBytes = pickedImageFile.lengthSync();
    if (sizeInBytes > 1000) {
      Utils.showToast(LocaleKeys.fileLarger.tr());
      return;
    } else {
      String image64 = Utils.convertFileToBase64(pickedImageFile);

      Utils.showLoading(context);
      ImageResponse response = await userRepo.updateUserImage(image64);
      Utils.hideLoading(context);
      Navigator.pop(context);
      if (response.messageStatus == 1) {
        Utils.showToast(LocaleKeys.imageUploaded.tr());
        AppState().getUser.data!.userInfo!.userImageUrl = response.data;
      } else {
        Utils.showToast(response.message ?? "");
      }
    }
  }

  Future<void> onRefresh(BuildContext context, {DashboardRouteEnum dashboardRouteEnum = DashboardRouteEnum.none}) async {
    AdVM adVM = Provider.of<AdVM>(context, listen: false);
    AppointmentsVM appointmentsVM = Provider.of<AppointmentsVM>(context, listen: false);
    RequestsVM requestsVM = Provider.of<RequestsVM>(context, listen: false);
    ChatVM chatVM = Provider.of<ChatVM>(context, listen: false);
    appointmentsVM.populateBranchesFilterList();
    appointmentsVM.populateAppointmentsFilterList();
    adVM.populateAdsFilterList();
    requestsVM.populateDataForRequestsFilter();
    await appointmentsVM.getMyAppointmentsForCustomer();
    await appointmentsVM.applyFilterOnBranches(index: 0); // to get all branches!
    await appointmentsVM.getMyRecentBranches(); // to get my recent branches

    if (dashboardRouteEnum != DashboardRouteEnum.fromAdsPayment && dashboardRouteEnum != DashboardRouteEnum.fromAdsSubmit) {
      await adVM.getMyAds();
      await adVM.getExploreAds();
    }

    await requestsVM.getRequests();
    await adVM.getVehicleTypes();
    await adVM.getVehicleAdsDuration();
    await chatVM.buildHubConnection(context);

    // if (appointmentsVM.myAppointments.isEmpty) {
    //   await appointmentsVM.getMyAppointments();
    // }
    //
    // if (appointmentsVM.nearbyBranches.isEmpty) {
    //   await appointmentsVM.applyFilterOnBranches(index: 0); // to get all branches!
    // }
    // if (appointmentsVM.myRecentBranches.isEmpty) {
    //   await appointmentsVM.getMyRecentBranches(); // to get my recent branches!
    // }
    //
    // if (adVM.myAds.isEmpty) {
    //   await adVM.getMyAds();
    // }
    // if (adVM.exploreAds.isEmpty) {
    //   await adVM.getExploreAds();
    // }
    // if (requestsVM.myRequests.isEmpty) {
    //   await requestsVM.getRequests(appType: AppType.customer);
    // }
    //
    // if (adVM.vehicleTypes.isEmpty) {
    //   await adVM.getVehicleTypes();
    // }
    //
    // if (adVM.vehicleAdsDurations.isEmpty) {
    //   await adVM.getVehicleAdsDuration();
    // }

    // adVM.updateVehicleAdDurationId(
    //   SelectionModel(
    //     selectedId: adVM.vehicleAdsDurations.first.id ?? 0,
    //     selectedOption: "${adVM.vehicleAdsDurations.first.days} Days",
    //     itemPrice: adVM.vehicleAdsDurations.first.price!.toInt().toString(),
    //   ),
    // );
  }

  Future<ImageResponse> updateUserImage(String image) async {
    return await userRepo.updateUserImage(image);
  }
}
