import 'dart:io';
import 'package:car_customer_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/classes/app_state_cm.dart';
import 'package:mc_common_app/models/user/image_response.dart';
import 'package:mc_common_app/repositories/user_repo.dart';
import 'package:mc_common_app/services/services.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';

class DashboardVM extends BaseVM {
  final CommonServices commonServices;
  final UserRepo userRepo;

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

  String pickedImage = "";

  int selectedNavbarBarIndex = 2;

  void onNavbarTapped(int index) {
    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());
        AppStateCm().getUser.data!.userInfo!.userImageUrl = response.data;
      } else {
        Utils.showToast(response.message ?? "");
      }
    }
  }

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