import 'dart:io';

import 'package:bloc/bloc.dart';
import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/util/logger.dart';
import 'package:equatable/equatable.dart';
import 'package:image_picker/image_picker.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:image/image.dart' as i;
part 'register_state.dart';

class RegisterCubit extends Cubit<RegisterState> {
  final AppRepository repository;
  RegisterCubit(this.repository) : super(RegisterInitial());
  File? image;
  void pickImageEvent() async {
    emit(RegisterLoading());
    final picker = ImagePicker();
    try {
      XFile? imageFile = await picker.pickImage(source: ImageSource.gallery);
      image = File(imageFile!.path);
      final i.Image? capturedImage = i.decodeImage(await imageFile.readAsBytes());
      final i.Image orientedImage = i.bakeOrientation(capturedImage!);
      File fileLocal = await File(imageFile.path).writeAsBytes(i.encodeJpg(orientedImage, quality: 100));
      emit(PickImageSuccess(fileLocal));
    } catch (e) {
      logger.e(e);
      emit(RegisterFailure(R.string.error.tr()));
    }
  }
}
