import 'package:bloc/bloc.dart';
import 'upload_event.dart';
import 'upload_state.dart';
import '../../repositories/file_repository.dart';

class UploadBloc extends Bloc<UploadEvent, UploadState> {
  final FileRepository _fileRepository;

  UploadBloc(this._fileRepository) : super(UploadInitial()) {
    on<StartUpload>(_onStartUpload);
    on<UploadProgress>(_onUploadProgress);
    on<UploadPaused>(_onUploadPaused);
    on<UploadCompleted>(_onUploadCompleted);
    on<UploadFailed>(_onUploadFailed);
    on<CancelUpload>(_onCancelUpload);
    on<ResetUploads>(_onResetUploads);
  }

  void _onStartUpload(StartUpload event, Emitter<UploadState> emit) async {
    final uploads = event.files
        .map((file) => UploadItem(fileName: file.name))
        .toList();
    emit(UploadInProgress(uploads));

    for (final file in event.files) {
      try {
        // Simulate upload
        await _fileRepository.uploadFile(event.orgId, file);

        add(UploadCompleted(file));
      } catch (e) {
        add(UploadFailed(fileName: file.name, error: e.toString()));
      }
    }
  }

  void _onUploadProgress(UploadProgress event, Emitter<UploadState> emit) {
    final currentState = state;
    if (currentState is UploadInProgress) {
      final updatedUploads = currentState.uploads.map((upload) {
        if (upload.fileName == event.fileName) {
          return UploadItem(
            fileName: upload.fileName,
            progress: event.progress,
            isPaused: upload.isPaused,
            isCompleted: upload.isCompleted,
            error: upload.error,
          );
        }
        return upload;
      }).toList();
      emit(UploadInProgress(updatedUploads));
    }
  }

  void _onUploadPaused(UploadPaused event, Emitter<UploadState> emit) {
    final currentState = state;
    if (currentState is UploadInProgress) {
      final updatedUploads = currentState.uploads.map((upload) {
        if (upload.fileName == event.fileName) {
          return UploadItem(
            fileName: upload.fileName,
            progress: upload.progress,
            isPaused: true,
            isCompleted: upload.isCompleted,
            error: upload.error,
          );
        }
        return upload;
      }).toList();
      emit(UploadInProgress(updatedUploads));
    }
  }

  void _onUploadCompleted(UploadCompleted event, Emitter<UploadState> emit) {
    final currentState = state;
    if (currentState is UploadInProgress) {
      final updatedUploads = currentState.uploads.map((upload) {
        if (upload.fileName == event.file.name) {
          return UploadItem(
            fileName: upload.fileName,
            progress: 1.0,
            isPaused: false,
            isCompleted: true,
            error: null,
          );
        }
        return upload;
      }).toList();
      emit(UploadInProgress(updatedUploads));
    }
  }

  void _onUploadFailed(UploadFailed event, Emitter<UploadState> emit) {
    final currentState = state;
    if (currentState is UploadInProgress) {
      final updatedUploads = currentState.uploads.map((upload) {
        if (upload.fileName == event.fileName) {
          return UploadItem(
            fileName: upload.fileName,
            progress: upload.progress,
            isPaused: upload.isPaused,
            isCompleted: upload.isCompleted,
            error: event.error,
          );
        }
        return upload;
      }).toList();
      emit(UploadInProgress(updatedUploads));
    }
  }

  void _onCancelUpload(CancelUpload event, Emitter<UploadState> emit) {
    // Reset to initial
    emit(UploadInitial());
  }

  void _onResetUploads(ResetUploads event, Emitter<UploadState> emit) {
    emit(UploadInitial());
  }
}
